Skip to content

Commit

Permalink
refactor: smarter channel sleeps, better byte handling
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Aug 19, 2023
1 parent 3689f12 commit 866008f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 68 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
61 changes: 32 additions & 29 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 Expand Up @@ -136,39 +137,41 @@ func (c *Channel) Open() (reterr error) {

go c.read()

if !c.AuthBypass {
var b []byte
if c.AuthBypass {
c.l.Debug("auth bypass is enabled, skipping in channel auth check")

return nil
}

authData := c.t.InChannelAuthData()
var b []byte

switch authData.Type {
case transport.InChannelAuthSSH:
c.l.Debug("transport requests in channel ssh auth, starting...")
authData := c.t.InChannelAuthData()

b, err = c.AuthenticateSSH(
[]byte(authData.Password),
[]byte(authData.PrivateKeyPassPhrase),
)
if err != nil {
return err
}
case transport.InChannelAuthTelnet:
c.l.Debug("transport requests in channel telnet auth, starting...")
switch authData.Type {
case transport.InChannelAuthSSH:
c.l.Debug("transport requests in channel ssh auth, starting...")

b, err = c.AuthenticateTelnet([]byte(authData.User), []byte(authData.Password))
if err != nil {
return err
}
b, err = c.AuthenticateSSH(
[]byte(authData.Password),
[]byte(authData.PrivateKeyPassPhrase),
)
if err != nil {
return err
}
case transport.InChannelAuthTelnet:
c.l.Debug("transport requests in channel telnet auth, starting...")

if len(b) > 0 {
// requeue any buffer data we get during in channel authentication back onto the
// read buffer. mostly this should only be relevant for netconf where we need to
// read the server capabilities.
c.Q.Requeue(b)
b, err = c.AuthenticateTelnet([]byte(authData.User), []byte(authData.Password))
if err != nil {
return err
}
} else {
c.l.Debug("auth bypass is enabled, skipping in channel auth check")
}

if len(b) > 0 {
// requeue any buffer data we get during in channel authentication back onto the
// read buffer. mostly this should only be relevant for netconf where we need to
// read the server capabilities.
c.Q.Requeue(b)
}

return nil
Expand Down
39 changes: 16 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 Down Expand Up @@ -117,15 +114,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 @@ -137,6 +130,8 @@ func (c *Channel) ReadUntilPrompt() ([]byte, error) {

return rb, nil
}

time.Sleep(c.ReadDelay)
}
}

Expand All @@ -146,28 +141,28 @@ 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
}
}

time.Sleep(c.ReadDelay)
}
}

Expand All @@ -177,25 +172,23 @@ 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
}

time.Sleep(c.ReadDelay)
}
}
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 866008f

Please sign in to comment.