proc

package
v0.0.0-...-ec22ba9 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 17 Imported by: 5

Documentation

Overview

Process manager for mewld

Index

Constants

This section is empty.

Variables

View Source
var (
	RollRestartChannel = make(chan int)
	DiagChannel        = make(chan DiagResponse)
	PingCheckStop      = make(chan int) // Channel to stop the ping checker
	ErrTimeout         = errors.New("timeoutError")
	ErrLockedInstance  = errors.New("lockedInstanceError")
)

Functions

func DefaultOnReshard

func DefaultOnReshard(l *InstanceList, i *Instance, cm *ClusterMap, oldShards []uint64, newShards []uint64) error

func DefaultStart

func DefaultStart(l *InstanceList, i *Instance, cm *ClusterMap) error

Types

type ClusterMap

type ClusterMap struct {
	ID     int      // The clusters ID
	Name   string   // The friendly name of the cluster
	Shards []uint64 // The shard numbers/IDs of the cluster
}

Represents a "cluster" of instances.

func GetClusterList

func GetClusterList(clusterNames []string, shards uint64, perCluster uint64) []ClusterMap

Given a shard count, return the shards for each cluster (128 would be [[0, 1, ..., 9], [10, 11, ..., 19]]) However, if the shard count is not a multiple of the number of clusters, the last cluster will have fewer shards etc. So, 1 would mean [[0]]

type DiagResponse

type DiagResponse struct {
	Nonce string        // Random nonce used to validate that a nonce comes from a specific diag request
	Data  []ShardHealth // The shard health data
}

type GatewayBot

type GatewayBot struct {
	Url               string            `json:"url"`
	Shards            uint64            `json:"shards"`
	SessionStartLimit SessionStartLimit `json:"session_start_limit"`
}

Represents a response from the 'Get Gateway Bot' API

func GetGatewayBot

func GetGatewayBot(c *config.CoreConfig) (*GatewayBot, error)

Gets gateway information from discord

type Instance

type Instance struct {
	StartedAt        time.Time     `json:"StartedAt"`        // The time the instance was last started
	SessionID        string        `json:"SessionID"`        // Internally used to identify the instance
	ClusterID        int           `json:"ClusterID"`        // ClusterID from clustermap
	Shards           []uint64      `json:"Shards"`           // Shards that this instance is responsible for currently, should be equal to clustermap
	Command          *exec.Cmd     `json:"-"`                // Command that is running on the instance
	Active           bool          `json:"Active"`           // Whether or not this instance is active
	ClusterHealth    []ShardHealth `json:"ClusterHealth"`    // Cache of shard health from a ping
	CurrentlyKilling bool          `json:"CurrentlyKilling"` // Whether or not we are currently killing this instance
	LockClusterTime  *time.Time    `json:"LockClusterTime"`  // Time at which we last locked the cluster
	LaunchedFully    bool          `json:"LaunchedFully"`    // Whether or not we have launched the instance fully (till launch_next)
	LastChecked      time.Time     `json:"LastChecked"`      // The last time the shard was checked for health.
}

Represents a instance of a cluster

func (*Instance) AcquireLock

func (i *Instance) AcquireLock()

Waits for the instance lock to end (if any), then returns thus acquiring the lock

func (*Instance) AcquireLockAndLock

func (i *Instance) AcquireLockAndLock(l *InstanceList, subsystem string)

Acquires the lock and then locks it

func (*Instance) Lock

func (i *Instance) Lock(l *InstanceList, subsystem string, critical bool) error

Attempts to lock the cluster from observing actions (such as shutdown/startup/rolling restart etc.)

func (*Instance) Locked

func (i *Instance) Locked() bool

Returns true if the cluster is locked, otherwise false

func (*Instance) Unlock

func (i *Instance) Unlock()

type InstanceList

type InstanceList struct {
	LastClusterStartedAt time.Time          `json:"LastClusterStartedAt"`
	Map                  []ClusterMap       `json:"Map"`            // The list of clusters (ClusterMap) which defines how mewld will start clusters
	Instances            []*Instance        `json:"Instances"`      // The list of instances (Instance) which are running
	ShardCount           uint64             `json:"ShardCount"`     // The number of shards in “mewld“
	GatewayBot           *GatewayBot        `json:"GetGatewayBot"`  // The response from Get Gateway Bot
	Config               *config.CoreConfig `json:"-"`              // The configuration for “mewld“ ANTIRAID-SPECIFIC: Don't marshal this into JSON
	LoaderData           *LoaderData        `json:"-"`              // Internal loader data, to make mewld embeddable
	Dir                  string             `json:"Dir"`            // The base directory instances will use when loading clusters
	Redis                *redis.Client      `json:"-"`              // Core redis instance
	Ctx                  context.Context    `json:"-"`              // Context for redis
	StartMutex           *sync.Mutex        `json:"-"`              // Internal mutex to prevent multiple instances from starting at the same time
	RollRestarting       bool               `json:"RollRestarting"` // whether or not we are roll restarting (rolling restart)
	FullyUp              bool               `json:"FullyUp"`        // whether or not we are fully up
}

The final store of the ClusterMap list as well as a instance store

func (*InstanceList) Acknowledge

func (l *InstanceList) Acknowledge(cmdId string) error

Acknowledge a published message

func (*InstanceList) ActionLog

func (l *InstanceList) ActionLog(payload map[string]any)

Creates a new action log for a cluster

func (*InstanceList) Cluster

func (l *InstanceList) Cluster(i *Instance) *ClusterMap

Returns the ClusterMap for a specific instance

func (*InstanceList) Init

func (l *InstanceList) Init() error

Initializes the instance list and sets needed fields, must be called

This function may update the config URL to include sane defaults

func (*InstanceList) InstanceByID

func (l *InstanceList) InstanceByID(id int) *Instance

Returns a Instance given its cluster ID

func (*InstanceList) KillAll

func (l *InstanceList) KillAll()

Kills all clusters in the instance list

func (*InstanceList) Observe

func (l *InstanceList) Observe(i *Instance, sid string)

Observes a cluster and restarts it if necessary (unexpected death of the cluster)

func (*InstanceList) PingCheck

func (l *InstanceList) PingCheck(i *Instance, sid string)

Pings a cluster every “ping_interval“ to check for responsiveness, restarts dead clusters if not responding to “diag“ ping checks

func (*InstanceList) Reshard

func (l *InstanceList) Reshard() error

Reshards an instance list

EXPERIMENTAL: Set 'reshard' in experimental_features to enable this

func (*InstanceList) RollingRestart

func (l *InstanceList) RollingRestart()

Begins a rolling restart, should be called as a seperate goroutine

func (*InstanceList) ScanShards

func (l *InstanceList) ScanShards(i *Instance) ([]ShardHealth, error)

Scans all shards of a instance using a diag request to get the shard health

func (*InstanceList) SendMessage

func (l *InstanceList) SendMessage(cmdId string, payload any, scope string, action string) error

Sends a message to redis

func (*InstanceList) Start

func (l *InstanceList) Start(i *Instance) error

Starts a instance in the instance list, this locks the cluster if not already locked before unlocking after startup

func (*InstanceList) StartNext

func (l *InstanceList) StartNext()

Starts the next cluster in the instance list if possible

func (*InstanceList) Stop

func (l *InstanceList) Stop(i *Instance) StopCode

Attempts to stop a instance returning a status code defining whether the cluster could be stopped or not

type LoaderData

type LoaderData struct {
	Start       func(l *InstanceList, i *Instance, cm *ClusterMap) error                                         // Start function
	OnReshard   func(l *InstanceList, i *Instance, cm *ClusterMap, oldShards []uint64, newShards []uint64) error // OnReshard function is called when the bot is resharded by mewld
	OnActionLog func(payload map[string]any) error
}

Internal loader data, to make mewld embeddable and more extendible

type SessionStartLimit

type SessionStartLimit struct {
	Total          uint64 `json:"total"`           // Total number of session starts the current user is allowed
	Remaining      uint64 `json:"remaining"`       // Remaining number of session starts the current user is allowed
	ResetAfter     uint64 `json:"reset_after"`     // Number of milliseconds after which the limit resets
	MaxConcurrency uint64 `json:"max_concurrency"` // Number of identify requests allowed per 5 seconds
}

type ShardHealth

type ShardHealth struct {
	ShardID uint64  `json:"shard_id"` // The shard ID
	Up      bool    `json:"up"`       // Whether or not the shard is up
	Latency float64 `json:"latency"`  // Latency of the shard (optional, send if possible)
	Guilds  uint64  `json:"guilds"`   // The number of guilds in the shard. Is optional
	Users   uint64  `json:"users"`    // The number of users in the shard. Is optional
}

type StopCode

type StopCode int
const (
	StopCodeNormal        StopCode = 0
	StopCodeRestartFailed StopCode = -1
)

Jump to

Keyboard shortcuts

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