Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Do not wait for known peers
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 26, 2024
1 parent 71c2063 commit df5973f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
18 changes: 13 additions & 5 deletions components/restapi/management/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit df5973f

Please sign in to comment.