Skip to content

Commit

Permalink
feat: block-fetch BatchDone callback function support (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney authored Nov 14, 2024
1 parent a307899 commit f2064fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions protocol/blockfetch/blockfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type BlockFetch struct {

type Config struct {
BlockFunc BlockFunc
BatchDoneFunc BatchDoneFunc
RequestRangeFunc RequestRangeFunc
BatchStartTimeout time.Duration
BlockTimeout time.Duration
Expand All @@ -102,6 +103,7 @@ type CallbackContext struct {

// Callback function types
type BlockFunc func(CallbackContext, uint, ledger.Block) error
type BatchDoneFunc func(CallbackContext) error
type RequestRangeFunc func(CallbackContext, common.Point, common.Point) error

func New(protoOptions protocol.ProtocolOptions, cfg *Config) *BlockFetch {
Expand Down Expand Up @@ -132,6 +134,12 @@ func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
}
}

func WithBatchDoneFunc(batchDoneFunc BatchDoneFunc) BlockFetchOptionFunc {
return func(c *Config) {
c.BatchDoneFunc = batchDoneFunc
}
}

func WithRequestRangeFunc(
requestRangeFunc RequestRangeFunc,
) BlockFetchOptionFunc {
Expand Down
6 changes: 6 additions & 0 deletions protocol/blockfetch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func (c *Client) handleBatchDone() error {
"role", "client",
"connection_id", c.callbackContext.ConnectionId.String(),
)
// Notify the user if requested
if c.blockUseCallback && c.config.BatchDoneFunc != nil {
if err := c.config.BatchDoneFunc(c.callbackContext); err != nil {
return err
}
}
c.busyMutex.Unlock()
return nil
}

0 comments on commit f2064fa

Please sign in to comment.