-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
153 lines (119 loc) · 2.72 KB
/
core.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package memcache
import (
"sync"
"sync/atomic"
)
type coreConnection struct {
responseReader *responseReader
sender *sender
shuttingDown uint32 // boolean
wg sync.WaitGroup
// job data
msgData []byte
cmdList *cmdListReader
}
func newCoreConnection(nc netConn, options *memcacheOptions) *coreConnection {
cmdSender := newSender(nc, 10)
c := &coreConnection{
responseReader: newResponseReader(21),
sender: cmdSender,
shuttingDown: 0,
msgData: make([]byte, options.bufferSize),
cmdList: newCmdListReader(cmdSender),
}
c.wg.Add(1)
go c.recvCommands()
return c
}
func (c *coreConnection) isShuttingDown() bool {
return atomic.LoadUint32(&c.shuttingDown) > 0
}
func (c *coreConnection) shutdown() {
atomic.StoreUint32(&c.shuttingDown, 1)
}
func (c *coreConnection) waitReceiverShutdown() {
c.wg.Wait()
}
func (c *coreConnection) publish(cmd *commandData) {
c.sender.publish(cmd)
}
func (c *coreConnection) resetNetConn(nc netConn) {
c.sender.resetNetConn(nc)
}
func (c *coreConnection) waitForError() {
c.sender.waitForError()
}
func (c *coreConnection) recvCommands() {
defer c.wg.Done()
for {
err := c.recvSingleCommand()
if err == ErrConnClosed { // cmd len == 0
return
}
if err != nil {
c.responseReader.reset()
reader := c.cmdList.current().reader
c.sender.setNetConnError(err, reader)
_ = reader.Close()
}
c.cmdList.current().setCompleted(err)
c.cmdList.next()
}
}
//revive:disable:cognitive-complexity
func (c *coreConnection) recvSingleCommand() error {
responseCount := 0
for {
cmdLen := c.cmdList.readIfExhausted()
if cmdLen == 0 {
return ErrConnClosed
}
current := c.cmdList.current()
// Read from response reader
ok := c.responseReader.hasNext()
if !ok {
if c.responseReader.hasError() != nil {
return c.responseReader.hasError() // TODO testing
}
n, err := current.reader.Read(c.msgData)
if err != nil {
return err
}
c.responseReader.recv(c.msgData[:n])
continue
}
current.responseData = c.responseReader.readData(current.responseData)
responseCount++
if responseCount >= current.cmdCount {
return nil
}
}
}
//revive:enable:cognitive-complexity
type cmdListReader struct {
sender *sender
cmdList []*commandData
length int
offset int
}
func newCmdListReader(sender *sender) *cmdListReader {
return &cmdListReader{
sender: sender,
cmdList: make([]*commandData, 128),
length: 0,
offset: 0,
}
}
func (c *cmdListReader) readIfExhausted() int {
if c.offset >= c.length {
c.length = c.sender.readSentCommands(c.cmdList)
c.offset = 0
}
return c.length
}
func (c *cmdListReader) current() *commandData {
return c.cmdList[c.offset]
}
func (c *cmdListReader) next() {
c.offset++
}