From ec2ccf69d8b08abb03fa3bdb3e7e95ae1862d619 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Tue, 23 May 2023 17:58:14 -0400 Subject: Major Update/Refactor to Support IETF 02 Beginning of a release candidate for support for IETF 02 tag of the responsiveness spec. --- lgc/collection.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'lgc/collection.go') diff --git a/lgc/collection.go b/lgc/collection.go index 7560186..cd6a87d 100644 --- a/lgc/collection.go +++ b/lgc/collection.go @@ -16,6 +16,7 @@ package lgc import ( "fmt" + "math/rand" "sync" ) @@ -33,10 +34,10 @@ func (collection *LoadGeneratingConnectionCollection) Get(idx int) (*LoadGenerat collection.Lock.Unlock() return nil, fmt.Errorf("collection is unlocked") } + return collection.lockedGet(idx) +} - if idx > len(*collection.LGCs) { - return nil, fmt.Errorf("index too large") - } +func (collection *LoadGeneratingConnectionCollection) lockedGet(idx int) (*LoadGeneratingConnection, error) { return &(*collection.LGCs)[idx], nil } @@ -49,6 +50,22 @@ func (collection *LoadGeneratingConnectionCollection) Append(conn LoadGenerating return nil } -func (collection *LoadGeneratingConnectionCollection) Len() int { - return len(*collection.LGCs) +func (collection *LoadGeneratingConnectionCollection) Len() (int, error) { + if collection.Lock.TryLock() { + collection.Lock.Unlock() + return -1, fmt.Errorf("collection is unlocked") + } + return len(*collection.LGCs), nil +} + +func (collection *LoadGeneratingConnectionCollection) GetRandom() (*LoadGeneratingConnection, error) { + if collection.Lock.TryLock() { + collection.Lock.Unlock() + return nil, fmt.Errorf("collection is unlocked") + } + + idx := int(rand.Uint32()) + idx = idx % len(*collection.LGCs) + + return collection.lockedGet(idx) } -- cgit v1.2.3