-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rest-store-v3
- Loading branch information
Showing
8 changed files
with
241 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package peermanager | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p/core/host" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/ping" | ||
"github.com/waku-org/go-waku/logging" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type FastestPeerSelector struct { | ||
sync.RWMutex | ||
|
||
host host.Host | ||
|
||
logger *zap.Logger | ||
} | ||
|
||
func NewFastestPeerSelector(logger *zap.Logger) *FastestPeerSelector { | ||
return &FastestPeerSelector{ | ||
logger: logger.Named("rtt-cache"), | ||
} | ||
} | ||
|
||
func (r *FastestPeerSelector) SetHost(h host.Host) { | ||
r.host = h | ||
} | ||
|
||
func (r *FastestPeerSelector) PingPeer(ctx context.Context, peer peer.ID) (time.Duration, error) { | ||
if peer == r.host.ID() { | ||
return 0, errors.New("can't ping yourself") | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 7*time.Second) | ||
defer cancel() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return 0, ctx.Err() | ||
|
||
case result := <-ping.Ping(ctx, r.host, peer): | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
if result.Error == nil { | ||
return result.RTT, nil | ||
} else { | ||
r.logger.Debug("could not ping", logging.HostID("peer", peer), zap.Error(result.Error)) | ||
return 0, result.Error | ||
} | ||
} | ||
|
||
} | ||
|
||
func (r *FastestPeerSelector) FastestPeer(ctx context.Context, peers peer.IDSlice) (peer.ID, error) { | ||
var peerRTT []pingResult | ||
var peerRTTMutex sync.Mutex | ||
|
||
wg := sync.WaitGroup{} | ||
pingCh := make(chan peer.ID) | ||
|
||
pinged := make(map[peer.ID]struct{}) | ||
|
||
go func() { | ||
// Ping any peer with no latency recorded | ||
for peerToPing := range pingCh { | ||
go func(p peer.ID) { | ||
defer wg.Done() | ||
rtt := time.Hour | ||
result, err := r.PingPeer(ctx, p) | ||
if err == nil { | ||
rtt = result | ||
} | ||
|
||
peerRTTMutex.Lock() | ||
peerRTT = append(peerRTT, pingResult{ | ||
peerID: p, | ||
rtt: rtt, | ||
connectedness: r.host.Network().Connectedness(p), | ||
}) | ||
peerRTTMutex.Unlock() | ||
}(peerToPing) | ||
} | ||
}() | ||
|
||
for _, p := range peers { | ||
latency := r.host.Peerstore().LatencyEWMA(p) | ||
if latency == 0 { | ||
wg.Add(1) | ||
pinged[p] = struct{}{} // To avoid double pings | ||
pingCh <- p | ||
} else { | ||
peerRTTMutex.Lock() | ||
peerRTT = append(peerRTT, pingResult{ | ||
peerID: p, | ||
rtt: latency, | ||
connectedness: r.host.Network().Connectedness(p), | ||
}) | ||
peerRTTMutex.Unlock() | ||
} | ||
} | ||
|
||
// Wait for pings to be done (if any) | ||
wg.Wait() | ||
close(pingCh) | ||
|
||
sort.Sort(pingSort(peerRTT)) | ||
|
||
for _, p := range peerRTT { | ||
if p.rtt == time.Hour { | ||
break | ||
} | ||
|
||
// Make sure peer is reachable | ||
_, exists := pinged[p.peerID] // Did we just ping the peer? | ||
if !exists { | ||
_, err := r.PingPeer(ctx, p.peerID) | ||
if err != nil { | ||
continue | ||
} else { | ||
if p.rtt != time.Hour { | ||
return p.peerID, nil | ||
} | ||
} | ||
} else { | ||
if p.rtt != time.Hour { | ||
return p.peerID, nil | ||
} | ||
} | ||
} | ||
|
||
return "", ErrNoPeersAvailable | ||
} | ||
|
||
type pingResult struct { | ||
peerID peer.ID | ||
rtt time.Duration | ||
connectedness network.Connectedness | ||
} | ||
|
||
type pingSort []pingResult | ||
|
||
func (a pingSort) Len() int { | ||
return len(a) | ||
} | ||
|
||
func (a pingSort) Swap(i, j int) { | ||
a[i], a[j] = a[j], a[i] | ||
} | ||
|
||
var connectednessPriority map[network.Connectedness]int | ||
|
||
func init() { | ||
// Closer to 0 is prefered | ||
connectednessPriority = map[network.Connectedness]int{ | ||
network.CanConnect: 1, | ||
network.Connected: 1, | ||
network.NotConnected: 2, | ||
network.CannotConnect: 3, | ||
} | ||
} | ||
|
||
func (a pingSort) Less(i, j int) bool { | ||
return connectednessPriority[a[i].connectedness] < connectednessPriority[a[j].connectedness] && a[i].rtt < a[j].rtt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package peermanager | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/peerstore" | ||
"github.com/stretchr/testify/require" | ||
"github.com/waku-org/go-waku/tests" | ||
"github.com/waku-org/go-waku/waku/v2/utils" | ||
) | ||
|
||
func TestRTT(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) | ||
defer cancel() | ||
|
||
h1, _ := tests.MakeHost(ctx, 0, rand.Reader) | ||
h2, _ := tests.MakeHost(ctx, 0, rand.Reader) | ||
h3, _ := tests.MakeHost(ctx, 0, rand.Reader) | ||
|
||
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), peerstore.PermanentAddrTTL) | ||
h1.Peerstore().AddAddrs(h3.ID(), h3.Addrs(), peerstore.PermanentAddrTTL) | ||
|
||
rtt := NewFastestPeerSelector(utils.Logger()) | ||
rtt.SetHost(h1) | ||
|
||
_, err := rtt.FastestPeer(ctx, peer.IDSlice{h2.ID(), h3.ID()}) | ||
require.NoError(t, err) | ||
|
||
// Simulate H3 being no longer available | ||
h3.Close() | ||
|
||
_, err = rtt.FastestPeer(ctx, peer.IDSlice{h3.ID()}) | ||
require.ErrorIs(t, err, ErrNoPeersAvailable) | ||
|
||
// H3 should never return | ||
for i := 0; i < 100; i++ { | ||
p, err := rtt.FastestPeer(ctx, peer.IDSlice{h2.ID(), h3.ID()}) | ||
if err != nil { | ||
require.ErrorIs(t, err, ErrNoPeersAvailable) | ||
} else { | ||
require.NotEqual(t, h3.ID(), p) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.