Skip to content

Commit c0f9000

Browse files
feat(workers): add timeout when starting
1 parent 1063889 commit c0f9000

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

internal/bot/workers.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package bot
22

33
import (
44
"EverythingSuckz/fsb/config"
5+
"context"
56
"fmt"
67
"os"
78
"path/filepath"
89
"sync"
10+
"time"
911

1012
"github.com/celestix/gotgproto"
1113
"github.com/celestix/gotgproto/sessionMaker"
@@ -89,6 +91,9 @@ func GetNextWorker() *Worker {
8991

9092
func StartWorkers(log *zap.Logger) {
9193
log.Sugar().Info("Starting workers")
94+
timeOut := 30 * time.Second
95+
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
96+
defer cancel()
9297
Workers.Init(log)
9398
if config.ValueOf.UseSessionFile {
9499
log.Sugar().Info("Using session file for workers")
@@ -99,21 +104,30 @@ func StartWorkers(log *zap.Logger) {
99104
return
100105
}
101106
}
102-
c := make(chan struct{})
107+
c := make(chan struct{}, len(config.ValueOf.MultiTokens))
103108
for i := 0; i < len(config.ValueOf.MultiTokens); i++ {
104109
go func(i int) {
105110
err := Workers.Add(config.ValueOf.MultiTokens[i])
106111
if err != nil {
107112
log.Error("Failed to start worker", zap.Error(err))
108113
return
109114
}
110-
c <- struct{}{}
115+
select {
116+
default:
117+
c <- struct{}{}
118+
case <-ctx.Done():
119+
return
120+
}
111121
}(i)
112122
}
113123
// wait for all workers to start
114124
log.Sugar().Info("Waiting for all workers to start")
115125
for i := 0; i < len(config.ValueOf.MultiTokens); i++ {
116-
<-c
126+
select {
127+
case <-c:
128+
case <-time.After(timeOut):
129+
log.Sugar().Warnf("Timed out waiting for worker %d to start", i)
130+
}
117131
}
118132
}
119133

0 commit comments

Comments
 (0)