Skip to content

Commit

Permalink
perf: Optimize the debounce part for output, changing the buffer type
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron3S authored and LeeEirc committed Jun 24, 2024
1 parent 34a89ad commit d77933c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/httpd/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpd

import (
"bytes"
"context"
"encoding/json"
"github.com/gliderlabs/ssh"
Expand All @@ -21,7 +22,7 @@ type Client struct {

sync.Mutex

buffer []byte
buffer bytes.Buffer
bufferMutex sync.Mutex
timer *time.Timer
}
Expand Down Expand Up @@ -49,7 +50,7 @@ func (c *Client) Write(p []byte) (n int, err error) {
c.bufferMutex.Lock()
defer c.bufferMutex.Unlock()

c.buffer = append(c.buffer, p...)
c.buffer.Write(p)

if c.timer == nil {
c.timer = time.AfterFunc(time.Millisecond, c.flushBuffer)
Expand All @@ -61,14 +62,14 @@ func (c *Client) flushBuffer() {
c.bufferMutex.Lock()
defer c.bufferMutex.Unlock()

if len(c.buffer) > 0 {
if c.buffer.Len() > 0 {
msg := Message{
Id: c.Conn.Uuid,
Type: TerminalBinary,
Raw: c.buffer,
Raw: c.buffer.Bytes(),
}
c.Conn.SendMessage(&msg)
c.buffer = nil
c.buffer.Reset()
}
c.timer.Stop()
c.timer = nil
Expand Down

0 comments on commit d77933c

Please sign in to comment.