-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
85 lines (68 loc) · 1.41 KB
/
conn.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
package memcache
import (
"github.com/QuangTung97/go-memcache/memcache/netconn"
"sync"
"time"
)
type clientConn struct {
wg sync.WaitGroup
core *coreConnection
mut sync.Mutex
closed bool
closeChan chan struct{}
}
func sleepWithCloseChan(d time.Duration, closeChan <-chan struct{}) {
select {
case <-closeChan:
case <-time.After(d):
}
}
func newConn(addr string, options ...Option) *clientConn {
opts := computeOptions(options...)
nc, err := netconn.DialNewConn(addr, opts.connOptions...)
if err != nil {
opts.dialErrorLogger(err)
nc = netconn.ErrorNetConn(err)
}
c := &clientConn{
core: newCoreConnection(nc, opts),
closeChan: make(chan struct{}),
}
c.wg.Add(1)
go func() {
defer c.wg.Done()
for {
c.core.waitForError()
if c.core.isShuttingDown() {
return
}
nc, err = netconn.DialNewConn(addr, opts.connOptions...)
if err != nil {
opts.dialErrorLogger(err)
sleepWithCloseChan(opts.retryDuration, c.closeChan)
continue
}
c.core.resetNetConn(nc)
continue
}
}()
return c
}
func (c *clientConn) pushCommand(cmd *commandData) {
c.core.publish(cmd)
}
func (c *clientConn) shutdown() error {
c.mut.Lock()
if !c.closed {
c.closed = true
close(c.closeChan)
}
c.mut.Unlock()
c.core.shutdown()
err := c.core.sender.closeNetConn()
return err
}
func (c *clientConn) waitCloseCompleted() {
c.wg.Wait()
c.core.waitReceiverShutdown()
}