$30 off During Our Annual Pro Sale. View Details »

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.
    Cloud Gaming Platform
    with Go
    castaneai
    Backend engineer
    Black Inc.

    View Slide

  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

    View Slide

  3. © Black Inc.
    Introduction of
    Cloud Gaming

    View Slide

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

    View Slide

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

    View Slide

  6. © Black Inc.
    Backend Architecture
    and Go

    View Slide

  7. © Black Inc.
    OOParts Architecture
    Frontend Backend
    7

    View Slide

  8. © Black Inc.
    92.6%
    8

    View Slide

  9. © Black Inc. 9

    View Slide

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

    View Slide

  11. © Black Inc.
    Stateful game server

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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)
    ...

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  25. © Black Inc.
    Video streaming

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. © Black Inc.
    Cloud Native
    Infrastructure

    View Slide

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

    View Slide

  36. © Black Inc.
    Cloud providers
    support

    View Slide

  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

    View Slide

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

    View Slide

  39. © Black Inc.
    Build and Deploy

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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 ...
    😅

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  49. © Black Inc.
    Conclusion

    View Slide

  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!

    View Slide

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

    View Slide