sharding

package
v0.19.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 15

Documentation

Index

Constants

View Source
const DefaultShardSplitCount = 2

DefaultShardSplitCount is the default count a shard should be split into when it needs re-sharding.

Variables

This section is empty.

Functions

func ShardIDByGuild

func ShardIDByGuild(guildID snowflake.ID, shardCount int) int

ShardIDByGuild returns the shard ID for the given guildID and shardCount.

Types

type ConfigOpt

type ConfigOpt func(config *config)

ConfigOpt is a type alias for a function that takes a config and is used to configure your Server.

func WithAutoScaling added in v0.11.0

func WithAutoScaling(autoScaling bool) ConfigOpt

WithAutoScaling sets whether the ShardManager should automatically re-shard shards if they are too large. This is disabled by default.

func WithCloseHandler added in v0.19.0

func WithCloseHandler(closeHandler gateway.CloseHandlerFunc) ConfigOpt

WithCloseHandler sets the function which is called when a gateway.Gateway is closed and could not reconnect (or auto reconnecting is disabled). If auto-scaling is enabled and the shard was closed due to gateway.CloseEventCodeShardingRequired, the closeHandler will not be called.

func WithDefault added in v0.19.0

func WithDefault() ConfigOpt

WithDefault returns a ConfigOpt that sets the default values for the ShardManager.

func WithDefaultIdentifyRateLimiterConfigOpt added in v0.19.0

func WithDefaultIdentifyRateLimiterConfigOpt(opts ...gateway.IdentifyRateLimiterConfigOpt) ConfigOpt

WithDefaultIdentifyRateLimiterConfigOpt lets you configure the default gateway.IdentifyRateLimiter used by the ShardManager and prepend the options to the existing ones.

func WithGatewayConfigOpts

func WithGatewayConfigOpts(opts ...gateway.ConfigOpt) ConfigOpt

WithGatewayConfigOpts lets you configure the gateway.Gateway created by the ShardManager.

func WithGatewayCreateFunc

func WithGatewayCreateFunc(gatewayCreateFunc gateway.CreateFunc) ConfigOpt

WithGatewayCreateFunc sets the function which is used by the ShardManager to create a new gateway.Gateway.

func WithIdentifyRateLimiter added in v0.19.0

func WithIdentifyRateLimiter(rateLimiter gateway.IdentifyRateLimiter) ConfigOpt

WithIdentifyRateLimiter lets you inject your own RateLimiter into the ShardManager.

func WithIdentifyRateLimiterConfigOpt added in v0.19.0

func WithIdentifyRateLimiterConfigOpt(opts ...gateway.IdentifyRateLimiterConfigOpt) ConfigOpt

WithIdentifyRateLimiterConfigOpt lets you configure the default gateway.IdentifyRateLimiter used by the ShardManager.

func WithLogger

func WithLogger(logger *slog.Logger) ConfigOpt

WithLogger sets the logger of the ShardManager.

func WithShardCount

func WithShardCount(shardCount int) ConfigOpt

WithShardCount sets the shard count of the ShardManager.

func WithShardIDs added in v0.12.0

func WithShardIDs(shardIDs ...int) ConfigOpt

WithShardIDs sets the shardIDs the ShardManager should manage.

func WithShardIDsWithStates added in v0.19.0

func WithShardIDsWithStates(shards map[int]ShardState) ConfigOpt

WithShardIDsWithStates sets the shardIDs and their ShardState the ShardManager should manage.

func WithShardSplitCount added in v0.11.0

func WithShardSplitCount(shardSplitCount int) ConfigOpt

WithShardSplitCount sets the count a shard should be split into if it is too large. This is only used if AutoScaling is enabled.

type ShardManager

type ShardManager interface {
	// Open opens all configured shards.
	Open(ctx context.Context)

	// Close closes all shards.
	Close(ctx context.Context)

	// OpenShard opens a specific shard.
	OpenShard(ctx context.Context, shardID int) error

	// ResumeShard resumes a specific shard with the given sessionID and sequence.
	ResumeShard(ctx context.Context, shardID int, state ShardState) error

	// CloseShard closes a specific shard.
	CloseShard(ctx context.Context, shardID int)

	// ShardByGuildID returns the gateway.Gateway for the shard that contains the given guild.
	ShardByGuildID(guildId snowflake.ID) gateway.Gateway

	// Shard returns the gateway.Gateway for the given shard ID.
	Shard(shardID int) gateway.Gateway

	// Shards returns all shards. This function is thread-safe.
	Shards() iter.Seq[gateway.Gateway]
}

ShardManager manages multiple gateway.Gateway connections. For more information on sharding see: https://discord.com/developers/docs/topics/gateway#sharding

func New

func New(token string, eventHandlerFunc gateway.EventHandlerFunc, opts ...ConfigOpt) ShardManager

New creates a new default ShardManager with the given token, eventHandlerFunc and ConfigOpt(s).

type ShardState added in v0.19.0

type ShardState struct {
	// SessionID is the session ID of the shard. This is used to resume the shard.
	SessionID string
	// Sequence is the sequence number of the shard. This is used to resume the shard.
	Sequence int
	// ResumeURL is the resume url to use for the shard. This is used to resume the shard.
	ResumeURL string
}

ShardState is used to tell a gateway.Gateway managed by the ShardManager which session & sequence it should use when starting the shard. This is useful for resuming shards when using the ShardManager.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL