Share
Embed
Transcript
51 slides
-
© Black Inc. Cloud Gaming Platform with Go castaneai Backend
engineer Black Inc. -
© 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 -
© Black Inc. What is cloud gaming? Player Gaming Server
Player Action Video Streaming 4 -
© Black Inc. OOParts Our cloud gaming platform for visual
novels https://oo.parts 5 -
© Black Inc. Player Gaming Server Player Action Video Streaming
Game Session 12 -
© 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 -
© 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
machine Waiting For Session Starting Game Playing Game Waiting For Reconnect Shutdown 15 -
© 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 -
© 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) ... -
© 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 -
© Black Inc. Handling asynchronous events in Go select {
} Player disconnected SIGTERM game exited Player input case <- case <- case <- case <- ... ... 21 🎉 -
© Black Inc. Stateful and asynchronous events select { }
case <- return StateB case <- case <- State A select { } case <- case <- case <- return StateA State B 22 -
© 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 -
© 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. 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 -
© Black Inc. How to deliver low-latency video? Player X11
Game PulseAudio Capturing frame and encoding Peering and streaming media 28 -
© Black Inc. go-gst • https://github.com/tinyzimmer/go-gst • Gstreamer bindings and
utilities for golang • Using CGO 31 -
© Black Inc. Pion WebRTC • https://github.com/pion/webrtc • A Pure
Go implementation of WebRTC API 32 -
© Black Inc. Why Go, How Go? • We use
gstreamer and WebRTC for video streaming ◦ Both of them have Go libraries! 33 -
© Black Inc. Cloud Native Infrastructure • Multi services, multi
containers • Immutable infrastructure • Using managed service in public cloud 35 -
© 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. gocloud 38 import ( "gocloud.dev/blob" _ "gocloud.dev/blob/fileblob"
) import ( "gocloud.dev/blob" _ "gocloud.dev/blob/gcsblob" ) Local Google Cloud -
© Black Inc. Build and Deploy How do we build
and deploy game servers? 40 -
© Black Inc. Build and Deploy How do we build
and deploy game servers? Build Deploy 41 -
© Black Inc. ko • https://github.com/google/ko • container image builder
for Go applications 42 -
© 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 -
© 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 ... 😅 -
© 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 -
© 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 -
© 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-prd 48 common deploy task local variables dev variables ... • YAML-based • flexible variables -
© 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! -
© Black Inc. Cloud Gaming Platform with Go castaneai Backend
engineer Black Inc.