Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #88 from libp2p/fix/document-query-notifications
Browse files Browse the repository at this point in the history
docs(routing/query): document public query event interfaces
  • Loading branch information
Stebalien authored Dec 7, 2019
2 parents 54a9d30 + acfdb3e commit 0a54f30
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions routing/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)

// QueryEventType indicates the query event's type.
type QueryEventType int

// Number of events to buffer.
var QueryEventBufferSize = 16

const (
// Sending a query to a peer.
SendingQuery QueryEventType = iota
// Got a response from a peer.
PeerResponse
// Found a "closest" peer (not currently used).
FinalPeer
// Got an error when querying.
QueryError
// Found a provider.
Provider
// Found a value.
Value
// Adding a peer to the query.
AddingPeer
// Dialing a peer.
DialingPeer
)

// QueryEvent is emitted for every notable event that happens during a DHT query.
type QueryEvent struct {
ID peer.ID
Type QueryEventType
Expand Down Expand Up @@ -67,13 +77,21 @@ func (e *eventChannel) send(ctx context.Context, ev *QueryEvent) {
e.mu.Unlock()
}

// RegisterForQueryEvents registers a query event channel with the given
// context. The returned context can be passed to DHT queries to receive query
// events on the returned channels.
//
// The passed context MUST be canceled when the caller is no longer interested
// in query events.
func RegisterForQueryEvents(ctx context.Context) (context.Context, <-chan *QueryEvent) {
ch := make(chan *QueryEvent, QueryEventBufferSize)
ech := &eventChannel{ch: ch, ctx: ctx}
go ech.waitThenClose()
return context.WithValue(ctx, routingQueryKey{}, ech), ch
}

// PublishQueryEvent publishes a query event to the query event channel
// associated with the given context, if any.
func PublishQueryEvent(ctx context.Context, ev *QueryEvent) {
ich := ctx.Value(routingQueryKey{})
if ich == nil {
Expand Down

0 comments on commit 0a54f30

Please sign in to comment.