utils.go 1.36 KB
Newer Older
1 2 3 4 5 6 7 8
package requestmanager

import (
	"context"

	"github.com/ipfs/go-graphsync/ipldbridge"
	gsmsg "github.com/ipfs/go-graphsync/message"
	"github.com/ipfs/go-graphsync/metadata"
9
	"github.com/ipfs/go-graphsync/requestmanager/types"
10 11 12
	ipld "github.com/ipld/go-ipld-prime"
)

13
func visitToChannel(ctx context.Context, inProgressChan chan types.ResponseProgress) ipldbridge.AdvVisitFn {
14 15 16
	return func(tp ipldbridge.TraversalProgress, node ipld.Node, tr ipldbridge.TraversalReason) error {
		select {
		case <-ctx.Done():
17
		case inProgressChan <- types.ResponseProgress{
18 19 20 21 22 23 24 25 26 27 28 29
			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 {
30 31 32 33 34 35
		mdRaw, err := response.Extension(gsmsg.ExtensionMetadata)
		if err != nil {
			log.Warningf("Unable to decode metadata in response for request id: %d", response.RequestID())
			continue
		}
		md, err := metadata.DecodeMetadata(mdRaw, ipldBridge)
36 37 38 39 40 41 42 43
		if err != nil {
			log.Warningf("Unable to decode metadata in response for request id: %d", response.RequestID())
			continue
		}
		responseMetadata[response.RequestID()] = md
	}
	return responseMetadata
}