Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fleet Manager Go interface #141

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ type Initializer interface {

// RegisterStorageIndexFilter can be used to define a filtering function for a given storage index.
RegisterStorageIndexFilter(indexName string, fn func(ctx context.Context, logger Logger, db *sql.DB, nk NakamaModule, write *StorageWrite) bool) error

// RegisterFleetManager can be used to register a FleetManager implementation that can be retrieved from the runtime using GetFleetManager().
RegisterFleetManager(fleetManagerInit FleetManagerInitializer) error
}

type PresenceReason uint8
Expand Down Expand Up @@ -1155,10 +1158,118 @@ type NakamaModule interface {
ChannelMessagesList(ctx context.Context, channelId string, limit int, forward bool, cursor string) (messages []*api.ChannelMessage, nextCursor string, prevCursor string, err error)

GetSatori() Satori
GetFleetManager() FleetManager
}

/*
Nakama fleet manager definitions.
*/
type InstanceInfo struct {
// A platform-specific unique instance identifier. Identifiers may be recycled for
// future use, but the underlying Fleet Manager platform is expected to ensure
// uniqueness at least among concurrently running instances.
Id string `json:"id"`
// Connection information in a platform-specific format, usually "address:port"
ConnectionInfo *ConnectionInfo `json:"connection_info"`
// When this instance was first created.
CreateTime time.Time `json:"create_time"`
// Number of active player sessions on the server
PlayerCount int `json:"player_count"`
// Status
Status string `json:"status"`
// Application-specific data for use in indexing and listings.
Metadata map[string]any `json:"metadata"`
}

type ConnectionInfo struct {
IpAddress string `json:"ip_address"`
DnsName string `json:"dns_name"`
Port int `json:"port"`
}

type JoinInfo struct {
InstanceInfo *InstanceInfo `json:"instance_info"`
SessionInfo []*SessionInfo `json:"session_info"`
}

type SessionInfo struct {
UserId string `json:"user_id"`
SessionId string `json:"session_id"`
}

type FmCreateStatus int

const (
// Create successfully created a new game instance.
CreateSuccess FmCreateStatus = iota
// Create request could not find a suitable instance within the configured timeout.
CreateTimeout
// Create failed to create a new game instance.
CreateError
)

type FmCallbackHandler interface {
// Generate a new callback id.
GenerateCallbackId() string
// Set the callback indexed by the generated id.
SetCallback(callbackId string, fn FmCreateCallbackFn)
// Invoke a callback by callback Id.
InvokeCallback(callbackId string, status FmCreateStatus, instanceInfo *InstanceInfo, sessionInfo []*SessionInfo, metadata map[string]any, err error)
}

type FleetUserLatencies struct {
// User id
UserId string
// Latency experienced by the user contacting a server in a fleet instance region.
LatencyInMilliseconds float32
// Region associated to the experienced latency value.
RegionIdentifier string
}

// FmCreateCallbackFn is the function that is invoked when Create asynchronously succeeds or fails (due to timeout or issues bringing up a new instance).
// The function params include all the information needed to inform a client with a realtime connection to the server of the status of the Create request,
// including the new instance connection information in case of success.
// If status != CreateSuccess, then instanceInfo, sessionInfo and metadata will be nil and err will contain an error message.
// If no userIds were provided to Create, then sessionInfo will be nil regardless of successful instance creation.
type FmCreateCallbackFn func(status FmCreateStatus, instanceInfo *InstanceInfo, sessionInfo []*SessionInfo, metadata map[string]any, err error)

type FleetManager interface {
// Get retrieves the most up-to-date information about an instance currently running
// in the Fleet Manager platform. An error is expected if the instance does not exist,
// either because it never existed or it was otherwise removed at some point.
Get(ctx context.Context, id string) (instance *InstanceInfo, err error)

// List retrieves a set of instances, optionally filtered by a platform-specific query.
// The limit and previous cursor inputs are used as part of pagination, if supported.
List(ctx context.Context, query string, limit int, previousCursor string) (list []*InstanceInfo, nextCursor string, err error)

// Create issues a request to the underlying Fleet Manager platform to create a new
// instance and initialize it with the given metadata. The metadata is expected to be
// application-specific and only relevant to the application itself, not the platform.
// The instance creation happens asynchronously - the passed callback is invoked once the
// creation process was either successful or failed.
// If a list of userIds is optionally provided, the new instance (on successful creation) will reserve slots
// for the respective clients to connect, and the callback will contain the required []*SessionInfo.
// Latencies is optional and its support depends on the Fleet Manager provider.
Create(ctx context.Context, maxPlayers int, userIds []string, latencies []FleetUserLatencies, metadata map[string]any, callback FmCreateCallbackFn) (err error)

// Join reserves a number of player slots in the target instance. These slots are reserved for a minute, after which,
// if clients do not connect to the instance to claim them, the returned SessionInfo will become invalid and the
// player slots will become available to new player sessions.
Join(ctx context.Context, id string, userIds []string, metadata map[string]string) (joinInfo *JoinInfo, err error)
}

type FleetManagerInitializer interface {
FleetManager
// Init function - it is called internally by RegisterFleetManager to expose NakamaModule and FmCallbackHandler.
// The implementation should keep references to nk and callbackHandler.
Init(nk NakamaModule, callbackHandler FmCallbackHandler) error
Update(ctx context.Context, id string, playerCount int, metadata map[string]any) error
Delete(ctx context.Context, id string) error
}

/*
Satori runtime integration defintions.
Satori runtime integration definitions.
*/
type Satori interface {
Authenticate(ctx context.Context, id string, ipAddress ...string) error
Expand Down
Loading