Skip to content

Commit

Permalink
Made suggested changes.
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Littley <[email protected]>
  • Loading branch information
cody-littley committed Aug 1, 2024
1 parent af9e50f commit 1e8702a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tools/traffic/table/blob_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func NewBlobMetadata(
blobIndex uint,
readPermits int) *BlobMetadata {

if readPermits == 0 {
panic("readPermits must be greater than 0, or -1 for unlimited reads")
}

return &BlobMetadata{
key: key,
checksum: checksum,
Expand Down
23 changes: 14 additions & 9 deletions tools/traffic/table/blob_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type BlobTable struct {
size uint

// lock is used to synchronize access to the requiredReads.
lock sync.Mutex
lock sync.RWMutex
}

// NewBlobTable creates a new BlobTable instance.
Expand All @@ -30,16 +30,16 @@ func NewBlobTable() BlobTable {

// Size returns the total number of blobs currently tracked by the requiredReads.
func (table *BlobTable) Size() uint {
table.lock.Lock()
defer table.lock.Unlock()
table.lock.RLock()
defer table.lock.RUnlock()

return table.size
}

// Get returns the blob at the specified index. Returns nil if the index is out of bounds.
func (table *BlobTable) Get(index uint) *BlobMetadata {
table.lock.Lock()
defer table.lock.Unlock()
table.lock.RLock()
defer table.lock.RUnlock()

if index >= table.size {
return nil
Expand All @@ -64,8 +64,8 @@ func (table *BlobTable) Add(blob *BlobMetadata) {
table.size++
}

// AddOrReplace adds a blob to the requiredReads if there is capacity or replaces an existing blob at random
// if the requiredReads is full. This method is a no-op if maximumCapacity is 0.
// AddOrReplace is equivalent to Add if there is capacity, or replaces an existing blob at random
// if the is no remaining capacity. This method is a no-op if maximumCapacity is 0.
func (table *BlobTable) AddOrReplace(blob *BlobMetadata, maximumCapacity uint) {
if maximumCapacity == 0 {
return
Expand Down Expand Up @@ -96,8 +96,13 @@ func (table *BlobTable) AddOrReplace(blob *BlobMetadata, maximumCapacity uint) {
// reaches 0, the blob is removed from the requiredReads. Returns the blob metadata (if there is at least one blob
// in the table) and a boolean indicating whether the blob was removed from the table as a result of this operation.
func (table *BlobTable) GetRandom(decrement bool) (*BlobMetadata, bool) {
table.lock.Lock()
defer table.lock.Unlock()
if decrement {
table.lock.Lock()
defer table.lock.Unlock()
} else {
table.lock.RLock()
defer table.lock.RUnlock()
}

if table.size == 0 {
return nil, false
Expand Down

0 comments on commit 1e8702a

Please sign in to comment.