Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
context option for simultaneous connect
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Feb 19, 2021
1 parent 412dbb3 commit 29aaf38
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions network/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ type noDialCtxKey struct{}
type dialPeerTimeoutCtxKey struct{}
type forceDirectDialCtxKey struct{}
type useTransientCtxKey struct{}
type simConnectCtxKey struct{}

var noDial = noDialCtxKey{}
var forceDirectDial = forceDirectDialCtxKey{}
var useTransient = useTransientCtxKey{}
var simConnect = simConnectCtxKey{}

// EXPERIMENTAL
// WithForceDirectDial constructs a new context with an option that instructs the network
Expand All @@ -37,6 +39,24 @@ func GetForceDirectDial(ctx context.Context) (forceDirect bool, reason string) {
return false, ""
}

// 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
// GetSimultaneousConnect returns true if the simultaneous connect option is set in the context
func GetSimultaneousConnect(ctx context.Context) (simconnect bool, reason string) {
v := ctx.Value(simConnect)
if v != nil {
return true, v.(string)
}

return false, ""
}

// 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 {
Expand Down

0 comments on commit 29aaf38

Please sign in to comment.