Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloud Gaming Platform with Go

castaneai
November 13, 2021

Cloud Gaming Platform with Go

Go Conference 2021 Autumn

castaneai

November 13, 2021
Tweet

More Decks by castaneai

Other Decks in Programming

Transcript

  1. © 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
  2. © 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
  3. © Black Inc. Handling a stateful game server • How

    do we handle stateful? • Real-world examples ◦ TCP Socket ◦ WebRTC Agent ◦ ... 14
  4. © Black Inc. Handling a stateful game server • State

    machine Waiting For Session Starting Game Playing Game Waiting For Reconnect Shutdown 15
  5. © 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
  6. © 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) ...
  7. © 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
  8. © Black Inc. Handling asynchronous events in Go select {

    } Player disconnected SIGTERM game exited Player input case <- case <- case <- case <- ... ... 21 🎉
  9. © Black Inc. Stateful and asynchronous events select { }

    case <- return StateB case <- case <- State A select { } case <- case <- case <- return StateA State B 22
  10. © 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
  11. © 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
  12. © 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
  13. © Black Inc. How to deliver low-latency video? Player X11

    Game PulseAudio Capturing frame and encoding Peering and streaming media 28
  14. © Black Inc. Why Go, How Go? • We use

    gstreamer and WebRTC for video streaming ◦ Both of them have Go libraries! 33
  15. © Black Inc. Cloud Native Infrastructure • Multi services, multi

    containers • Immutable infrastructure • Using managed service in public cloud 35
  16. © 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
  17. © Black Inc. gocloud 38 import ( "gocloud.dev/blob" _ "gocloud.dev/blob/fileblob"

    ) import ( "gocloud.dev/blob" _ "gocloud.dev/blob/gcsblob" ) Local Google Cloud
  18. © Black Inc. Build and Deploy How do we build

    and deploy game servers? Build Deploy 41
  19. © 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
  20. © 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 ... 😅
  21. © 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
  22. © 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
  23. © 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
  24. © 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
  25. © 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!