Skip to content

Commit

Permalink
feat: add a random jitter to sasl session reauthentication deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
ertanden committed Nov 10, 2023
1 parent daa69a4 commit b416e11
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -1649,8 +1650,10 @@ func (c *Conn) saslAuthenticate(data []byte) ([]byte, error) {
errorCode = response.ErrorCode
authData = response.Data
if response.SessionLifetimeMs > 0 {
// set sasl session deadline to %90 of session lifetime
c.saslSessionDeadline = time.Now().Add(time.Duration(float64(response.SessionLifetimeMs)*0.9) * time.Millisecond)
// set sasl session deadline to a random %80-%90 of session lifetime
jitter := 0.10 * rand.New(rand.NewSource(time.Now().UnixNano())).Float64()
reducedLifetimeMs := (0.80 + jitter) * float64(response.SessionLifetimeMs)
c.saslSessionDeadline = time.Now().Add(time.Duration(reducedLifetimeMs) * time.Millisecond)
}
}

Expand Down
7 changes: 4 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,9 +1333,10 @@ func authenticateSASL(ctx context.Context, pc *protocol.Conn, mechanism sasl.Mec
}

if sessionLifetimeMs > 0 {
// set sasl session deadline to %90 of session lifetime
var saslSessionDeadline = time.Now().Add(time.Duration(float64(sessionLifetimeMs)*0.9) * time.Millisecond)
pc.SetSaslSessionDeadline(saslSessionDeadline)
// set sasl session deadline to a random %80-%90 of session lifetime
jitter := 0.10 * rand.New(rand.NewSource(time.Now().UnixNano())).Float64()
reducedLifetimeMs := (0.80 + jitter) * float64(sessionLifetimeMs)
pc.SetSaslSessionDeadline(time.Now().Add(time.Duration(reducedLifetimeMs) * time.Millisecond))
}
}

Expand Down

0 comments on commit b416e11

Please sign in to comment.