master

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 12 Imported by: 0

README

Master server that managed slave bot instances of YAGPDB

Currently its main duty is zero downtime restarts.

Slaves and the master talks over TCP using the event ID (uint32) followed by data in msgpack format.

Handshake:

Upon connecting, the slave need to tell the master that it's a slave, after that the master will tell the slave what to do, be it a cold start or a greacefull start.

Currently only 1 main slave and 1 slave being migrated to is supported, in the future i may add the ability to manage multiple slaves with their own sets of shards, on multiple machines.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EventsToStringMap = map[EventType]string{
	1:  "EvtSoftStart",
	2:  "EvtFullStart",
	3:  "EvtShardMigrationStart",
	4:  "EvtStopShard",
	5:  "EvtResume",
	6:  "EvtShutdown",
	7:  "EvtGuildState",
	8:  "EvtSlaveHello",
	9:  "EvtSoftStartComplete",
	10: "EvtShardStopped",
}
View Source
var EvtDataMap = map[EventType]func() interface{}{
	EvtSlaveHello:          func() interface{} { return new(SlaveHelloData) },
	EvtShardMigrationStart: func() interface{} { return new(ShardMigrationStartData) },
	EvtStopShard:           func() interface{} { return new(StopShardData) },
	EvtResume:              func() interface{} { return new(ResumeShardData) },
	EvtGuildState:          func() interface{} { return new(GuildStateData) },
}

Mapping of events to structs for their data

Functions

func EncodeEvent

func EncodeEvent(evtID EventType, data interface{}) ([]byte, error)

EncodeEvent encodes the event to the wire format The wire format is pretty basic, first 4 bytes is a uin32 representing what type of event this is next 4 bytes is another uin32 which represents the length of the body next n bytes is the body itself, which can even be empty in some cases

func Listen

func Listen(addr string)

Listen starts listening for slave connections, it also starts the monitor that will start new slaves if none has been spotted for 15 seconds (in case of crashes and such)

func StartSlave

func StartSlave()

Types

type Conn

type Conn struct {
	ID int64

	// Called on incoming messages
	MessageHandler func(*Message)

	// called when the connection is closed
	ConnClosedHanlder func()
	// contains filtered or unexported fields
}

Simple helper to manage the underlying connection using locks

func ConnFromNetCon

func ConnFromNetCon(conn net.Conn) *Conn

ConnFromNetCon wraos a Conn around a net.Conn

func (*Conn) Listen

func (c *Conn) Listen()

Listen starts listening for events on the connection

func (*Conn) Send

func (c *Conn) Send(evtID EventType, data interface{}) error

Send sends the specified message over the connection, marshaling the data using json this locks the writer

func (*Conn) SendLogErr

func (c *Conn) SendLogErr(evtID EventType, data interface{})

Same as Send but logs the error (usefull for launching send in new goroutines)

func (*Conn) SendNoLock

func (c *Conn) SendNoLock(data []byte) error

SendNoLock sends the specified message over the connection, marshaling the data using json This does no locking and the caller is responsible for making sure its not called in multiple goroutines at the same time

type EventType

type EventType uint32

The event IDs are hardcoded to preserve compatibility between versions

const (
	// Master -> slave
	EvtSoftStart EventType = 1 // Sent to signal the slave to not start anything other than start updating the state
	EvtFullStart EventType = 2 // Sent after a soft start event to start up everything other than the state

	// Sent to tell a shard that shard migration is about to happen, either to or from this shard
	// If from this shard to a new one, then responds with the session info needed
	// Otherwise, responds with no data once ready
	EvtShardMigrationStart EventType = 3

	// Sent to tell the slave to stop a shard, responds with EvtStopShard once all state has been transfered and shard has been stopped
	EvtStopShard EventType = 4
	// Sent to tell the slave to resume the specified shard, responds with EvtResume once finished
	EvtResume EventType = 5

	EvtShutdown   EventType = 6 // Sent to tell a slave to shut down, and immediately stop processing events, responds with the same event once shut down
	EvtGuildState EventType = 7

	// Slave -> master
	EvtSlaveHello        EventType = 8
	EvtSoftStartComplete EventType = 9  // Sent to indicate that all shards has been connected and are waiting for the full start event
	EvtShardStopped      EventType = 10 // Send by a slave when the shard has been stopped, includes state information for guilds related to that shardd
)

func (EventType) String

func (evt EventType) String() string

type GuildStateData

type GuildStateData struct {
	GuildState *dstate.GuildState
}

type Message

type Message struct {
	EvtID EventType
	Body  []byte
}

type ResumeShardData

type ResumeShardData struct {
	Shard int

	SessionID string
	Sequence  int64
}

type ShardMigrationStartData

type ShardMigrationStartData struct {
	FromThisSlave bool // If true, were migrating from this shard to a new one, otherwise its the other way around, (from another shard to this one)
	NumShards     int
}

type SlaveConn

type SlaveConn struct {
	Conn *Conn
}

Represents a connection from a master server to a slave

func NewSlaveConn

func NewSlaveConn(netConn net.Conn) *SlaveConn

NewSlaveConn creates a new slaveconn (connection from master to slave) from a net.Conn

type SlaveHelloData

type SlaveHelloData struct {
	Running bool // Wether the slave was already running or not
}

type StopShardData

type StopShardData struct {
	Shard int

	SessionID string
	Sequence  int64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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