utils.go 1.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package requestmanager

import (
	"context"

	"github.com/ipfs/go-graphsync/ipldbridge"
	gsmsg "github.com/ipfs/go-graphsync/message"
	"github.com/ipfs/go-graphsync/metadata"
	ipld "github.com/ipld/go-ipld-prime"
)

func visitToChannel(ctx context.Context, inProgressChan chan ResponseProgress) ipldbridge.AdvVisitFn {
	return func(tp ipldbridge.TraversalProgress, node ipld.Node, tr ipldbridge.TraversalReason) error {
		select {
		case <-ctx.Done():
		case inProgressChan <- ResponseProgress{
			Node:      node,
			Path:      tp.Path,
			LastBlock: tp.LastBlock,
		}:
		}
		return nil
	}
}

func metadataForResponses(responses []gsmsg.GraphSyncResponse, ipldBridge ipldbridge.IPLDBridge) map[gsmsg.GraphSyncRequestID]metadata.Metadata {
	responseMetadata := make(map[gsmsg.GraphSyncRequestID]metadata.Metadata, len(responses))
	for _, response := range responses {
		md, err := metadata.DecodeMetadata(response.Extra(), ipldBridge)
		if err != nil {
			log.Warningf("Unable to decode metadata in response for request id: %d", response.RequestID())
			continue
		}
		responseMetadata[response.RequestID()] = md
	}
	return responseMetadata
}