-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
92 lines (81 loc) · 1.63 KB
/
pool.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
package transfer
import (
"fmt"
"log"
"sync"
"time"
)
// ClientsPool is client pool for transfer servers
type ClientsPool struct {
cs clientSlice
nest *sync.Pool
lock sync.RWMutex
connTimeout time.Duration
callTimeout time.Duration
addrs []string
}
func (cp *ClientsPool) NewClient() interface{} {
cp.lock.RLock()
c, err := cp.cs.minBy(quick)
cp.lock.RUnlock()
if err == nil {
return c
}
log.Println("NewClient error:", err.Error())
return nil
}
// Put cache client
func (cp *ClientsPool) Put(c *client) {
if time.Since(c.connTime).Hours() > 2 {
c.close()
return
}
cp.nest.Put(c)
}
// Get get client from cache or new
func (cp *ClientsPool) Get() *client {
c, ok := (cp.nest.Get()).(*client)
if ok {
return c
}
cp.cs.aggregateInt(aggregate)
return nil
}
// ReListClients reset the clients list
func (cp *ClientsPool) ReListClients(al []string) {
newcs := make(clientSlice, 0)
for _, a := range al {
c := NewClient(a, cp.connTimeout, cp.callTimeout)
newcs = append(newcs, c)
}
newcs.shuffle()
cp.lock.Lock()
cp.cs = newcs
cp.addrs = al
cp.lock.Unlock()
}
// CreatePool create client pool
func CreatePool(al []string, connTW, callTW time.Duration) *ClientsPool {
cp := new(ClientsPool)
cp.nest = &sync.Pool{
New: cp.NewClient,
}
cp.connTimeout = connTW
cp.callTimeout = callTW
cp.ReListClients(al)
return cp
}
// Call the rpc call
func (cp *ClientsPool) Call(method string, args interface{}, reply interface{}) error {
c := cp.Get()
if c == nil {
return fmt.Errorf("Cant get client")
}
err := c.call(method, args, reply)
if err == nil {
cp.Put(c)
return nil
}
c.close()
return err
}