Skip to content

Commit

Permalink
Use gods library for data structures.
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 26, 2024
1 parent 71a5aaa commit f8327df
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 143 deletions.
56 changes: 0 additions & 56 deletions common/queue/linked_queue.go

This file was deleted.

58 changes: 0 additions & 58 deletions common/queue/linked_queue_test.go

This file was deleted.

18 changes: 0 additions & 18 deletions common/queue/queue.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.12
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.6
github.com/consensys/gnark-crypto v0.12.1
github.com/emirpasic/gods v1.18.1
github.com/ethereum/go-ethereum v1.14.8
github.com/fxamacker/cbor/v2 v2.5.0
github.com/gin-contrib/logger v0.2.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-ethereum v1.14.8 h1:NgOWvXS+lauK+zFukEvi85UmmsS/OkV0N23UZ1VTIig=
Expand Down
14 changes: 8 additions & 6 deletions relay/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ 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"
"github.com/emirpasic/gods/queues"
"github.com/emirpasic/gods/queues/linkedlistqueue"
lru "github.com/hashicorp/golang-lru/v2"
"sync"
"time"
Expand Down Expand Up @@ -39,7 +40,7 @@ type requestAuthenticator struct {
authenticatedClients map[string]struct{}

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

// 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 @@ -68,7 +69,7 @@ func NewRequestAuthenticator(
authenticator := &requestAuthenticator{
ics: ics,
authenticatedClients: make(map[string]struct{}),
authenticationTimeouts: &queue.LinkedQueue[*authenticationTimeout]{},
authenticationTimeouts: linkedlistqueue.New(),
authenticationTimeoutDuration: authenticationTimeoutDuration,
keyCache: keyCache,
}
Expand Down Expand Up @@ -171,7 +172,7 @@ func (a *requestAuthenticator) saveAuthenticationResult(now time.Time, origin st
defer a.savedAuthLock.Unlock()

a.authenticatedClients[origin] = struct{}{}
a.authenticationTimeouts.Push(
a.authenticationTimeouts.Enqueue(
&authenticationTimeout{
origin: origin,
expiration: now.Add(a.authenticationTimeoutDuration),
Expand All @@ -197,11 +198,12 @@ func (a *requestAuthenticator) isAuthenticationStillValid(now time.Time, address
// This method is not thread safe and should be called with the savedAuthLock held.
func (a *requestAuthenticator) removeOldAuthentications(now time.Time) {
for a.authenticationTimeouts.Size() > 0 {
next, _ := a.authenticationTimeouts.Peek()
val, _ := a.authenticationTimeouts.Peek()
next := val.(*authenticationTimeout)
if next.expiration.After(now) {
break
}
delete(a.authenticatedClients, next.origin)
a.authenticationTimeouts.Pop()
a.authenticationTimeouts.Dequeue()
}
}
12 changes: 7 additions & 5 deletions relay/cache/fifo-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package cache

import (
"errors"
"github.com/Layr-Labs/eigenda/common/queue"
"github.com/emirpasic/gods/queues"
"github.com/emirpasic/gods/queues/linkedlistqueue"
)

var _ Cache[string, string] = &FIFOCache[string, string]{}
Expand All @@ -15,7 +16,7 @@ type FIFOCache[K comparable, V any] struct {
currentWeight uint64
maxWeight uint64
data map[K]V
expirationQueue queue.Queue[K]
expirationQueue queues.Queue
}

// NewFIFOCache creates a new FIFOCache.
Expand All @@ -28,7 +29,7 @@ func NewFIFOCache[K comparable, V any](maxWeight uint64) *FIFOCache[K, V] {
maxWeight: maxWeight,
data: make(map[K]V),
weightCalculator: defaultWeightCalculator,
expirationQueue: &queue.LinkedQueue[K]{},
expirationQueue: linkedlistqueue.New(),
}
}

Expand All @@ -51,7 +52,7 @@ func (f *FIFOCache[K, V]) Put(key K, value V) {
oldWeight := f.weightCalculator(key, old)
f.currentWeight -= oldWeight
} else {
f.expirationQueue.Push(key)
f.expirationQueue.Enqueue(key)
}

if f.currentWeight < f.maxWeight {
Expand All @@ -60,7 +61,8 @@ func (f *FIFOCache[K, V]) Put(key K, value V) {
}

for f.currentWeight > f.maxWeight {
keyToEvict, _ := f.expirationQueue.Pop()
val, _ := f.expirationQueue.Dequeue()
keyToEvict := val.(K)
weightToEvict := f.weightCalculator(keyToEvict, f.data[keyToEvict])
delete(f.data, keyToEvict)
f.currentWeight -= weightToEvict
Expand Down

0 comments on commit f8327df

Please sign in to comment.