From df5973f3799d90fc86f566628f88dc5b58c52d9f Mon Sep 17 00:00:00 2001 From: muXxer Date: Mon, 25 Mar 2024 14:02:19 +0100 Subject: [PATCH] Do not wait for known peers --- components/restapi/management/peers.go | 18 +++++++++++++----- go.mod | 2 +- go.sum | 4 ++-- tools/gendoc/go.mod | 2 +- tools/gendoc/go.sum | 4 ++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/components/restapi/management/peers.go b/components/restapi/management/peers.go index e01562118..c1f9cbbcd 100644 --- a/components/restapi/management/peers.go +++ b/components/restapi/management/peers.go @@ -129,28 +129,36 @@ func addPeer(c echo.Context) (*api.PeerInfo, error) { return nil, ierrors.WithMessagef(httpserver.ErrInvalidParameter, "invalid address info from multiAddress (%s): %w", request.MultiAddress, err) } + // we don't need to add the peer if it is already marked as a manual peer if deps.NetworkManager.ManualNeighborExists(addrInfo.ID) { return nil, ierrors.WithMessagef(echo.ErrBadRequest, "manual peer already exists, peerID: %s", addrInfo.ID.String()) } - ctx, cancel := context.WithTimeout(c.Request().Context(), 5*time.Second) - defer cancel() + connectedCtx, connectedCtxCancel := context.WithTimeout(c.Request().Context(), 5*time.Second) + defer connectedCtxCancel() + // hook to the event so we wait until the peer is connected unhook := deps.NetworkManager.OnNeighborAdded(func(neighbor network.Neighbor) { if neighbor.Peer().ID == addrInfo.ID { // cancel the context to stop waiting - cancel() + connectedCtxCancel() } }).Unhook defer unhook() + // if the peer was already connected, we don't need to wait for it, but we still want to add + // it to the manual peers. + if deps.NetworkManager.NeighborExists(addrInfo.ID) { + connectedCtxCancel() + } + if err := deps.NetworkManager.AddManualPeers(multiAddr); err != nil { return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to add peer: %w", err) } // wait for the peer to be added or the context to be done - <-ctx.Done() - if ierrors.Is(ctx.Err(), context.DeadlineExceeded) { + <-connectedCtx.Done() + if ierrors.Is(connectedCtx.Err(), context.DeadlineExceeded) { return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to add peer: timeout") } diff --git a/go.mod b/go.mod index 74672a25d..535f5b2aa 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/iotaledger/hive.go/stringify v0.0.0-20240326102522-2e37ab3611a3 github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 - github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 + github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205 github.com/labstack/echo/v4 v4.11.4 github.com/labstack/gommon v0.4.2 github.com/libp2p/go-libp2p v0.33.1 diff --git a/go.sum b/go.sum index be7d15714..dd4f9f46a 100644 --- a/go.sum +++ b/go.sum @@ -326,8 +326,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM= github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8fakxvFaj7dLckoo/z+mRyBdZo8QvT8HcgnQlG2Sg= github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff/go.mod h1:aVEutEWFnhDNJBxtVuzy2BeTN+8FAlnR83k7hKV0CFE= -github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 h1:e0CGNpvHDaAO1Xj/LUu9f+GZrixncJgyId4o4iS55RU= -github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg= +github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205 h1:nn7nCEezVLmFExiONAJ2XAgZKOJJ+iWrwfDBFdYTKSY= +github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg= github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw= github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= diff --git a/tools/gendoc/go.mod b/tools/gendoc/go.mod index 5b811049d..54e70dc0c 100644 --- a/tools/gendoc/go.mod +++ b/tools/gendoc/go.mod @@ -75,7 +75,7 @@ require ( github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec // indirect github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 // indirect github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff // indirect - github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 // indirect + github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205 // indirect github.com/ipfs/boxo v0.18.0 // indirect github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect diff --git a/tools/gendoc/go.sum b/tools/gendoc/go.sum index 6eaf45ce0..f3f480790 100644 --- a/tools/gendoc/go.sum +++ b/tools/gendoc/go.sum @@ -330,8 +330,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM= github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8fakxvFaj7dLckoo/z+mRyBdZo8QvT8HcgnQlG2Sg= github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff/go.mod h1:aVEutEWFnhDNJBxtVuzy2BeTN+8FAlnR83k7hKV0CFE= -github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0 h1:e0CGNpvHDaAO1Xj/LUu9f+GZrixncJgyId4o4iS55RU= -github.com/iotaledger/iota.go/v4 v4.0.0-20240322144458-38af7c91d5a0/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg= +github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205 h1:nn7nCEezVLmFExiONAJ2XAgZKOJJ+iWrwfDBFdYTKSY= +github.com/iotaledger/iota.go/v4 v4.0.0-20240325092426-32979eef3205/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg= github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw= github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=