Skip to content

Commit

Permalink
Use queue for authenticator because it's a lot clener.
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Littley <[email protected]>
  • Loading branch information
cody-littley committed Nov 25, 2024
1 parent 1db3a74 commit 9100eac
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions relay/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
pb "github.com/Layr-Labs/eigenda/api/grpc/relay"
"github.com/Layr-Labs/eigenda/common/queue"
"github.com/Layr-Labs/eigenda/core"
lru "github.com/hashicorp/golang-lru/v2"
"sync"
Expand Down Expand Up @@ -38,7 +39,7 @@ type requestAuthenticator struct {
authenticatedClients map[string]struct{}

// authenticationTimeouts is a list of authentications that have been performed, along with their expiration times.
authenticationTimeouts []*authenticationTimeout // TODO use a queue here why not
authenticationTimeouts queue.Queue[*authenticationTimeout]

// authenticationTimeoutDuration is the duration for which an auth is valid.
// If this is zero, then auth saving is disabled, and each request will be authenticated independently.
Expand Down Expand Up @@ -67,7 +68,7 @@ func NewRequestAuthenticator(
authenticator := &requestAuthenticator{
ics: ics,
authenticatedClients: make(map[string]struct{}),
authenticationTimeouts: make([]*authenticationTimeout, 0),
authenticationTimeouts: &queue.LinkedQueue[*authenticationTimeout]{},
authenticationTimeoutDuration: authenticationTimeoutDuration,
keyCache: keyCache,
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func (a *requestAuthenticator) saveAuthenticationResult(now time.Time, origin st
defer a.savedAuthLock.Unlock()

a.authenticatedClients[origin] = struct{}{}
a.authenticationTimeouts = append(a.authenticationTimeouts,
a.authenticationTimeouts.Push(
&authenticationTimeout{
origin: origin,
expiration: now.Add(a.authenticationTimeoutDuration),
Expand All @@ -195,14 +196,12 @@ func (a *requestAuthenticator) isAuthenticationStillValid(now time.Time, address
// removeOldAuthentications removes any authentications that have expired.
// This method is not thread safe and should be called with the savedAuthLock held.
func (a *requestAuthenticator) removeOldAuthentications(now time.Time) {
index := 0
for ; index < len(a.authenticationTimeouts); index++ {
if a.authenticationTimeouts[index].expiration.After(now) {
for a.authenticationTimeouts.Size() > 0 {
next, _ := a.authenticationTimeouts.Peek()
if next.expiration.After(now) {
break
}
delete(a.authenticatedClients, a.authenticationTimeouts[index].origin)
}
if index > 0 {
a.authenticationTimeouts = a.authenticationTimeouts[index:]
delete(a.authenticatedClients, next.origin)
a.authenticationTimeouts.Pop()
}
}

0 comments on commit 9100eac

Please sign in to comment.