mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-01-15 02:30:05 +08:00
add support for transient connections
This commit is contained in:
parent
410e6bdbd0
commit
7eee7c00a6
@ -13,9 +13,11 @@ var DialPeerTimeout = 60 * time.Second
|
|||||||
type noDialCtxKey struct{}
|
type noDialCtxKey struct{}
|
||||||
type dialPeerTimeoutCtxKey struct{}
|
type dialPeerTimeoutCtxKey struct{}
|
||||||
type forceDirectDialCtxKey struct{}
|
type forceDirectDialCtxKey struct{}
|
||||||
|
type useTransientCtxKey struct{}
|
||||||
|
|
||||||
var noDial = noDialCtxKey{}
|
var noDial = noDialCtxKey{}
|
||||||
var forceDirectDial = forceDirectDialCtxKey{}
|
var forceDirectDial = forceDirectDialCtxKey{}
|
||||||
|
var useTransient = useTransientCtxKey{}
|
||||||
|
|
||||||
// EXPERIMENTAL
|
// EXPERIMENTAL
|
||||||
// WithForceDirectDial constructs a new context with an option that instructs the network
|
// WithForceDirectDial constructs a new context with an option that instructs the network
|
||||||
@ -66,3 +68,18 @@ func GetDialPeerTimeout(ctx context.Context) time.Duration {
|
|||||||
func WithDialPeerTimeout(ctx context.Context, timeout time.Duration) context.Context {
|
func WithDialPeerTimeout(ctx context.Context, timeout time.Duration) context.Context {
|
||||||
return context.WithValue(ctx, dialPeerTimeoutCtxKey{}, timeout)
|
return context.WithValue(ctx, dialPeerTimeoutCtxKey{}, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithUseTransient constructs a new context with an option that instructs to network
|
||||||
|
// that it is acceptable to use a transient connection when opening a new stream.
|
||||||
|
func WithUseTransient(ctx context.Context) context.Context {
|
||||||
|
return context.WithValue(ctx, useTransient, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUseTransient returns true if the use transient option is set in the context.
|
||||||
|
func GetUseTransient(ctx context.Context) bool {
|
||||||
|
v := ctx.Value(useTransient)
|
||||||
|
if v != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -103,6 +103,8 @@ type Stat struct {
|
|||||||
Direction Direction
|
Direction Direction
|
||||||
// Opened is the timestamp when this connection was opened.
|
// Opened is the timestamp when this connection was opened.
|
||||||
Opened time.Time
|
Opened time.Time
|
||||||
|
// Transient indicates that this connection is transient and may be closed soon
|
||||||
|
Transient bool
|
||||||
// Extra stores additional metadata about this connection.
|
// Extra stores additional metadata about this connection.
|
||||||
Extra map[interface{}]interface{}
|
Extra map[interface{}]interface{}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user