peerresponsemanager.go 932 Bytes
Newer Older
1 2 3 4 5
package peerresponsemanager

import (
	"context"

6
	"github.com/ipfs/go-graphsync/peermanager"
7 8 9
	peer "github.com/libp2p/go-libp2p-peer"
)

10 11
// PeerSenderFactory provides a function that will create a PeerResponseSender.
type PeerSenderFactory func(ctx context.Context, p peer.ID) PeerResponseSender
12

13 14 15
// PeerReponseManager manages message queues for peers
type PeerReponseManager struct {
	*peermanager.PeerManager
16 17
}

18 19 20 21 22 23
// New generates a new peer manager for sending responses
func New(ctx context.Context, createPeerSender PeerSenderFactory) *PeerReponseManager {
	return &PeerReponseManager{
		PeerManager: peermanager.New(ctx, func(ctx context.Context, p peer.ID) peermanager.PeerProcess {
			return createPeerSender(ctx, p)
		}),
24 25 26
	}
}

27 28 29
// SenderForPeer returns a response sender to use with the given peer
func (prm *PeerReponseManager) SenderForPeer(p peer.ID) PeerResponseSender {
	return prm.GetProcess(p).(PeerResponseSender)
30
}