Documentation ¶
Overview ¶
Package controller provides an API available to workers to write games. It also provides the internal API for starting games and watching.
Index ¶
- Constants
- Variables
- type Server
- func (s *Server) AddGameFrame(ctx context.Context, req *pb.AddGameFrameRequest) (*pb.AddGameFrameResponse, error)
- func (s *Server) Create(ctx context.Context, req *pb.CreateRequest) (*pb.CreateResponse, error)
- func (s *Server) DialAddress() string
- func (s *Server) EndGame(ctx context.Context, req *pb.EndGameRequest) (*pb.EndGameResponse, error)
- func (s *Server) ListGameFrames(ctx context.Context, req *pb.ListGameFramesRequest) (*pb.ListGameFramesResponse, error)
- func (s *Server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error)
- func (s *Server) Pop(ctx context.Context, _ *pb.PopRequest) (*pb.PopResponse, error)
- func (s *Server) Serve(listen string) error
- func (s *Server) Start(ctx context.Context, req *pb.StartRequest) (*pb.StartResponse, error)
- func (s *Server) Status(ctx context.Context, req *pb.StatusRequest) (*pb.StatusResponse, error)
- func (s *Server) ValidateSnake(ctx context.Context, req *pb.ValidateSnakeRequest) (*pb.ValidateSnakeResponse, error)
- func (s *Server) Wait()
- type Store
Constants ¶
const MaxTicks = 100
MaxTicks is the maximum amount of ticks that can be returned.
Variables ¶
var ( // ErrNotFound is thrown when a game is not found. ErrNotFound = status.Error(codes.NotFound, "controller: game not found") // ErrIsLocked is returned when a game is locked. ErrIsLocked = status.Error(codes.ResourceExhausted, "controller: game is locked") // ErrInvalidSequence is returned when a game tick is written with an // invalid sequence. ErrInvalidSequence = status.Error(codes.ResourceExhausted, "controller: invalid game tick sequence") )
var ErrLimited = status.Error(codes.ResourceExhausted, "controller: rate limited")
var LockExpiry = 5 * time.Second
LockExpiry is the time after which a lock will expire.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct { Store Store // contains filtered or unexported fields }
Server is a grpc server for pb.ControllerServer.
func (*Server) AddGameFrame ¶
func (s *Server) AddGameFrame(ctx context.Context, req *pb.AddGameFrameRequest) (*pb.AddGameFrameResponse, error)
AddGameFrame adds a new game frame to the game. A lock must be held for this call to succeed.
func (*Server) Create ¶
func (s *Server) Create(ctx context.Context, req *pb.CreateRequest) (*pb.CreateResponse, error)
Create creates a new game, but doesn't start running frames.
func (*Server) DialAddress ¶
DialAddress will return a localhost address to reach the server. This is useful if the server will select it's own port.
func (*Server) EndGame ¶
func (s *Server) EndGame(ctx context.Context, req *pb.EndGameRequest) (*pb.EndGameResponse, error)
EndGame sets the game status to complete. A lock must be held for this call to succeed.
func (*Server) ListGameFrames ¶
func (s *Server) ListGameFrames(ctx context.Context, req *pb.ListGameFramesRequest) (*pb.ListGameFramesResponse, error)
ListGameFrames will list all game frames given a limit and offset.
func (*Server) Ping ¶
func (s *Server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error)
Ping returns the health and current version of the server.
func (*Server) Pop ¶
func (s *Server) Pop(ctx context.Context, _ *pb.PopRequest) (*pb.PopResponse, error)
Pop should pop a game that is unlocked and unfinished from the queue, lock the game and return it to the worker to begin processing. This call will be polled by the workers.
func (*Server) Start ¶
func (s *Server) Start(ctx context.Context, req *pb.StartRequest) (*pb.StartResponse, error)
Start starts the game running, and will make it ready to be picked up by a worker.
func (*Server) Status ¶
func (s *Server) Status(ctx context.Context, req *pb.StatusRequest) (*pb.StatusResponse, error)
Status retrieves the game state including the last processed game frame.
func (*Server) ValidateSnake ¶
func (s *Server) ValidateSnake(ctx context.Context, req *pb.ValidateSnakeRequest) (*pb.ValidateSnakeResponse, error)
ValidateSnake takes a snake URL and sends requests to validate a snakes validity.
type Store ¶
type Store interface { // Lock will lock a specific game, returning a token that must be used to // write frames to the game. Lock(ctx context.Context, key, token string) (string, error) // Unlock will unlock a game if it is locked and the token used to lock it // is correct. Unlock(ctx context.Context, key, token string) error // PopGameID returns a new game that is unlocked and running. Workers call // this method through the controller to find games to process. PopGameID(context.Context) (string, error) // SetGameStatus is used to set a specific game status. This operation // should be atomic. SetGameStatus(c context.Context, id string, status rules.GameStatus) error // CreateGame will insert a game with the default game frames. CreateGame(context.Context, *pb.Game, []*pb.GameFrame) error // PushGameFrame will push a game frame onto the list of frames. PushGameFrame(c context.Context, id string, t *pb.GameFrame) error // ListGameFrames will list frames by an offset and limit, it supports // negative offset. ListGameFrames(c context.Context, id string, limit, offset int) ([]*pb.GameFrame, error) // GetGame will fetch the game. GetGame(context.Context, string) (*pb.Game, error) // Game Queue Length returns the number of games currently in the running state GameQueueLength(context.Context) (running int, waiting int, err error) }
Store is the interface to the game store. It implements locking for workers that are processing games and implements logic for distributing games to specific workers that need it.
func InMemStore ¶
func InMemStore() Store
InMemStore returns an in memory implementation of the Store interface.
func InstrumentStore ¶
InstrumentStore wraps all store methods to instrument the underlying calls.