Merge branch 'feature/sharding' of https://github.com/b1naryth1ef/discordgo into b1naryth1ef-feature/sharding

This commit is contained in:
Bruce Marriner 2016-06-14 11:45:59 -05:00
commit d03b3eedfa
3 changed files with 27 additions and 1 deletions

View file

@ -40,6 +40,8 @@ func New(args ...interface{}) (s *Session, err error) {
StateEnabled: true, StateEnabled: true,
Compress: true, Compress: true,
ShouldReconnectOnError: true, ShouldReconnectOnError: true,
ShardID: 0,
NumShards: 1,
} }
// If no arguments are passed return the empty Session interface. // If no arguments are passed return the empty Session interface.

View file

@ -39,6 +39,10 @@ type Session struct {
// Should the session request compressed websocket data. // Should the session request compressed websocket data.
Compress bool Compress bool
// Sharding
ShardID int
NumShards int
// Should state tracking be enabled. // Should state tracking be enabled.
// State tracking is the best way for getting the the users // State tracking is the best way for getting the the users
// active guilds and the members of the guilds. // active guilds and the members of the guilds.

View file

@ -39,6 +39,7 @@ type handshakeData struct {
Properties handshakeProperties `json:"properties"` Properties handshakeProperties `json:"properties"`
LargeThreshold int `json:"large_threshold"` LargeThreshold int `json:"large_threshold"`
Compress bool `json:"compress"` Compress bool `json:"compress"`
Shard [2]int `json:"shard"`
} }
type handshakeOp struct { type handshakeOp struct {
@ -72,6 +73,16 @@ func (s *Session) Open() (err error) {
return return
} }
if s.NumShards <= 0 {
err = errors.New("NumShards must be greater or equal to 1")
return
}
if s.ShardID >= s.NumShards {
err = errors.New("ShardID must be less than NumShards")
return
}
if s.VoiceConnections == nil { if s.VoiceConnections == nil {
s.log(LogInformational, "creating new VoiceConnections map") s.log(LogInformational, "creating new VoiceConnections map")
s.VoiceConnections = make(map[string]*VoiceConnection) s.VoiceConnections = make(map[string]*VoiceConnection)
@ -130,6 +141,7 @@ func (s *Session) Open() (err error) {
}, },
250, 250,
s.Compress, s.Compress,
[2]int{s.ShardID, s.NumShards},
}, },
} }
s.log(LogInformational, "sending identify packet to gateway") s.log(LogInformational, "sending identify packet to gateway")
@ -373,7 +385,15 @@ func (s *Session) onEvent(messageType int, message []byte) {
s.log(LogInformational, "sending identify packet to gateway in response to Op9") s.log(LogInformational, "sending identify packet to gateway in response to Op9")
s.wsMutex.Lock() s.wsMutex.Lock()
err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}}) err = s.wsConn.WriteJSON(handshakeOp{
2,
handshakeData{
s.Token,
handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""},
250,
s.Compress,
[2]int{s.ShardID, s.NumShards},
}})
s.wsMutex.Unlock() s.wsMutex.Unlock()
if err != nil { if err != nil {
s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err) s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err)