Skip to content

Commit daed6e0

Browse files
authored
Merge pull request #37 from IceFireDB/fix/performance-regression
Fix/performance regression
2 parents 19b600d + 30c8f7e commit daed6e0

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

redhub.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,12 @@ func (rs *RedHub) OnClose(c gnet.Conn, err error) (action gnet.Action) {
102102

103103
// OnTraffic fires when a socket receives data from the remote
104104
func (rs *RedHub) OnTraffic(c gnet.Conn) (action gnet.Action) {
105-
var out []byte
106105
rs.connSync.RLock()
107106
cb, ok := rs.redHubBufMap[c]
108107
rs.connSync.RUnlock()
109108

110109
if !ok {
111-
c.AsyncWrite(resp.AppendError(nil, "ERR Client is closed"), nil)
110+
_, _ = c.Write(resp.AppendError(nil, "ERR Client is closed"))
112111
return gnet.None
113112
}
114113

@@ -120,28 +119,32 @@ func (rs *RedHub) OnTraffic(c gnet.Conn) (action gnet.Action) {
120119
cb.buf.Write(buf)
121120
cmds, lastbyte, err := resp.ReadCommands(cb.buf.Bytes())
122121
if err != nil {
123-
c.AsyncWrite(resp.AppendError(nil, "ERR "+err.Error()), nil)
122+
_, _ = c.Write(resp.AppendError(nil, "ERR "+err.Error()))
124123
return gnet.None
125124
}
126125

127126
cb.command = append(cb.command, cmds...)
128127
cb.buf.Reset()
129128

130129
if len(lastbyte) == 0 {
130+
var out []byte
131131
for len(cb.command) > 0 {
132132
cmd := cb.command[0]
133133
cb.command = cb.command[1:]
134134

135135
var status Action
136-
result, status := rs.handler(cmd, out)
137-
if len(result) > 0 {
138-
c.AsyncWrite(result, nil)
139-
}
136+
out, status = rs.handler(cmd, out)
140137

141138
if status == Close {
139+
if len(out) > 0 {
140+
_, _ = c.Write(out)
141+
}
142142
return gnet.Close
143143
}
144144
}
145+
if len(out) > 0 {
146+
_, _ = c.Write(out)
147+
}
145148
} else {
146149
cb.buf.Write(lastbyte)
147150
}

redhub_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ func (m *mockConn) Write(buf []byte) (n int, err error) {
2424
return len(buf), nil
2525
}
2626

27+
func (m *mockConn) Writev(bufs [][]byte) (n int, err error) {
28+
for _, buf := range bufs {
29+
m.written = append(m.written, buf...)
30+
n += len(buf)
31+
}
32+
return n, nil
33+
}
34+
2735
func (m *mockConn) Close() error {
2836
m.closed = true
2937
return nil

0 commit comments

Comments
 (0)