Slide 1

Slide 1 text

© Black Inc. Cloud Gaming Platform with Go castaneai Backend engineer Black Inc.

Slide 2

Slide 2 text

© Black Inc. Agenda ● Introduction of Cloud Gaming ● Backend Architecture and Go ○ Stateful game server ○ Video streaming ● Cloud Native Infrastructure ○ Cloud providers support ○ Build and deploy 2

Slide 3

Slide 3 text

© Black Inc. Introduction of Cloud Gaming

Slide 4

Slide 4 text

© Black Inc. What is cloud gaming? Player Gaming Server Player Action Video Streaming 4

Slide 5

Slide 5 text

© Black Inc. OOParts Our cloud gaming platform for visual novels https://oo.parts 5

Slide 6

Slide 6 text

© Black Inc. Backend Architecture and Go

Slide 7

Slide 7 text

© Black Inc. OOParts Architecture Frontend Backend 7

Slide 8

Slide 8 text

© Black Inc. 92.6% 8

Slide 9

Slide 9 text

© Black Inc. 9

Slide 10

Slide 10 text

© Black Inc. Why Go? How Go?

Slide 11

Slide 11 text

© Black Inc. Stateful game server

Slide 12

Slide 12 text

© Black Inc. Player Gaming Server Player Action Video Streaming Game Session 12

Slide 13

Slide 13 text

© 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 complex 13

Slide 14

Slide 14 text

© Black Inc. Handling a stateful game server ● How do we handle stateful? ● Real-world examples ○ TCP Socket ○ WebRTC Agent ○ ... 14

Slide 15

Slide 15 text

© Black Inc. Handling a stateful game server ● State machine Waiting For Session Starting Game Playing Game Waiting For Reconnect Shutdown 15

Slide 16

Slide 16 text

© Black Inc. Handling a stateful game server type State string const ( StateWaitingForSession State = "WaitingForSession" StateStartingGame State = "StartingGame" StatePlayingGame State = "PlayingGame" ... ) type StateLogic interface { Run(ctx context.Context, ts Transition) (State, Transition) } 16

Slide 17

Slide 17 text

© Black Inc. Handling a stateful game server 17 State machine loop & logging func (a *Agent) MainLoop(ctx context.Context) error { for { prevState := nextState nextState, transition = logic.Run(ctx, transition) log.Infof(" +++ State [%s] -> [%s]", prevState, nextState) ...

Slide 18

Slide 18 text

© Black Inc. Handling asynchronous events ● Many asynchronous events on cloud gaming 😱 Key-frame request Player disconnected SIGTERM game window resized game exited Player input Game Server 18

Slide 19

Slide 19 text

© Black Inc. Handling asynchronous events in Go ● Goroutine ● Channel 19

Slide 20

Slide 20 text

© Black Inc. Handling asynchronous events in Go select { } 20

Slide 21

Slide 21 text

© Black Inc. Handling asynchronous events in Go select { } Player disconnected SIGTERM game exited Player input case <- case <- case <- case <- ... ... 21 🎉

Slide 22

Slide 22 text

© Black Inc. Stateful and asynchronous events select { } case <- return StateB case <- case <- State A select { } case <- case <- case <- return StateA State B 22

Slide 23

Slide 23 text

© Black Inc. Stateful and asynchronous events select { } case <- case <- case <- case <- State A select { } case <- case <- case <- case <- State D select { } case <- case <- case <- case <- State B select { } case <- case <- case <- case <- State C 23

Slide 24

Slide 24 text

© 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

Slide 25

Slide 25 text

© Black Inc. Video streaming

Slide 26

Slide 26 text

© Black Inc. Player Gaming Server Player Action Video Streaming 26

Slide 27

Slide 27 text

© 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-source 27

Slide 28

Slide 28 text

© Black Inc. How to deliver low-latency video? Player X11 Game PulseAudio Capturing frame and encoding Peering and streaming media 28

Slide 29

Slide 29 text

© Black Inc. What language? 29 https://en.wikipedia.org/wiki/GStreamer

Slide 30

Slide 30 text

© Black Inc. What language? 30 https://en.wikipedia.org/wiki/GStreamer https://en.wikipedia.org/wiki/WebRTC

Slide 31

Slide 31 text

© Black Inc. go-gst ● https://github.com/tinyzimmer/go-gst ● Gstreamer bindings and utilities for golang ● Using CGO 31

Slide 32

Slide 32 text

© Black Inc. Pion WebRTC ● https://github.com/pion/webrtc ● A Pure Go implementation of WebRTC API 32

Slide 33

Slide 33 text

© Black Inc. Why Go, How Go? ● We use gstreamer and WebRTC for video streaming ○ Both of them have Go libraries! 33

Slide 34

Slide 34 text

© Black Inc. Cloud Native Infrastructure

Slide 35

Slide 35 text

© Black Inc. Cloud Native Infrastructure ● Multi services, multi containers ● Immutable infrastructure ● Using managed service in public cloud 35

Slide 36

Slide 36 text

© Black Inc. Cloud providers support

Slide 37

Slide 37 text

© 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

Slide 38

Slide 38 text

© Black Inc. gocloud 38 import ( "gocloud.dev/blob" _ "gocloud.dev/blob/fileblob" ) import ( "gocloud.dev/blob" _ "gocloud.dev/blob/gcsblob" ) Local Google Cloud

Slide 39

Slide 39 text

© Black Inc. Build and Deploy

Slide 40

Slide 40 text

© Black Inc. Build and Deploy How do we build and deploy game servers? 40

Slide 41

Slide 41 text

© Black Inc. Build and Deploy How do we build and deploy game servers? Build Deploy 41

Slide 42

Slide 42 text

© Black Inc. ko ● https://github.com/google/ko ● container image builder for Go applications 42

Slide 43

Slide 43 text

© Black Inc. Without ko FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... $ docker build -t gcr.io/... $ docker push gcr.io/... 43 Writing Dockerfile per container

Slide 44

Slide 44 text

© Black Inc. Without ko FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... 44 service A FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... service B FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... service C FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... service D, ... FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... 😅

Slide 45

Slide 45 text

© Black Inc. With ko FROM golang as builder RUN CGO_ENABLED=0 go build ... FROM scratch COPY --from=builder ... $ ko publish ./src/app => gcr.io/... $ docker build -t gcr.io/... $ docker push gcr.io/... 45 Go package path

Slide 46

Slide 46 text

© Black Inc. Deploying game servers 46 apiVersion: "agones.dev/v1" kind: Fleet spec: template: spec: template: spec: containers: - name: agent image: ko://path/to/... kustomize apply YAML replace the image

Slide 47

Slide 47 text

© 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

Slide 48

Slide 48 text

© Black Inc. go-task $ task deploy:serviceA-local $ task deploy:serviceA-dev $ task deploy:serviceA-prd 48 common deploy task local variables dev variables ... ● YAML-based ● flexible variables

Slide 49

Slide 49 text

© Black Inc. Conclusion

Slide 50

Slide 50 text

© 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 Go 50 … and more!

Slide 51

Slide 51 text

© Black Inc. Cloud Gaming Platform with Go castaneai Backend engineer Black Inc.