1
0
mirror of https://github.com/libp2p/go-libp2p-core.git synced 2025-04-09 15:00:11 +08:00

reorg network context options.

This commit is contained in:
Raúl Kripalani 2019-04-17 16:04:54 +01:00
parent c9a30b9fbe
commit 2e11bcafba
3 changed files with 19 additions and 23 deletions

View File

@ -10,8 +10,27 @@ import (
// each independently.
var DialPeerTimeout = 60 * time.Second
type noDialCtxKey struct{}
type dialPeerTimeoutCtxKey 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.Context {
return 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, ""
}
// GetDialPeerTimeout returns the current DialPeer timeout (or the default).
func GetDialPeerTimeout(ctx context.Context) time.Duration {
if to, ok := ctx.Value(dialPeerTimeoutCtxKey{}).(time.Duration); ok {

View File

@ -1,23 +0,0 @@
package network
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.Context {
return 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, ""
}