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

Commit

Permalink
use the resource manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 30, 2021
1 parent 2affefe commit 66e8d9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/libp2p/go-ws-transport
require (
github.com/gorilla/websocket v1.4.2
github.com/libp2p/go-conn-security-multistream v0.3.0
github.com/libp2p/go-libp2p-core v0.13.0
github.com/libp2p/go-libp2p-core v0.13.1-0.20211230092045-caacd9659af8
github.com/libp2p/go-libp2p-mplex v0.4.1
github.com/libp2p/go-libp2p-testing v0.5.0
github.com/libp2p/go-libp2p-transport-upgrader v0.6.0
github.com/libp2p/go-libp2p-transport-upgrader v0.6.1-0.20211230125144-e068fbba0778
github.com/multiformats/go-multiaddr v0.3.3
github.com/multiformats/go-multiaddr-fmt v0.1.0
)
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL
github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0=
github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-core v0.13.0 h1:IFG/s8dN6JN2OTrXX9eq2wNU/Zlz2KLdwZUp5FplgXI=
github.com/libp2p/go-libp2p-core v0.13.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-core v0.13.1-0.20211230092045-caacd9659af8 h1:YMGNLo9extN6iwBCaLbkoEyw1qtCXbtPmSZLHbxaYuo=
github.com/libp2p/go-libp2p-core v0.13.1-0.20211230092045-caacd9659af8/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc=
github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g=
github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k=
Expand All @@ -88,8 +88,8 @@ github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod
github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0=
github.com/libp2p/go-libp2p-testing v0.5.0 h1:bTjC29TTQ/ODq0ld3+0KLq3irdA5cAH3OMbRi0/QsvE=
github.com/libp2p/go-libp2p-testing v0.5.0/go.mod h1:QBk8fqIL1XNcno/l3/hhaIEn4aLRijpYOR+zVjjlh+A=
github.com/libp2p/go-libp2p-transport-upgrader v0.6.0 h1:GfMCU+2aGGEm1zW3UcOz6wYSn8tXQalFfVfcww99i5A=
github.com/libp2p/go-libp2p-transport-upgrader v0.6.0/go.mod h1:1e07y1ZSZdHo9HPbuU8IztM1Cj+DR5twgycb4pnRzRo=
github.com/libp2p/go-libp2p-transport-upgrader v0.6.1-0.20211230125144-e068fbba0778 h1:XGq4vqi97uxoiix5gCjNDj3bTmgIZIhv6R7YvPVYZMM=
github.com/libp2p/go-libp2p-transport-upgrader v0.6.1-0.20211230125144-e068fbba0778/go.mod h1:0ebY9quQ1UjdxvMa4sFYWIDPLRjH+bSAblgyVq2q+WE=
github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU=
github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU=
github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ=
Expand Down
21 changes: 19 additions & 2 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ func init() {

type Option func(*WebsocketTransport) error

func WithResourceManager(m network.ResourceManager) Option {
return func(t *WebsocketTransport) error {
t.rcmgr = m
return nil
}
}

// WebsocketTransport is the actual go-libp2p transport
type WebsocketTransport struct {
upgrader *tptu.Upgrader
rcmgr network.ResourceManager
}

var _ transport.Transport = (*WebsocketTransport)(nil)

func New(u *tptu.Upgrader, opts ...Option) (*WebsocketTransport, error) {
t := &WebsocketTransport{u}
t := &WebsocketTransport{upgrader: u}
for _, opt := range opts {
if err := opt(t); err != nil {
return nil, err
Expand All @@ -58,9 +66,18 @@ func (t *WebsocketTransport) Proxy() bool {
}

func (t *WebsocketTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.CapableConn, error) {
var connScope network.ConnectionManagementScope
if t.rcmgr != nil {
var err error
connScope, err = t.rcmgr.OpenConnection(network.DirOutbound, true)
if err != nil {
return nil, err
}
}
macon, err := t.maDial(ctx, raddr)
if err != nil {
connScope.Done()
return nil, err
}
return t.upgrader.Upgrade(ctx, t, macon, network.DirOutbound, p)
return t.upgrader.Upgrade(ctx, t, macon, network.DirOutbound, p, connScope)
}
2 changes: 1 addition & 1 deletion websocket_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *WebsocketTransport) Listen(a ma.Multiaddr) (transport.Listener, error)
if err != nil {
return nil, err
}
return t.upgrader.UpgradeListener(t, malist), nil
return t.upgrader.UpgradeListener(t, malist, t.rcmgr), nil
}

func (t *WebsocketTransport) wrapListener(l net.Listener, origin *url.URL) (*listener, error) {
Expand Down

0 comments on commit 66e8d9b

Please sign in to comment.