-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cody Littley <[email protected]>
- Loading branch information
1 parent
3089bb0
commit 463f88f
Showing
4 changed files
with
112 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,81 @@ | ||
package traffic | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// BlobReader reads blobs from a disperser at a configured rate. | ||
type BlobReader struct { | ||
// The context for the generator. All work should cease when this context is cancelled. | ||
ctx *context.Context | ||
|
||
// Tracks the number of active goroutines within the generator. | ||
waitGroup *sync.WaitGroup | ||
|
||
// TODO this type should be refactored maybe | ||
generator *TrafficGenerator | ||
|
||
// table of blobs to read from. | ||
table *BlobTable | ||
} | ||
|
||
// NewBlobReader creates a new BlobReader instance. | ||
func NewBlobReader( | ||
ctx *context.Context, | ||
waitGroup *sync.WaitGroup, | ||
generator *TrafficGenerator, | ||
table *BlobTable) BlobReader { | ||
|
||
return BlobReader{ | ||
ctx: ctx, | ||
waitGroup: waitGroup, | ||
generator: generator, | ||
table: table, | ||
} | ||
} | ||
|
||
// Start begins a blob reader goroutine. | ||
func (reader *BlobReader) Start() { | ||
reader.waitGroup.Add(1) | ||
go func() { | ||
defer reader.waitGroup.Done() | ||
reader.run() | ||
}() | ||
} | ||
|
||
// run periodically performs reads on blobs. | ||
func (reader *BlobReader) run() { | ||
ticker := time.NewTicker(time.Second) // TODO setting | ||
for { | ||
select { | ||
case <-(*reader.ctx).Done(): | ||
return | ||
case <-ticker.C: | ||
reader.randomRead() | ||
} | ||
} | ||
} | ||
|
||
// randomRead reads a random blob. | ||
func (reader *BlobReader) randomRead() { | ||
|
||
metadata := reader.table.GetRandom(true) | ||
if metadata == nil { | ||
// There are no blobs to read, do nothing. | ||
return | ||
} | ||
|
||
// TODO convert this to a proper read | ||
data, err := reader.generator.DisperserClient.RetrieveBlob(*reader.ctx, *metadata.batchHeaderHash, metadata.blobIndex) | ||
if err != nil { | ||
fmt.Println("Error reading blob:", err) // TODO | ||
return | ||
} | ||
|
||
// TODO it would be nice to do some verification, perhaps just of the hash of the blob | ||
|
||
fmt.Println("Read blob:", data) // TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters