Skip to content

Commit d43c256

Browse files
committed
If buffer size if too large, send immediately
This patch enhance some details to handle the console buffer considering the performance and error handling. partial: xcat2#14
1 parent 6d4d4a6 commit d43c256

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

console/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (c *ConsoleClient) input(args ...interface{}) {
8181
}
8282
exit, pos := c.checkEscape(b, n, node)
8383
if exit == true {
84-
b = []byte(ExitSequence)
84+
b = EXIT_SEQUENCE[0:]
8585
n = len(b)
8686
c.retry = false
8787
printConsoleDisconnectPrompt()

console/console.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package console
22

33
import (
4+
"bytes"
45
"fmt"
56
"github.com/chenglch/goconserver/common"
67
pl "github.com/chenglch/goconserver/console/pipeline"
@@ -12,15 +13,15 @@ import (
1213
)
1314

1415
const (
15-
ExitSequence = "\x05c." // ctrl-e, c
1616
CLIENT_CMD_EXIT = '.'
1717
CLIENT_CMD_HELP = '?'
1818
CLIENT_CMD_REPLAY = 'r'
1919
CLIENT_CMD_WHO = 'w'
2020
)
2121

2222
var (
23-
CLIENT_CMDS = []byte{CLIENT_CMD_HELP, CLIENT_CMD_REPLAY, CLIENT_CMD_WHO}
23+
CLIENT_CMDS = []byte{CLIENT_CMD_HELP, CLIENT_CMD_REPLAY, CLIENT_CMD_WHO}
24+
EXIT_SEQUENCE = [...]byte{'\x05', 'c', '.'} // ctrl-e, c
2425
)
2526

2627
type Console struct {
@@ -97,7 +98,7 @@ func (self *Console) writeTarget(conn net.Conn) {
9798
plog.WarningNode(self.node.StorageNode.Name, fmt.Sprintf("Failed to receive message from client. Error:%s.", err.Error()))
9899
return
99100
}
100-
if string(b) == ExitSequence {
101+
if bytes.Equal(b, EXIT_SEQUENCE[0:]) {
101102
plog.InfoNode(self.node.StorageNode.Name, "Received exit signal from client")
102103
return
103104
}

console/pipeline/logger.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ func (self *LineLogger) MakeRecord(node string, b []byte, last *RemainBuffer) er
8080
self.bufChan <- append(buf, []byte{'\n'}...)
8181
}
8282
}
83+
if len(b[p:]) >= 4096 {
84+
lineBuf := NewLineBuf(CONSOLE_TYPE, string(b[p:]), node, serverConfig.Console.LogTimestamp)
85+
buf, err = lineBuf.Marshal()
86+
if err != nil {
87+
plog.ErrorNode(node, err)
88+
}
89+
self.bufChan <- append(buf, []byte{'\n'}...)
90+
p = len(b)
91+
}
8392
copyLast(last, b[p:])
8493
return nil
8594
}
@@ -139,19 +148,25 @@ func (self *LineLogger) Prompt(node string, message string) error {
139148
}
140149

141150
func (self *LineLogger) run() {
142-
var buf bytes.Buffer
151+
buf := new(bytes.Buffer)
143152
plog.Debug("Starting line logger")
144153
ticker := time.NewTicker(common.PIPELINE_SEND_INTERVAL)
145154
// timeout may block data
146155
for {
147156
select {
148157
case b := <-self.bufChan:
149158
buf.Write(b)
159+
if buf.Len() > 8192 {
160+
// too large, send immediately
161+
self.emit(buf.Bytes())
162+
// slice in channel is transferred by reference
163+
buf = new(bytes.Buffer)
164+
}
150165
case <-ticker.C:
151166
if buf.Len() > 0 {
152167
// send buf to the channel
153168
self.emit(buf.Bytes())
154-
buf.Reset()
169+
buf = new(bytes.Buffer)
155170
}
156171
}
157172
}

0 commit comments

Comments
 (0)