From 29aaf384cfe287faf1aa46dff0943b6d437c58c4 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 19 Feb 2021 12:15:05 +0200 Subject: [PATCH] context option for simultaneous connect --- network/context.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/network/context.go b/network/context.go index 0eed3cd..36a4af4 100644 --- a/network/context.go +++ b/network/context.go @@ -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 @@ -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 { -- GitLab