Skip to content

Commit

Permalink
Merge pull request goburrow#36 from grid-x/wz2b-master
Browse files Browse the repository at this point in the history
fix: flush read buffer before writing request
  • Loading branch information
guelfey authored Feb 3, 2021
2 parents ab35ea4 + fd3dfbb commit bb72151
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ func (mb *tcpTransporter) Send(aduRequest []byte) (aduResponse []byte, err error
return
}

// If an answer to a previously timed-out request is already in teh buffer, this will result
// in a transaction ID mismatch from which we will never recover. To prevent this, just
// flush any previous reponses before launching the next poll. That's throwing away
// possibly useful data, but the previous request was already satisfied with a timeout
// error so that probably makes the most sense here.

// Be aware that this call resets the read deadline.
mb.flushAll()

// Set timer to close when idle
mb.lastActivity = time.Now()
mb.startCloseTimer()
Expand Down Expand Up @@ -332,3 +341,27 @@ func (mb *tcpTransporter) closeIdle() {
mb.close()
}
}

// flushAll implements a non-blocking read flush. Be warned it resets
// the read deadline.
func (mb *tcpTransporter) flushAll() (int, error) {
if err := mb.conn.SetReadDeadline(time.Now()); err != nil {
return 0, err
}

count := 0
buffer := make([]byte, 1024)

for {
n, err := mb.conn.Read(buffer)

if err != nil {
return count + n, err
} else if n > 0 {
count = count + n
} else {
// didn't flush any new bytes, return
return count, err
}
}
}

0 comments on commit bb72151

Please sign in to comment.