Go Conference 2021 Autumn
© Black Inc.Cloud Gaming Platformwith GocastaneaiBackend engineerBlack Inc.
View Slide
© Black Inc.Agenda● Introduction of Cloud Gaming● Backend Architecture and Go○ Stateful game server○ Video streaming● Cloud Native Infrastructure○ Cloud providers support○ Build and deploy2
© Black Inc.Introduction ofCloud Gaming
© Black Inc.What is cloud gaming?Player Gaming ServerPlayer ActionVideo Streaming4
© Black Inc.OOPartsOur cloud gaming platform for visual novelshttps://oo.parts5
© Black Inc.Backend Architectureand Go
© Black Inc.OOParts ArchitectureFrontend Backend7
© Black Inc.92.6%8
© Black Inc. 9
© Black Inc.Why Go?How Go?
© Black Inc.Stateful game server
© Black Inc.Player Gaming ServerPlayer ActionVideo StreamingGame Session12
© Black Inc.Handling a stateful game server● Game session is “Stateful”○ Web-based frontend, but game server is NOT a HTTP Server○ Long-time (1h~)○ Resumable---> Stateful is complex13
© Black Inc.Handling a stateful game server● How do we handle stateful?● Real-world examples○ TCP Socket○ WebRTC Agent○ ...14
© Black Inc.Handling a stateful game server● State machineWaiting ForSessionStarting GamePlaying GameWaiting ForReconnectShutdown15
© Black Inc.Handling a stateful game servertype State stringconst (StateWaitingForSession State = "WaitingForSession"StateStartingGame State = "StartingGame"StatePlayingGame State = "PlayingGame"...)type StateLogic interface {Run(ctx context.Context, ts Transition) (State, Transition)}16
© Black Inc.Handling a stateful game server17State machine loop & loggingfunc (a *Agent) MainLoop(ctx context.Context) error {for {prevState := nextStatenextState, transition = logic.Run(ctx, transition)log.Infof(" +++ State [%s] -> [%s]", prevState, nextState)...
© Black Inc.Handling asynchronous events● Many asynchronous events on cloud gaming 😱Key-framerequestPlayerdisconnectedSIGTERMgame windowresizedgame exitedPlayer inputGame Server18
© Black Inc.Handling asynchronous events in Go● Goroutine● Channel19
© Black Inc.Handling asynchronous events in Goselect {}20
© Black Inc.Handling asynchronous events in Goselect {}PlayerdisconnectedSIGTERMgame exitedPlayer inputcase <-case <-case <-case <-......21🎉
© Black Inc.Stateful and asynchronous eventsselect {}case <-return StateBcase <-case <-State Aselect {}case <-case <-case <-return StateAState B22
© Black Inc.Stateful and asynchronous eventsselect {}case <-case <-case <-case <-State Aselect {}case <-case <-case <-case <-State Dselect {}case <-case <-case <-case <-State Bselect {}case <-case <-case <-case <-State C23
© Black Inc.Why Go, How Go?● Go provides good asynchronous event handling○ Goroutine and Channel○ Handling async events with `select` statement● Good debugging for asynchronous○ built-in race detector○ built-in goroutine profiler (pprof)24
© Black Inc.Video streaming
© Black Inc.Player Gaming ServerPlayer ActionVideo Streaming26
© Black Inc.How to deliver low-latency video?● low-latency streaming● peer-to-peer (P2P) connection● standardization by W3C and IETF● media processing (video/audio)● free and open-source27
© Black Inc.How to deliver low-latency video?PlayerX11GamePulseAudioCapturing frameand encodingPeering andstreaming media28
© Black Inc.What language?29https://en.wikipedia.org/wiki/GStreamer
© Black Inc.What language?30https://en.wikipedia.org/wiki/GStreamer https://en.wikipedia.org/wiki/WebRTC
© Black Inc.go-gst● https://github.com/tinyzimmer/go-gst● Gstreamer bindings and utilities for golang● Using CGO31
© Black Inc.Pion WebRTC● https://github.com/pion/webrtc● A Pure Go implementation of WebRTC API32
© Black Inc.Why Go, How Go?● We use gstreamer and WebRTC for video streaming○ Both of them have Go libraries!33
© Black Inc.Cloud NativeInfrastructure
© Black Inc.Cloud Native Infrastructure● Multi services, multi containers● Immutable infrastructure● Using managed service in public cloud35
© Black Inc.Cloud providerssupport
© Black Inc.gocloud● https://github.com/google/go-cloud● Abstraction layer for cloud providers○ Amazon, Azure, Google, …○ Local environment support● Storage, KVS, DB, PubSub, Secrets, ...37
© Black Inc.gocloud38import ("gocloud.dev/blob"_ "gocloud.dev/blob/fileblob")import ("gocloud.dev/blob"_ "gocloud.dev/blob/gcsblob")Local Google Cloud
© Black Inc.Build and Deploy
© Black Inc.Build and DeployHow do we build and deploy game servers?40
© Black Inc.Build and DeployHow do we build and deploy game servers?Build Deploy41
© Black Inc.ko● https://github.com/google/ko● container image builder for Go applications42
© Black Inc.Without koFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...$ docker build -t gcr.io/...$ docker push gcr.io/...43Writing Dockerfile per container
© Black Inc.Without koFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...44service AFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...service BFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...service CFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...service D, ...FROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...FROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...😅
© Black Inc.With koFROM golang as builderRUN CGO_ENABLED=0 go build ...FROM scratchCOPY --from=builder ...$ ko publish ./src/app=> gcr.io/...$ docker build -t gcr.io/...$ docker push gcr.io/...45Go package path
© Black Inc.Deploying game servers46apiVersion: "agones.dev/v1"kind: Fleetspec:template:spec:template:spec:containers:- name: agentimage: ko://path/to/... kustomizeapply YAMLreplace the image
© Black Inc.go-task● https://github.com/go-task/task● simpler GNU Make alternative written in Go● We use go-task as a task runner○ build○ deploy○ code generation○ ...47
© Black Inc.go-task$ task deploy:serviceA-local$ task deploy:serviceA-dev$ task deploy:serviceA-prd48common deploytasklocalvariablesdevvariables...● YAML-based● flexible variables
© Black Inc.Conclusion
© Black Inc.Conclusion● Go is well suited for cloud gaming platform development○ features for writing stateful servers○ bindings of media frameworks (gstreamer, WebRTC)● Many of the tools for Cloud Native are powered by Go50… and more!