sharding

package
v0.13.4 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: Apache-2.0 Imports: 8 Imported by: 13

Documentation

Index

Constants

This section is empty.

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.

func ShardMaxConcurrencyKey added in v0.12.0

func ShardMaxConcurrencyKey(shardID int, maxConcurrency int) int

ShardMaxConcurrencyKey returns the bucket the given shardID with maxConcurrency belongs to.

Types

type Config

type Config struct {
	Logger                    log.Logger
	ShardIDs                  map[int]struct{}
	ShardCount                int
	ShardSplitCount           int
	AutoScaling               bool
	GatewayCreateFunc         gateway.CreateFunc
	GatewayConfigOpts         []gateway.ConfigOpt
	RateLimiter               RateLimiter
	RateRateLimiterConfigOpts []RateLimiterConfigOpt
}

Config lets you configure your ShardManager instance.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

func (*Config) Apply

func (c *Config) Apply(opts []ConfigOpt)

Apply applies the given ConfigOpt(s) to the Config

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.

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 WithLogger

func WithLogger(logger log.Logger) ConfigOpt

WithLogger sets the logger of the ShardManager.

func WithRateLimiter

func WithRateLimiter(rateLimiter RateLimiter) ConfigOpt

WithRateLimiter lets you inject your own srate.RateLimiter into the ShardManager.

func WithRateRateLimiterConfigOpt added in v0.12.0

func WithRateRateLimiterConfigOpt(opts ...RateLimiterConfigOpt) ConfigOpt

WithRateRateLimiterConfigOpt lets you configure the default srate.RateLimiter used by 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 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 RateLimiter added in v0.12.0

type RateLimiter interface {
	// Logger returns the logger the RateLimiter uses
	Logger() log.Logger

	// Close gracefully closes the RateLimiter.
	// If the context deadline is exceeded, the RateLimiter will be closed immediately.
	Close(ctx context.Context)

	// WaitBucket waits for the given shardID bucket to be available for new logins.
	// If the context deadline is exceeded, WaitBucket will return immediately and no login will be attempted.
	WaitBucket(ctx context.Context, shardID int) error

	// UnlockBucket unlocks the given shardID bucket.
	UnlockBucket(shardID int)
}

RateLimiter limits how many shards can log in to Discord at the same time.

func NewRateLimiter added in v0.12.0

func NewRateLimiter(opts ...RateLimiterConfigOpt) RateLimiter

NewRateLimiter creates a new default RateLimiter with the given RateLimiterConfigOpt(s).

type RateLimiterConfig added in v0.12.0

type RateLimiterConfig struct {
	Logger         log.Logger
	MaxConcurrency int
}

RateLimiterConfig lets you configure your RateLimiter instance.

func DefaultRateLimiterConfig added in v0.12.0

func DefaultRateLimiterConfig() *RateLimiterConfig

DefaultRateLimiterConfig returns a RateLimiterConfig with sensible defaults.

func (*RateLimiterConfig) Apply added in v0.12.0

func (c *RateLimiterConfig) Apply(opts []RateLimiterConfigOpt)

Apply applies the given RateLimiterConfigOpt(s) to the RateLimiterConfig

type RateLimiterConfigOpt added in v0.12.0

type RateLimiterConfigOpt func(config *RateLimiterConfig)

RateLimiterConfigOpt is a type alias for a function that takes a RateLimiterConfig and is used to configure your Server.

func WithMaxConcurrency added in v0.12.0

func WithMaxConcurrency(maxConcurrency int) RateLimiterConfigOpt

WithMaxConcurrency sets the maximum number of concurrent identifies in 5 seconds.

func WithRateLimiterLogger added in v0.12.0

func WithRateLimiterLogger(logger log.Logger) RateLimiterConfigOpt

WithRateLimiterLogger sets the logger for the RateLimiter.

type ShardManager

type ShardManager interface {
	// Logger returns the logger used by the ShardManager.
	Logger() log.Logger

	// 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

	// 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 a copy of all shards as a map.
	Shards() map[int]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).

Jump to

Keyboard shortcuts

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