Skip to content

Commit

Permalink
perf: update cmd input parse and use ring buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Jan 9, 2025
1 parent 0a1f722 commit 45f7cdd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/proxy/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ func (p *Parser) IsNeedParse() bool {
}

func (p *Parser) writeInputBuffer(b []byte) {
if b[0] == CtrlC {
p.cmdInputParser.buf.Reset()
return
}
if p.disableInputAsCmd {
return
}
Expand Down
57 changes: 50 additions & 7 deletions pkg/proxy/parsercmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proxy

import (
"bytes"
"strings"
"sync"

Expand All @@ -10,18 +9,64 @@ import (
"github.com/jumpserver/koko/pkg/logger"
)

type RingBuffer struct {
data []byte
size int
start int
end int
length int
}

func (rb *RingBuffer) Write(p []byte) {
n := len(p)
for i := 0; i < n; i++ {
rb.data[rb.end] = p[i]
rb.end = (rb.end + 1) % rb.size
if rb.length == rb.size {
// 覆盖旧数据,start 也要前移
rb.start = (rb.start + 1) % rb.size
} else {
rb.length++
}
}
}

func (rb *RingBuffer) Bytes() []byte {
p := make([]byte, rb.length)
for i := 0; i < rb.length; i++ {
p[i] = rb.data[(rb.start+i)%rb.size]
}
return p
}

func (rb *RingBuffer) Reset() {
rb.start = 0
rb.end = 0
rb.length = 0
for i := 0; i < rb.size; i++ {
rb.data[i] = 0
}
}

func NewRingBuffer(size int) *RingBuffer {
return &RingBuffer{
data: make([]byte, size),
size: size,
}
}

const maxBufSize = 1024 * 100

func NewCmdParser(sid, name string) *CmdParser {
parser := CmdParser{id: sid, name: name}
parser := CmdParser{id: sid, name: name, buf: NewRingBuffer(maxBufSize)}
return &parser
}

type CmdParser struct {
id string
name string

buf bytes.Buffer
buf *RingBuffer
lock sync.Mutex

ps1 string
Expand All @@ -30,10 +75,8 @@ type CmdParser struct {
func (cp *CmdParser) WriteData(p []byte) (int, error) {
cp.lock.Lock()
defer cp.lock.Unlock()
if cp.buf.Len() > maxBufSize {
return 0, nil
}
return cp.buf.Write(p)
cp.buf.Write(p)
return len(p), nil
}

func (cp *CmdParser) Close() error {
Expand Down

0 comments on commit 45f7cdd

Please sign in to comment.