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

add support for transient connections #175

Merged
merged 6 commits into from Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions network/conn.go
Expand Up @@ -19,6 +19,7 @@ type Conn interface {

ConnSecurity
ConnMultiaddrs
ConnStat

// ID returns an identifier that uniquely identifies this Conn within this
// host, during this run. Connection IDs may repeat across restarts.
Expand All @@ -29,9 +30,6 @@ type Conn interface {

// GetStreams returns all open streams over this conn.
GetStreams() []Stream

// Stat stores metadata pertaining to this conn.
Stat() Stat
}

// ConnSecurity is the interface that one can mix into a connection interface to
Expand Down Expand Up @@ -61,3 +59,9 @@ type ConnMultiaddrs interface {
// with this connection
RemoteMultiaddr() ma.Multiaddr
}

// ConnStat is an interface mixin for connection types that provide connection statistics.
type ConnStat interface {
vyzo marked this conversation as resolved.
Show resolved Hide resolved
// Stat stores metadata pertaining to this conn.
Stat() Stat
}
17 changes: 17 additions & 0 deletions network/context.go
Expand Up @@ -13,9 +13,11 @@ var DialPeerTimeout = 60 * time.Second
type noDialCtxKey struct{}
type dialPeerTimeoutCtxKey struct{}
type forceDirectDialCtxKey struct{}
type useTransientCtxKey struct{}

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

// EXPERIMENTAL
// WithForceDirectDial constructs a new context with an option that instructs the network
Expand Down Expand Up @@ -66,3 +68,18 @@ func GetDialPeerTimeout(ctx context.Context) time.Duration {
func WithDialPeerTimeout(ctx context.Context, timeout time.Duration) context.Context {
return context.WithValue(ctx, dialPeerTimeoutCtxKey{}, timeout)
}

// WithUseTransient constructs a new context with an option that instructs the network
// that it is acceptable to use a transient connection when opening a new stream.
func WithUseTransient(ctx context.Context, reason string) context.Context {
return context.WithValue(ctx, useTransient, reason)
}

// GetUseTransient returns true if the use transient option is set in the context.
func GetUseTransient(ctx context.Context) (usetransient bool, reason string) {
v := ctx.Value(useTransient)
if v != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return v != nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double sigh...

return true, v.(string)
}
return false, ""
}
4 changes: 4 additions & 0 deletions network/errors.go
Expand Up @@ -8,3 +8,7 @@ var ErrNoRemoteAddrs = errors.New("no remote addresses")
// ErrNoConn is returned when attempting to open a stream to a peer with the NoDial
// option and no usable connection is available.
var ErrNoConn = errors.New("no usable connection to peer")

// ErrTransientConn is returned when attempting to open a stream to a peer with only a transient
// connection, without specifying the UseTransient option.
var ErrTransientConn = errors.New("transient connection to peer")
2 changes: 2 additions & 0 deletions network/network.go
Expand Up @@ -103,6 +103,8 @@ type Stat struct {
Direction Direction
// Opened is the timestamp when this connection was opened.
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 map[interface{}]interface{}
}
Expand Down