Skip to content

Commit

Permalink
Add event logging to blockservce get methods
Browse files Browse the repository at this point in the history
Contextual logging for async methods

License: MIT
Signed-off-by: ForrestWeston <forrest@protocol.ai>
  • Loading branch information
frrist committed Feb 28, 2018
1 parent cbc584e commit 00d0582
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ func (s *blockService) AddBlocks(bs []blocks.Block) error {

// GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash).
func (s *blockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) {
func (s *blockService) GetBlock(ctx context.Context, c *cid.Cid) (blk blocks.Block, err error) {
eip := log.EventBegin(ctx, "BlockService.GetBlock", c)
defer func() {
eip.DoneWithErr(err)
}()
log.Debugf("BlockService GetBlock: '%s'", c)

var f exchange.Fetcher
Expand Down Expand Up @@ -224,12 +228,14 @@ func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f excha
// the returned channel.
// NB: No guarantees are made about order.
func (s *blockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
ctx = log.EventBeginInContext(ctx, "BlockService.GetBlocks")
return getBlocks(ctx, ks, s.blockstore, s.exchange)
}

func getBlocks(ctx context.Context, ks []*cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) <-chan blocks.Block {
out := make(chan blocks.Block)
go func() {
defer logging.MaybeFinishEvent(ctx)
defer close(out)
var misses []*cid.Cid
for _, c := range ks {
Expand Down Expand Up @@ -284,12 +290,17 @@ type Session struct {
}

// GetBlock gets a block in the context of a request session
func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) {
func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blk blocks.Block, err error) {
eip := log.EventBegin(ctx, "Session.GetBlock", c)
defer func() {
eip.DoneWithErr(err)
}()
return getBlock(ctx, c, s.bs, s.ses)
}

// GetBlocks gets blocks in the context of a request session
func (s *Session) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
ctx = log.EventBeginInContext(ctx, "Session.GetBlocks")
return getBlocks(ctx, ks, s.bs, s.ses)
}

Expand Down

0 comments on commit 00d0582

Please sign in to comment.