Skip to content

Commit

Permalink
More renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Apr 24, 2024
1 parent 495cd64 commit a105d63
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 37 deletions.
28 changes: 24 additions & 4 deletions core/network/context.go
Expand Up @@ -13,12 +13,12 @@ var DialPeerTimeout = 60 * time.Second
type noDialCtxKey struct{}
type dialPeerTimeoutCtxKey struct{}
type forceDirectDialCtxKey struct{}
type useTransientCtxKey struct{}
type allowLimitedConnCtxKey struct{}
type simConnectCtxKey struct{ isClient bool }

var noDial = noDialCtxKey{}
var forceDirectDial = forceDirectDialCtxKey{}
var useTransient = useTransientCtxKey{}
var allowLimitedConn = allowLimitedConnCtxKey{}
var simConnectIsServer = simConnectCtxKey{}
var simConnectIsClient = simConnectCtxKey{isClient: true}

Expand Down Expand Up @@ -94,15 +94,35 @@ func WithDialPeerTimeout(ctx context.Context, timeout time.Duration) context.Con
return context.WithValue(ctx, dialPeerTimeoutCtxKey{}, timeout)
}

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

// 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.
//
// Deprecated: Use WithAllowLimitedConn instead.
func WithUseTransient(ctx context.Context, reason string) context.Context {
return context.WithValue(ctx, useTransient, reason)
return context.WithValue(ctx, allowLimitedConn, reason)
}

// GetAllowLimitedConn returns true if the allow limited conn option is set in the context.
func GetAllowLimitedConn(ctx context.Context) (usetransient bool, reason string) {
v := ctx.Value(allowLimitedConn)
if v != nil {
return true, v.(string)
}
return false, ""
}

// GetUseTransient returns true if the use transient option is set in the context.
//
// Deprecated: Use GetAllowLimitedConn instead.
func GetUseTransient(ctx context.Context) (usetransient bool, reason string) {
v := ctx.Value(useTransient)
v := ctx.Value(allowLimitedConn)
if v != nil {
return true, v.(string)
}
Expand Down
8 changes: 7 additions & 1 deletion core/network/errors.go
Expand Up @@ -22,7 +22,13 @@ 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")
//
// Deprecated: Use ErrLimitedConn instead.
var ErrTransientConn = ErrLimitedConn

// ErrLimitedConn is returned when attempting to open a stream to a peer with only a conn
// connection, without specifying the AllowLimitedConn option.
var ErrLimitedConn = errors.New("limited connection to peer")

// ErrResourceLimitExceeded is returned when attempting to perform an operation that would
// exceed system resource limits.
Expand Down
6 changes: 4 additions & 2 deletions core/network/network.go
Expand Up @@ -118,8 +118,10 @@ type Stats 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
// Limited indicates that this connection is Limited. It maybe limited by
// bytes or time. In practice, this is a connection formed over a circuit v2
// relay.
Limited bool
// Extra stores additional metadata about this connection.
Extra map[interface{}]interface{}
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/basic/basic_host.go
Expand Up @@ -723,10 +723,10 @@ func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)

forceDirect, _ := network.GetForceDirectDial(ctx)
canUseTransient, _ := network.GetUseTransient(ctx)
canUseLimitedConn, _ := network.GetAllowLimitedConn(ctx)
if !forceDirect {
connectedness := h.Network().Connectedness(pi.ID)
if connectedness == network.Connected || (canUseTransient && connectedness == network.Limited) {
if connectedness == network.Connected || (canUseLimitedConn && connectedness == network.Limited) {
return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/routed/routed.go
Expand Up @@ -48,10 +48,10 @@ func Wrap(h host.Host, r Routing) *RoutedHost {
func (rh *RoutedHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
// first, check if we're already connected unless force direct dial.
forceDirect, _ := network.GetForceDirectDial(ctx)
canUseTransient, _ := network.GetUseTransient(ctx)
canUseLimitedConn, _ := network.GetAllowLimitedConn(ctx)
if !forceDirect {
connectedness := rh.Network().Connectedness(pi.ID)
if connectedness == network.Connected || (canUseTransient && connectedness == network.Limited) {
if connectedness == network.Connected || (canUseLimitedConn && connectedness == network.Limited) {
return nil
}
}
Expand Down
40 changes: 20 additions & 20 deletions p2p/net/swarm/swarm.go
Expand Up @@ -344,7 +344,7 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
}
stat.Direction = dir
stat.Opened = time.Now()
isTransient := stat.Transient
isLimited := stat.Limited

// Wrap and register the connection.
c := &Conn{
Expand Down Expand Up @@ -400,7 +400,7 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
c.notifyLk.Lock()
s.conns.Unlock()

if !isTransient {
if !isLimited {
// Notify goroutines waiting for a direct connection
//
// Go routines interested in waiting for direct connection first acquire this lock
Expand Down Expand Up @@ -453,14 +453,14 @@ func (s *Swarm) StreamHandler() network.StreamHandler {

// NewStream creates a new stream on any available connection to peer, dialing
// if necessary.
// Use network.WithUseTransient to open a stream over a transient(relayed)
// Use network.WithAllowLimitedConn to open a stream over a limited(relayed)
// connection.
func (s *Swarm) NewStream(ctx context.Context, p peer.ID) (network.Stream, error) {
log.Debugf("[%s] opening stream to peer [%s]", s.local, p)

// Algorithm:
// 1. Find the best connection, otherwise, dial.
// 2. If the best connection is transient, wait for a direct conn via conn
// 2. If the best connection is limited, wait for a direct conn via conn
// reversal or hole punching.
// 3. Try opening a stream.
// 4. If the underlying connection is, in fact, closed, close the outer
Expand Down Expand Up @@ -489,8 +489,8 @@ func (s *Swarm) NewStream(ctx context.Context, p peer.ID) (network.Stream, error
}
}

useTransient, _ := network.GetUseTransient(ctx)
if !useTransient && c.Stat().Transient {
limitedAllowed, _ := network.GetAllowLimitedConn(ctx)
if !limitedAllowed && c.Stat().Limited {
var err error
c, err = s.waitForDirectConn(ctx, p)
if err != nil {
Expand All @@ -516,12 +516,12 @@ func (s *Swarm) waitForDirectConn(ctx context.Context, p peer.ID) (*Conn, error)
if c == nil {
s.directConnNotifs.Unlock()
return nil, network.ErrNoConn
} else if !c.Stat().Transient {
} else if !c.Stat().Limited {
s.directConnNotifs.Unlock()
return c, nil
}

// Wait for transient connection to upgrade to a direct connection either by
// Wait for limited connection to upgrade to a direct connection either by
// connection reversal or hole punching.
ch := make(chan struct{})
s.directConnNotifs.m[p] = append(s.directConnNotifs.m[p], ch)
Expand Down Expand Up @@ -553,8 +553,8 @@ func (s *Swarm) waitForDirectConn(ctx context.Context, p peer.ID) (*Conn, error)
if c == nil {
return nil, network.ErrNoConn
}
if c.Stat().Transient {
return nil, network.ErrTransientConn
if c.Stat().Limited {
return nil, network.ErrLimitedConn
}
return c, nil
}
Expand All @@ -575,11 +575,11 @@ func (s *Swarm) ConnsToPeer(p peer.ID) []network.Conn {
}

func isBetterConn(a, b *Conn) bool {
// If one is transient and not the other, prefer the non-transient connection.
aTransient := a.Stat().Transient
bTransient := b.Stat().Transient
if aTransient != bTransient {
return !aTransient
// If one is limited and not the other, prefer the unlimited connection.
aLimited := a.Stat().Limited
bLimited := b.Stat().Limited
if aLimited != bLimited {
return !aLimited
}

// If one is direct and not the other, prefer the direct connection.
Expand Down Expand Up @@ -630,7 +630,7 @@ func (s *Swarm) bestConnToPeer(p peer.ID) *Conn {

// bestAcceptableConnToPeer returns the best acceptable connection, considering the passed in ctx.
// If network.WithForceDirectDial is used, it only returns a direct connections, ignoring
// any transient (relayed) connections to the peer.
// any limited (relayed) connections to the peer.
func (s *Swarm) bestAcceptableConnToPeer(ctx context.Context, p peer.ID) *Conn {
conn := s.bestConnToPeer(p)

Expand Down Expand Up @@ -661,19 +661,19 @@ func (s *Swarm) Connectedness(p peer.ID) network.Connectedness {
// the underlying transport connection is closed first, so tracking changes to the connectedness
// state requires considering this recently closed connections impact on Connectedness.
func (s *Swarm) connectednessUnlocked(p peer.ID, considerClosed bool) network.Connectedness {
var haveTransient bool
var haveLimited bool
for _, c := range s.conns.m[p] {
if !considerClosed && c.IsClosed() {
// These will be garbage collected soon
continue
}
if c.Stat().Transient {
haveTransient = true
if c.Stat().Limited {
haveLimited = true
} else {
return network.Connected
}
}
if haveTransient {
if haveLimited {
return network.Limited
} else {
return network.NotConnected
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_conn.go
Expand Up @@ -193,7 +193,7 @@ func (c *Conn) Stat() network.ConnStats {

// NewStream returns a new Stream from this connection
func (c *Conn) NewStream(ctx context.Context) (network.Stream, error) {
if c.Stat().Transient {
if c.Stat().Limited {
if useTransient, _ := network.GetUseTransient(ctx); !useTransient {

Check failure on line 197 in p2p/net/swarm/swarm_conn.go

View workflow job for this annotation

GitHub Actions / go-check / All

network.GetUseTransient is deprecated: Use GetAllowLimitedConn instead. (SA1019)
return nil, network.ErrTransientConn

Check failure on line 198 in p2p/net/swarm/swarm_conn.go

View workflow job for this annotation

GitHub Actions / go-check / All

network.ErrTransientConn is deprecated: Use ErrLimitedConn instead. (SA1019)
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/circuitv2/client/dial.go
Expand Up @@ -179,7 +179,7 @@ func (c *Client) connect(s network.Stream, dest peer.AddrInfo) (*Conn, error) {
// relay connection and we mark the connection as transient.
var stat network.ConnStats
if limit := msg.GetLimit(); limit != nil {
stat.Transient = true
stat.Limited = true
stat.Extra = make(map[interface{}]interface{})
stat.Extra[StatLimitDuration] = time.Duration(limit.GetDuration()) * time.Second
stat.Extra[StatLimitData] = limit.GetData()
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/circuitv2/client/handlers.go
Expand Up @@ -67,7 +67,7 @@ func (c *Client) handleStreamV2(s network.Stream) {
// relay connection and we mark the connection as transient.
var stat network.ConnStats
if limit := msg.GetLimit(); limit != nil {
stat.Transient = true
stat.Limited = true
stat.Extra = make(map[interface{}]interface{})
stat.Extra[StatLimitDuration] = time.Duration(limit.GetDuration()) * time.Second
stat.Extra[StatLimitData] = limit.GetData()
Expand Down
6 changes: 3 additions & 3 deletions p2p/protocol/circuitv2/relay/relay_test.go
Expand Up @@ -154,7 +154,7 @@ func TestBasicRelay(t *testing.T) {
if len(conns) != 1 {
t.Fatalf("expected 1 connection, but got %d", len(conns))
}
if !conns[0].Stat().Transient {
if !conns[0].Stat().Limited {
t.Fatal("expected transient connection")
}

Expand Down Expand Up @@ -229,7 +229,7 @@ func TestRelayLimitTime(t *testing.T) {
if len(conns) != 1 {
t.Fatalf("expected 1 connection, but got %d", len(conns))
}
if !conns[0].Stat().Transient {
if !conns[0].Stat().Limited {
t.Fatal("expected transient connection")
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func TestRelayLimitData(t *testing.T) {
if len(conns) != 1 {
t.Fatalf("expected 1 connection, but got %d", len(conns))
}
if !conns[0].Stat().Transient {
if !conns[0].Stat().Limited {
t.Fatal("expected transient connection")
}

Expand Down

0 comments on commit a105d63

Please sign in to comment.