-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient-rr.go
76 lines (60 loc) · 1.29 KB
/
client-rr.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
package gearman // import "github.com/nathanaelle/gearman/v2"
import (
"context"
"log"
"sync"
)
type (
rrServer struct {
ctx context.Context
debug *log.Logger
pool []Client
climutex *sync.Mutex
idx int
}
)
// RoundRobinClient creates a new (Client)[#Client]
func RoundRobinClient(ctx context.Context, debug *log.Logger) Client {
c := new(rrServer)
c.debug = debug
c.ctx = ctx
c.climutex = new(sync.Mutex)
c.idx = 0
return c
}
func (c *rrServer) Receivers() (<-chan Message, context.Context) {
return nil, nil
}
func (c *rrServer) Close() error {
c.climutex.Lock()
defer c.climutex.Unlock()
for _, server := range c.pool {
server.Close()
}
return nil
}
// Add a list of gearman server
func (c *rrServer) AddServers(servers ...Conn) {
c.climutex.Lock()
defer c.climutex.Unlock()
for _, server := range servers {
ssc := SingleServerClient(c.ctx, c.debug)
ssc.AddServers(server)
c.pool = append(c.pool, ssc)
}
}
func (c *rrServer) Submit(req Task) Task {
c.climutex.Lock()
defer c.climutex.Unlock()
cli := c.pool[c.idx]
c.idx = (c.idx + 1) % (len(c.pool))
return cli.Submit(req)
}
func (c *rrServer) AssignTask(tid TaskID) {
}
func (c *rrServer) GetTask(tid TaskID) Task {
return nil
}
func (c *rrServer) ExtractTask(tid TaskID) Task {
return nil
}