Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta468 #470

Merged
merged 23 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions botgo/sessions/multi/multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package multi

import (
"sync"
"time"

"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/log"
"github.com/tencent-connect/botgo/sessions/manager"
"github.com/tencent-connect/botgo/token"
"github.com/tencent-connect/botgo/websocket"
)

type ShardManager struct {
Sessions []dto.Session
SessionChans []chan dto.Session
Clients []websocket.WebSocket
APInfo *dto.WebsocketAP
Token *token.Token
Intents *dto.Intent
StartInterval time.Duration
wg sync.WaitGroup
}

func NewShardManager(apInfo *dto.WebsocketAP, token *token.Token, intents *dto.Intent) *ShardManager {
m := &ShardManager{
APInfo: apInfo,
Token: token,
Intents: intents,
Sessions: make([]dto.Session, apInfo.Shards),
Clients: make([]websocket.WebSocket, apInfo.Shards),
SessionChans: make([]chan dto.Session, apInfo.Shards),
}
for i := range m.Sessions {
m.SessionChans[i] = make(chan dto.Session, 1)
}
m.StartInterval = manager.CalcInterval(apInfo.SessionStartLimit.MaxConcurrency)
return m
}

func (sm *ShardManager) StartAllShards() {
for i := uint32(0); i < sm.APInfo.Shards; i++ {
sm.StartShard(i)
}
sm.wg.Wait()
}

func (sm *ShardManager) StartShard(shardID uint32) {
sm.wg.Add(1)
go func() {
defer sm.wg.Done()
session := dto.Session{
URL: sm.APInfo.URL,
Token: *sm.Token,
Intent: *sm.Intents,
LastSeq: 0,
Shards: dto.ShardConfig{
ShardID: shardID,
ShardCount: sm.APInfo.Shards,
},
}
sm.Sessions[shardID] = session
sm.SessionChans[shardID] <- session

for session := range sm.SessionChans[shardID] {
time.Sleep(sm.StartInterval)
sm.newConnect(session, shardID)
}
}()
}

func (sm *ShardManager) newConnect(session dto.Session, shardID uint32) {
wsClient := websocket.ClientImpl.New(session)
sm.Clients[shardID] = wsClient
if err := wsClient.Connect(); err != nil {
log.Error(err)
sm.SessionChans[shardID] <- session // Reconnect
return
}
if session.ID != "" {
err := wsClient.Resume()
if err != nil {
log.Errorf("[ws/session] Resume error: %+v", err)
return
}
} else {
err := wsClient.Identify()
if err != nil {
log.Errorf("[ws/session] Identify error: %+v", err)
return
}
}
if err := wsClient.Listening(); err != nil {
log.Errorf("[ws/session] Listening error: %+v", err)
sm.SessionChans[shardID] <- session // Reconnect
}
}
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/hoshinonyaruko/gensokyo/url"
"github.com/hoshinonyaruko/gensokyo/webui"
"github.com/hoshinonyaruko/gensokyo/wsclient"
"github.com/tencent-connect/botgo/sessions/multi"
"google.golang.org/grpc"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -283,8 +284,12 @@ func main() {
if conf.Settings.ShardCount == 1 {
go func() {
wsInfo.Shards = uint32(conf.Settings.ShardNum)
if err = botgo.NewSessionManager().Start(wsInfo, token, &intent); err != nil {
log.Fatalln(err)
if wsInfo.Shards == 1 {
if err = botgo.NewSessionManager().Start(wsInfo, token, &intent); err != nil {
log.Fatalln(err)
}
} else {
multi.NewShardManager(wsInfo, token, &intent).StartAllShards()
}
}()
log.Printf("不使用分片,所有信息都由当前gensokyo处理...\n")
Expand Down
Loading