@@ -95,13 +95,21 @@ func DisableReuseport() Option {
95
95
return nil
96
96
}
97
97
}
98
+
98
99
func WithConnectionTimeout (d time.Duration ) Option {
99
100
return func (tr * TcpTransport ) error {
100
101
tr .connectTimeout = d
101
102
return nil
102
103
}
103
104
}
104
105
106
+ func WithResourceManager (rcmgr network.ResourceManager ) Option {
107
+ return func (tr * TcpTransport ) error {
108
+ tr .rcmgr = rcmgr
109
+ return nil
110
+ }
111
+ }
112
+
105
113
// TcpTransport is the TCP transport.
106
114
type TcpTransport struct {
107
115
// Connection upgrader for upgrading insecure stream connections to
@@ -114,6 +122,8 @@ type TcpTransport struct {
114
122
// TCP connect timeout
115
123
connectTimeout time.Duration
116
124
125
+ rcmgr network.ResourceManager
126
+
117
127
reuse rtpt.Transport
118
128
}
119
129
@@ -159,6 +169,17 @@ func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Co
159
169
160
170
// Dial dials the peer at the remote address.
161
171
func (t * TcpTransport ) Dial (ctx context.Context , raddr ma.Multiaddr , p peer.ID ) (transport.CapableConn , error ) {
172
+ var connScope network.ConnectionScope
173
+ if t .rcmgr != nil {
174
+ var err error
175
+ connScope , err = t .rcmgr .OpenConnection (network .DirOutbound , true )
176
+ if err != nil {
177
+ return nil , err
178
+ }
179
+ if err := connScope .SetPeer (p ); err != nil {
180
+ return nil , err
181
+ }
182
+ }
162
183
conn , err := t .maDial (ctx , raddr )
163
184
if err != nil {
164
185
return nil , err
@@ -176,7 +197,7 @@ func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID)
176
197
if ok , isClient , _ := network .GetSimultaneousConnect (ctx ); ok && ! isClient {
177
198
direction = network .DirInbound
178
199
}
179
- return t .Upgrader .Upgrade (ctx , t , c , direction , p )
200
+ return t .Upgrader .Upgrade (ctx , t , c , direction , p , connScope )
180
201
}
181
202
182
203
// UseReuseport returns true if reuseport is enabled and available.
@@ -198,7 +219,7 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
198
219
return nil , err
199
220
}
200
221
list = newTracingListener (& tcpListener {list , 0 })
201
- return t .Upgrader .UpgradeListener (t , list ), nil
222
+ return t .Upgrader .UpgradeListener (t , list , t . rcmgr ), nil
202
223
}
203
224
204
225
// Protocols returns the list of terminal protocols this transport can dial.
0 commit comments