-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
110 lines (99 loc) · 2.15 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package openai_scheduler
import (
"fmt"
"time"
)
type Scheduler struct {
workableCh chan *Client
brokenCh chan *Client
outOfServiceCh chan *Client
startedDaemon bool
stopCh chan bool
}
func NewScheduler(tokens []string) *Scheduler {
var clis []*Client
for _, token := range tokens {
clis = append(clis, NewClient(token))
}
return NewSchedulerFromClients(clis)
}
func NewSchedulerFromClients(clients []*Client) *Scheduler {
if len(clients) == 0 {
panic("no gpt token")
}
sche := &Scheduler{
startedDaemon: false,
}
sche.workableCh = make(chan *Client, len(clients))
sche.brokenCh = make(chan *Client, len(clients))
sche.outOfServiceCh = make(chan *Client, len(clients))
for _, gpt := range clients {
sche.workableCh <- gpt
}
//sche.statistic()
return sche
}
func (s *Scheduler) StartDaemon() {
if s.startedDaemon {
return
}
s.startedDaemon = true
go s.daemon()
s.Statistic()
}
func (s *Scheduler) Dispose() {
if s.startedDaemon {
s.stopCh <- true
}
close(s.workableCh)
close(s.brokenCh)
close(s.outOfServiceCh)
}
func (s *Scheduler) DaemonStarted() bool {
return s.startedDaemon
}
func (s *Scheduler) GetWorkable() int {
return len(s.workableCh)
}
func (s *Scheduler) daemon() {
for {
select {
case <-s.stopCh:
return
case gpt := <-s.outOfServiceCh:
time.Sleep(time.Minute)
gpt.Status = OK
s.workableCh <- gpt
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> OK")
s.Statistic()
}
}
}
func (s *Scheduler) Statistic() {
fmt.Println("[GPT STATISTICS]",
"Valid:", len(s.workableCh),
"Broken:", len(s.brokenCh),
"OOS:", len(s.outOfServiceCh))
if len(s.workableCh) == 0 {
fmt.Println("[GPT SCHEDULER] NO GPT Available!")
}
}
func (s *Scheduler) GetClient() *Client {
gpt := <-s.workableCh
if gpt.IsOk() {
s.workableCh <- gpt
return gpt
}
if gpt.IsBanned() {
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> BANNED")
s.brokenCh <- gpt
} else if gpt.IsOutOfService() {
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> OOS")
s.outOfServiceCh <- gpt
} else {
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> OOS")
s.outOfServiceCh <- gpt
}
s.Statistic()
return s.GetClient()
}