This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from libp2p/feat/no-dial-option
NoDial context option
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "" | ||
} |