Skip to content

Commit

Permalink
refactor: smarter channel sleeps, better byte handling, update crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Aug 19, 2023
1 parent 3689f12 commit 38a7c3f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 42 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
unit-test:
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 8
max-parallel: 10
matrix:
os:
- ubuntu-latest
Expand All @@ -19,19 +19,20 @@ jobs:
- "1.17"
- "1.18"
- "1.19"
- "1.20"
steps:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
fetch-depth: 1
- name: set up go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52
version: v1.54
args: --timeout 5m
- name: install gotestsum
run: go install gotest.tools/gotestsum@latest
Expand All @@ -45,7 +46,7 @@ jobs:
os:
- ubuntu-latest
go-version:
- "1.19"
- "1.20"
runtime:
- "docker"
needs:
Expand All @@ -54,7 +55,7 @@ jobs:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
fetch-depth: 1
- name: set up go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ issues:
text: "package-comments"

run:
go: '1.19'
go: '1.20'
skip-dirs:
- private

output:
uniq-by-line: false

service:
golangci-lint-version: 1.52.x
golangci-lint-version: 1.54.x
7 changes: 4 additions & 3 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ const (
// DefaultTimeoutOpsSeconds is the default time value for operations -- 60 seconds.
DefaultTimeoutOpsSeconds = 60
// DefaultReadDelayMicroSeconds is the default value for the delay between reads of the
// transport -- 100 microseconds. Going very low is likely to lead to very high cpu and not
// transport -- 250 microseconds. Going very low is likely to lead to very high cpu and not
// yield any recognizable gains, so be careful changing this!
DefaultReadDelayMicroSeconds = 250
// DefaultReturnChar is the character used to send an "enter" key to the device, "\n".
DefaultReturnChar = "\n"
// DefaultPromptSearchDepth -- is the default depth to search for the prompt in the received
// bytes.
DefaultPromptSearchDepth = 1_000
redacted = "redacted"
readDelayDivisor = 1_000

redacted = "redacted"
readDelayDivisor = 1_000
)

var (
Expand Down
34 changes: 11 additions & 23 deletions channel/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func (c *Channel) read() {
// afraid to remove it!
b = bytes.ReplaceAll(b, []byte("\r"), []byte(""))

// trim out all the space we padded in the buffer to read into
b = bytes.ReplaceAll(b, []byte("\x00"), []byte(""))

if bytes.Contains(b, []byte("\x1b")) {
b = util.StripANSI(b)
}
Expand All @@ -61,6 +58,7 @@ func (c *Channel) Read() ([]byte, error) {
case err := <-c.Errs:
return nil, err
default:
time.Sleep(c.ReadDelay)
}

b := c.Q.Dequeue()
Expand Down Expand Up @@ -117,15 +115,11 @@ func (c *Channel) ReadUntilPrompt() ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}
Expand All @@ -146,23 +140,21 @@ func (c *Channel) ReadUntilAnyPrompt(prompts []*regexp.Regexp) ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}

rb = append(rb, nb...)

prb := c.processReadBuf(rb)

for _, p := range prompts {
if p.Match(rb) {
if p.Match(prb) {
c.l.Debugf("channel read %#v", string(rb))

return rb, nil
Expand All @@ -177,22 +169,18 @@ func (c *Channel) ReadUntilExplicit(b []byte) ([]byte, error) {
var rb []byte

for {
select {
case err := <-c.Errs:
nb, err := c.Read()
if err != nil {
return nil, err
default:
time.Sleep(c.ReadDelay)
}

nb := c.Q.Dequeue()

if nb == nil {
continue
}

rb = append(rb, nb...)

if bytes.Contains(rb, b) {
if bytes.Contains(c.processReadBuf(rb), b) {
c.l.Debugf("channel read %#v", string(rb))

return rb, nil
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ require (
github.com/google/go-cmp v0.5.9
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4
golang.org/x/crypto v0.6.0
golang.org/x/term v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
7 changes: 5 additions & 2 deletions transport/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ func (t *Standard) IsAlive() bool {
func (t *Standard) Read(n int) ([]byte, error) {
b := make([]byte, n)

_, err := t.reader.Read(b)
n, err := t.reader.Read(b)
if err != nil {
return nil, err
}

return b, err
return b[0:n], nil
}

// Write writes bytes b to the transport.
Expand Down
7 changes: 5 additions & 2 deletions transport/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,12 @@ func (t *System) IsAlive() bool {
func (t *System) Read(n int) ([]byte, error) {
b := make([]byte, n)

_, err := t.fd.Read(b)
n, err := t.fd.Read(b)
if err != nil {
return nil, err
}

return b, err
return b[0:n], nil
}

// Write writes bytes b to the transport.
Expand Down

0 comments on commit 38a7c3f

Please sign in to comment.