mirror of
https://github.com/soypat/lneto.git
synced 2026-09-10 08:39:30 +00:00
Tcp buffer bug fix (#79)
* start working on tracking down tcp buffer bug * mtu refactor * modularize test * more precise testing * tests fail, but is it the failure we are looking for? * fix typo in espradio link (#76) * implement a new backoff abstraction (#75) * rewrite backoff api * rewrite tcp.Conn.Write * keep fixing small things * much better Conn.Read implementation * fix critical overflow bug in internal.ConnRWBackoff --------- Co-authored-by: Joel Wetzell <jwetzell@yahoo.com>
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package lneto
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Flag return values for a BackoffStrategy.
|
||||
const (
|
||||
BackoffFlagGosched = time.Duration(-1)
|
||||
BackoffFlagNop = time.Duration(-2)
|
||||
)
|
||||
|
||||
// BackoffStrategy is the abstraction of a backoff strategy for retrying an operation.
|
||||
// It returns the amount of time to sleep for or a flag value:
|
||||
// - Returns [BackoffFlagNop]: Signal no yielding function should be called.
|
||||
// Useful for when the BackoffStrategy implements its own yield.
|
||||
// - Returns [BackoffFlagGosched]: Signal [runtime.Gosched] should be called.
|
||||
//
|
||||
// consecutiveBackoffs starts at 1 and increments by 1 every time the operation is retried.
|
||||
// See internal/backoff.go for a implementation example.
|
||||
type BackoffStrategy func(consecutiveBackoffs int) (sleepOrFlag time.Duration)
|
||||
|
||||
// Do applies the backoff strategy by calling backoff(consecutiveBackoffs)
|
||||
// and then the corresponding yield function for the returned value. See [BackoffStrategy].
|
||||
func (backoff BackoffStrategy) Do(consecutiveBackoffs int) {
|
||||
sleep := backoff(consecutiveBackoffs)
|
||||
switch sleep {
|
||||
case BackoffFlagNop:
|
||||
// No yield. Yield implemented by backoff.
|
||||
case BackoffFlagGosched:
|
||||
runtime.Gosched()
|
||||
default:
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user