package network
import (
"context"
"time"
)
var DialPeerTimeout = 60 * time .Second
type noDialCtxKey struct {}
type dialPeerTimeoutCtxKey struct {}
type forceDirectDialCtxKey struct {}
type allowLimitedConnCtxKey struct {}
type simConnectCtxKey struct { isClient bool }
var noDial = noDialCtxKey {}
var forceDirectDial = forceDirectDialCtxKey {}
var allowLimitedConn = allowLimitedConnCtxKey {}
var simConnectIsServer = simConnectCtxKey {}
var simConnectIsClient = simConnectCtxKey {isClient : true }
func WithForceDirectDial (ctx context .Context , reason string ) context .Context {
return context .WithValue (ctx , forceDirectDial , reason )
}
func GetForceDirectDial (ctx context .Context ) (forceDirect bool , reason string ) {
v := ctx .Value (forceDirectDial )
if v != nil {
return true , v .(string )
}
return false , ""
}
func WithSimultaneousConnect (ctx context .Context , isClient bool , reason string ) context .Context {
if isClient {
return context .WithValue (ctx , simConnectIsClient , reason )
}
return context .WithValue (ctx , simConnectIsServer , reason )
}
func GetSimultaneousConnect (ctx context .Context ) (simconnect bool , isClient bool , reason string ) {
if v := ctx .Value (simConnectIsClient ); v != nil {
return true , true , v .(string )
}
if v := ctx .Value (simConnectIsServer ); v != nil {
return true , false , v .(string )
}
return false , false , ""
}
func WithNoDial (ctx context .Context , reason string ) context .Context {
return context .WithValue (ctx , noDial , reason )
}
func GetNoDial (ctx context .Context ) (nodial bool , reason string ) {
v := ctx .Value (noDial )
if v != nil {
return true , v .(string )
}
return false , ""
}
func GetDialPeerTimeout (ctx context .Context ) time .Duration {
if to , ok := ctx .Value (dialPeerTimeoutCtxKey {}).(time .Duration ); ok {
return to
}
return DialPeerTimeout
}
func WithDialPeerTimeout (ctx context .Context , timeout time .Duration ) context .Context {
return context .WithValue (ctx , dialPeerTimeoutCtxKey {}, timeout )
}
func WithAllowLimitedConn (ctx context .Context , reason string ) context .Context {
return context .WithValue (ctx , allowLimitedConn , reason )
}
func WithUseTransient (ctx context .Context , reason string ) context .Context {
return context .WithValue (ctx , allowLimitedConn , reason )
}
func GetAllowLimitedConn (ctx context .Context ) (usetransient bool , reason string ) {
v := ctx .Value (allowLimitedConn )
if v != nil {
return true , v .(string )
}
return false , ""
}
func GetUseTransient (ctx context .Context ) (usetransient bool , reason string ) {
v := ctx .Value (allowLimitedConn )
if v != nil {
return true , v .(string )
}
return false , ""
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .