context.go 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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{}
15
type forceDirectDialCtxKey struct{}
vyzo's avatar
vyzo committed
16
type useTransientCtxKey struct{}
vyzo's avatar
vyzo committed
17
type simConnectCtxKey struct{}
18 19

var noDial = noDialCtxKey{}
20
var forceDirectDial = forceDirectDialCtxKey{}
vyzo's avatar
vyzo committed
21
var useTransient = useTransientCtxKey{}
vyzo's avatar
vyzo committed
22
var simConnect = simConnectCtxKey{}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

// 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, ""
}
41

vyzo's avatar
vyzo committed
42 43 44 45 46 47 48 49
// EXPERIMENTAL
// WithSimultaneousConnect constructs a new context with an option that instructs the transport
// to apply hole punching logic where applicable.
func WithSimultaneousConnect(ctx context.Context, reason string) context.Context {
	return context.WithValue(ctx, simConnect, reason)
}

// EXPERIMENTAL
vyzo's avatar
vyzo committed
50
// GetSimultaneousConnect returns true if the simultaneous connect option is set in the context.
vyzo's avatar
vyzo committed
51 52 53 54 55 56 57 58 59
func GetSimultaneousConnect(ctx context.Context) (simconnect bool, reason string) {
	v := ctx.Value(simConnect)
	if v != nil {
		return true, v.(string)
	}

	return false, ""
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
// 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)
}
vyzo's avatar
vyzo committed
91

vyzo's avatar
vyzo committed
92
// WithUseTransient constructs a new context with an option that instructs the network
vyzo's avatar
vyzo committed
93
// that it is acceptable to use a transient connection when opening a new stream.
94 95
func WithUseTransient(ctx context.Context, reason string) context.Context {
	return context.WithValue(ctx, useTransient, reason)
vyzo's avatar
vyzo committed
96 97 98
}

// GetUseTransient returns true if the use transient option is set in the context.
99
func GetUseTransient(ctx context.Context) (usetransient bool, reason string) {
vyzo's avatar
vyzo committed
100 101
	v := ctx.Value(useTransient)
	if v != nil {
102
		return true, v.(string)
vyzo's avatar
vyzo committed
103
	}
104
	return false, ""
vyzo's avatar
vyzo committed
105
}