Unverified Commit 7ada4e50 authored by Raúl Kripalani's avatar Raúl Kripalani Committed by GitHub

`ID()` method on connections and streams + record opening time (#224)

Co-authored-by: default avatarAarsh Shah <aarshkshah1992@gmail.com>
Co-authored-by: default avatarRaúl Kripalani <raul@protocol.ai>
parent 4e44cfc0
This diff is collapsed.
......@@ -54,6 +54,9 @@ type Swarm struct {
local peer.ID
peers peerstore.Peerstore
nextConnID uint32 // guarded by atomic
nextStreamID uint32 // guarded by atomic
conns struct {
sync.RWMutex
m map[peer.ID][]*Conn
......@@ -197,11 +200,13 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
}
}
stat := network.Stat{Direction: dir}
// Wrap and register the connection.
stat := network.Stat{Direction: dir, Opened: time.Now()}
c := &Conn{
conn: tc,
swarm: s,
stat: stat,
id: atomic.AddUint32(&s.nextConnID, 1),
}
// we ONLY check upgraded connections here so we can send them a Disconnect message.
......@@ -234,7 +239,6 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
return nil, ErrSwarmClosed
}
// Wrap and register the connection.
c.streams.m = make(map[*Stream]struct{})
s.conns.m[p] = append(s.conns.m[p], c)
......
......@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/mux"
......@@ -22,6 +24,7 @@ var ErrConnClosed = errors.New("connection closed")
// Conn is the connection type used by swarm. In general, you won't use this
// type directly.
type Conn struct {
id uint32
conn transport.CapableConn
swarm *Swarm
......@@ -38,6 +41,11 @@ type Conn struct {
stat network.Stat
}
func (c *Conn) ID() string {
// format: <first 10 chars of peer id>-<global conn ordinal>
return fmt.Sprintf("%s-%d", c.RemotePeer().Pretty()[0:10], c.id)
}
// Close closes this connection.
//
// Note: This method won't wait for the close notifications to finish as that
......@@ -169,6 +177,7 @@ func (c *Conn) Stat() network.Stat {
// NewStream returns a new Stream from this connection
func (c *Conn) NewStream() (network.Stream, error) {
ts, err := c.conn.OpenStream()
if err != nil {
return nil, err
}
......@@ -185,11 +194,15 @@ func (c *Conn) addStream(ts mux.MuxedStream, dir network.Direction) (*Stream, er
}
// Wrap and register the stream.
stat := network.Stat{Direction: dir}
stat := network.Stat{
Direction: dir,
Opened: time.Now(),
}
s := &Stream{
stream: ts,
conn: c,
stat: stat,
id: atomic.AddUint32(&c.swarm.nextStreamID, 1),
}
c.streams.m[s] = struct{}{}
......
......@@ -28,6 +28,8 @@ var _ network.Stream = &Stream{}
// Stream is the stream type used by swarm. In general, you won't use this type
// directly.
type Stream struct {
id uint32
stream mux.MuxedStream
conn *Conn
......@@ -43,6 +45,11 @@ type Stream struct {
stat network.Stat
}
func (s *Stream) ID() string {
// format: <first 10 chars of peer id>-<global conn ordinal>-<global stream ordinal>
return fmt.Sprintf("%s-%d", s.conn.ID(), s.id)
}
func (s *Stream) String() string {
return fmt.Sprintf(
"<swarm.Stream[%s] %s (%s) <-> %s (%s)>",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment