Skip to content

Commit

Permalink
p2p: reuse existing p2p Host for http clients
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Sep 12, 2024
1 parent 3b3b5ce commit 13f21a9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
39 changes: 32 additions & 7 deletions network/p2p/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,38 @@ func (s *HTTPServer) RegisterHTTPHandlerFunc(path string, handler func(http.Resp
})
}

type httpClientConfig struct {
host host.Host
}

type httpClientOption func(*httpClientConfig)

// WithHost sets the libp2p host for the http client.
func WithHost(h host.Host) httpClientOption {
return func(o *httpClientConfig) {
o.host = h

Check warning on line 87 in network/p2p/http.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/http.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
}

// MakeHTTPClient creates a http.Client that uses libp2p transport for a given protocol and peer address.
func MakeHTTPClient(addrInfo *peer.AddrInfo) (*http.Client, error) {
clientStreamHost, err := libp2p.New(libp2p.NoListenAddrs)
if err != nil {
return nil, err
// If service is nil, a new libp2p host is created.
func MakeHTTPClient(addrInfo *peer.AddrInfo, opts ...httpClientOption) (*http.Client, error) {
var config httpClientConfig
for _, opt := range opts {
opt(&config)

Check warning on line 96 in network/p2p/http.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/http.go#L93-L96

Added lines #L93 - L96 were not covered by tests
}

var clientStreamHost host.Host
if config.host != nil {
clientStreamHost = config.host
} else {
var err error
clientStreamHost, err = libp2p.New(libp2p.NoListenAddrs)
if err != nil {
return nil, err

Check warning on line 106 in network/p2p/http.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/http.go#L99-L106

Added lines #L99 - L106 were not covered by tests
}
logging.Base().Debugf("MakeHTTPClient made a new P2P host %s for %s", clientStreamHost.ID(), addrInfo.String())

Check warning on line 108 in network/p2p/http.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/http.go#L108

Added line #L108 was not covered by tests
}
logging.Base().Debugf("MakeHTTPClient made a new P2P host %s for %s", clientStreamHost.ID(), addrInfo.String())

client := libp2phttp.Host{StreamHost: clientStreamHost}

Expand All @@ -98,8 +123,8 @@ func MakeHTTPClient(addrInfo *peer.AddrInfo) (*http.Client, error) {
}

// MakeHTTPClientWithRateLimit creates a http.Client that uses libp2p transport for a given protocol and peer address.
func MakeHTTPClientWithRateLimit(addrInfo *peer.AddrInfo, pstore limitcaller.ConnectionTimeStore, queueingTimeout time.Duration) (*http.Client, error) {
cl, err := MakeHTTPClient(addrInfo)
func MakeHTTPClientWithRateLimit(addrInfo *peer.AddrInfo, service Service, pstore limitcaller.ConnectionTimeStore, queueingTimeout time.Duration) (*http.Client, error) {
cl, err := MakeHTTPClient(addrInfo, WithHost(service.(*serviceImpl).host))

Check warning on line 127 in network/p2p/http.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/http.go#L126-L127

Added lines #L126 - L127 were not covered by tests
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions network/p2pNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func addrInfoToWsPeerCore(n *P2PNetwork, addrInfo *peer.AddrInfo) (wsPeerCore, b
}
addr := mas[0].String()

client, err := p2p.MakeHTTPClientWithRateLimit(addrInfo, n.pstore, limitcaller.DefaultQueueingTimeout)
client, err := p2p.MakeHTTPClientWithRateLimit(addrInfo, n.service, n.pstore, limitcaller.DefaultQueueingTimeout)
if err != nil {
n.log.Warnf("MakeHTTPClient failed: %v", err)
return wsPeerCore{}, false
Expand Down Expand Up @@ -718,7 +718,7 @@ func (n *P2PNetwork) GetHTTPClient(address string) (*http.Client, error) {
if err != nil {
return nil, err
}
return p2p.MakeHTTPClientWithRateLimit(addrInfo, n.pstore, limitcaller.DefaultQueueingTimeout)
return p2p.MakeHTTPClientWithRateLimit(addrInfo, n.service, n.pstore, limitcaller.DefaultQueueingTimeout)

Check warning on line 721 in network/p2pNetwork.go

View check run for this annotation

Codecov / codecov/patch

network/p2pNetwork.go#L721

Added line #L721 was not covered by tests
}

// OnNetworkAdvance notifies the network library that the agreement protocol was able to make a notable progress.
Expand Down Expand Up @@ -771,7 +771,7 @@ func (n *P2PNetwork) wsStreamHandler(ctx context.Context, p2pPeer peer.ID, strea

// create a wsPeer for this stream and added it to the peers map.
addrInfo := &peer.AddrInfo{ID: p2pPeer, Addrs: []multiaddr.Multiaddr{ma}}
client, err := p2p.MakeHTTPClientWithRateLimit(addrInfo, n.pstore, limitcaller.DefaultQueueingTimeout)
client, err := p2p.MakeHTTPClientWithRateLimit(addrInfo, n.service, n.pstore, limitcaller.DefaultQueueingTimeout)
if err != nil {
n.log.Warnf("Cannot construct HTTP Client for %s: %v", p2pPeer, err)
client = nil
Expand Down
4 changes: 3 additions & 1 deletion network/p2pNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,12 @@ func TestP2PHTTPHandler(t *testing.T) {

// check rate limiting client:
// zero clients allowed, rate limiting window (10s) is greater than queue deadline (1s)
netB, err := NewP2PNetwork(log, cfg, "", nil, genesisID, config.Devtestnet, &nopeNodeInfo{}, nil)
require.NoError(t, err)
pstore, err := peerstore.MakePhonebook(0, 10*time.Second)
require.NoError(t, err)
pstore.AddPersistentPeers([]*peer.AddrInfo{&peerInfoA}, "net", phonebook.PhoneBookEntryRelayRole)
httpClient, err = p2p.MakeHTTPClientWithRateLimit(&peerInfoA, pstore, 1*time.Second)
httpClient, err = p2p.MakeHTTPClientWithRateLimit(&peerInfoA, netB.service, pstore, 1*time.Second)
require.NoError(t, err)
_, err = httpClient.Get("/test")
require.ErrorIs(t, err, limitcaller.ErrConnectionQueueingTimeout)
Expand Down

0 comments on commit 13f21a9

Please sign in to comment.