2019-05-23 01:31:11 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DialPeerTimeout is the default timeout for a single call to `DialPeer`. When
|
|
|
|
// there are multiple concurrent calls to `DialPeer`, this timeout will apply to
|
|
|
|
// each independently.
|
|
|
|
var DialPeerTimeout = 60 * time.Second
|
|
|
|
|
|
|
|
type noDialCtxKey struct{}
|
|
|
|
type dialPeerTimeoutCtxKey struct{}
|
2021-02-15 15:50:04 +08:00
|
|
|
type forceDirectDialCtxKey struct{}
|
2019-05-23 01:31:11 +08:00
|
|
|
|
|
|
|
var noDial = noDialCtxKey{}
|
2021-02-15 15:50:04 +08:00
|
|
|
var forceDirectDial = forceDirectDialCtxKey{}
|
|
|
|
|
|
|
|
// EXPERIMENTAL
|
|
|
|
// WithForceDirectDial constructs a new context with an option that instructs the network
|
|
|
|
// to attempt to force a direct connection to a peer via a dial even if a proxied connection to it already exists.
|
|
|
|
func WithForceDirectDial(ctx context.Context, reason string) context.Context {
|
|
|
|
return context.WithValue(ctx, forceDirectDial, reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EXPERIMENTAL
|
|
|
|
// GetForceDirectDial returns true if the force direct dial option is set in the context.
|
|
|
|
func GetForceDirectDial(ctx context.Context) (forceDirect bool, reason string) {
|
|
|
|
v := ctx.Value(forceDirectDial)
|
|
|
|
if v != nil {
|
|
|
|
return true, v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, ""
|
|
|
|
}
|
2019-05-23 01:31:11 +08:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
return DialPeerTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialPeerTimeout returns a new context with the DialPeer timeout applied.
|
|
|
|
//
|
|
|
|
// This timeout overrides the default DialPeerTimeout and applies per-dial
|
|
|
|
// independently.
|
|
|
|
func WithDialPeerTimeout(ctx context.Context, timeout time.Duration) context.Context {
|
|
|
|
return context.WithValue(ctx, dialPeerTimeoutCtxKey{}, timeout)
|
|
|
|
}
|