Save 37% off PRO during our Black Friday Sale! »

Cloud Gaming Platform with Go

434123e6e96afad5905d876ff5e355ca?s=47 castaneai
November 13, 2021

Cloud Gaming Platform with Go

Go Conference 2021 Autumn

434123e6e96afad5905d876ff5e355ca?s=128

castaneai

November 13, 2021
Tweet

Transcript

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

    engineer Black Inc.
  2. © 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
  3. © Black Inc. Introduction of Cloud Gaming

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

    Player Action Video Streaming 4
  5. © Black Inc. OOParts Our cloud gaming platform for visual

    novels https://oo.parts 5
  6. © Black Inc. Backend Architecture and Go

  7. © Black Inc. OOParts Architecture Frontend Backend 7

  8. © Black Inc. 92.6% 8

  9. © Black Inc. 9

  10. © Black Inc. Why Go? How Go?

  11. © Black Inc. Stateful game server

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

    Game Session 12
  13. © 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
  14. © Black Inc. Handling a stateful game server • How

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

    machine Waiting For Session Starting Game Playing Game Waiting For Reconnect Shutdown 15
  16. © 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
  17. © 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) ...
  18. © 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
  19. © Black Inc. Handling asynchronous events in Go • Goroutine

    • Channel 19
  20. © Black Inc. Handling asynchronous events in Go select {

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

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

    case <- return StateB case <- case <- State A select { } case <- case <- case <- return StateA State B 22
  23. © 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
  24. © 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
  25. © Black Inc. Video streaming

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

    26
  27. © 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
  28. © Black Inc. How to deliver low-latency video? Player X11

    Game PulseAudio Capturing frame and encoding Peering and streaming media 28
  29. © Black Inc. What language? 29 https://en.wikipedia.org/wiki/GStreamer

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

  31. © Black Inc. go-gst • https://github.com/tinyzimmer/go-gst • Gstreamer bindings and

    utilities for golang • Using CGO 31
  32. © Black Inc. Pion WebRTC • https://github.com/pion/webrtc • A Pure

    Go implementation of WebRTC API 32
  33. © Black Inc. Why Go, How Go? • We use

    gstreamer and WebRTC for video streaming ◦ Both of them have Go libraries! 33
  34. © Black Inc. Cloud Native Infrastructure

  35. © Black Inc. Cloud Native Infrastructure • Multi services, multi

    containers • Immutable infrastructure • Using managed service in public cloud 35
  36. © Black Inc. Cloud providers support

  37. © 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
  38. © Black Inc. gocloud 38 import ( "gocloud.dev/blob" _ "gocloud.dev/blob/fileblob"

    ) import ( "gocloud.dev/blob" _ "gocloud.dev/blob/gcsblob" ) Local Google Cloud
  39. © Black Inc. Build and Deploy

  40. © Black Inc. Build and Deploy How do we build

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

    and deploy game servers? Build Deploy 41
  42. © Black Inc. ko • https://github.com/google/ko • container image builder

    for Go applications 42
  43. © 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
  44. © 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 ... 😅
  45. © 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
  46. © 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
  47. © 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
  48. © 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
  49. © Black Inc. Conclusion

  50. © 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!
  51. © Black Inc. Cloud Gaming Platform with Go castaneai Backend

    engineer Black Inc.