Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #42 from libp2p/feat/no-dial-option
Browse files Browse the repository at this point in the history
NoDial context option
  • Loading branch information
vyzo authored Apr 10, 2019
2 parents 2f01012 + 4331ed2 commit 673b626
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ type Network interface {
// There are no addresses associated with a peer when they were needed.
var ErrNoRemoteAddrs = errors.New("no remote addresses")

// ErrNoConn is returned when attempting to open a stream to a peer with the NoDial
// option and no usable connection is available.
var ErrNoConn = errors.New("no usable connection to peer")

// Dialer represents a service that can dial out to peers
// (this is usually just a Network, but other services may not need the whole
// stack, and thus it becomes easier to mock)
Expand Down
25 changes: 25 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net

import (
"context"
)

type noDialCtxKey struct{}

var noDial = noDialCtxKey{}

// WithNoDial constructs a new context with an option that instructs the network
// to not attempt a new dial when opening a stream.
func WithNoDial(ctx context.Context, reason string) {
context.WithValue(ctx, noDial, reason)
}

// GetNoDial returns true if the no dial option is set in the context.
func GetNoDial(ctx context.Context) (nodial bool, reason string) {
v := ctx.Value(noDial)
if v != nil {
return true, v.(string)
}

return false, ""
}

0 comments on commit 673b626

Please sign in to comment.