Skip to content

Commit

Permalink
Replace freeport dependency with modified version (#3470)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Feb 15, 2024
1 parent 6f00c8a commit 0e3476b
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 31 deletions.
6 changes: 3 additions & 3 deletions cmd/cli/serve/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/multiformats/go-multiaddr"
"github.com/phayes/freeport"
"github.com/stretchr/testify/suite"
"golang.org/x/sync/errgroup"

Expand All @@ -22,6 +21,7 @@ import (

"github.com/bacalhau-project/bacalhau/pkg/docker"
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
"github.com/bacalhau-project/bacalhau/pkg/lib/network"

cmd2 "github.com/bacalhau-project/bacalhau/cmd/cli"
"github.com/bacalhau-project/bacalhau/cmd/cli/serve"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *ServeSuite) serve(extraArgs ...string) (uint16, error) {
}
}

bigPort, err := freeport.GetFreePort()
bigPort, err := network.GetFreePort()
s.Require().NoError(err)
port := uint16(bigPort)

Expand Down Expand Up @@ -299,7 +299,7 @@ func (s *ServeSuite) Test200ForNotStartingWebUI() {
}

func (s *ServeSuite) Test200ForRoot() {
webUIPort, err := freeport.GetFreePort()
webUIPort, err := network.GetFreePort()
if err != nil {
s.T().Fatal(err, "Could not get port for web-ui")
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ require (
github.com/open-policy-agent/opa v0.60.0
github.com/opencontainers/image-spec v1.1.0-rc5
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285
github.com/rs/zerolog v1.31.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,6 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 h1:1/WtZae0yGtPq+TI6+Tv1WTxkukpXeMlviSxvL7SRgk=
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9/go.mod h1:x3N5drFsm2uilKKuuYo6LdyD8vZAW55sH/9w+pbo1sw=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
28 changes: 14 additions & 14 deletions pkg/devstack/devstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"strings"

"github.com/bacalhau-project/bacalhau/pkg/authn"
"github.com/bacalhau-project/bacalhau/pkg/lib/network"
"github.com/bacalhau-project/bacalhau/pkg/models"
"github.com/bacalhau-project/bacalhau/pkg/util/multiaddresses"
"github.com/imdario/mergo"
"github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multiaddr"
"github.com/phayes/freeport"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"

"github.com/bacalhau-project/bacalhau/pkg/config"
Expand All @@ -28,7 +29,6 @@ import (

const (
DefaultLibp2pKeySize = 2048
portsPerNode = 3 // 1 for libp2p, 1 for IPFS, 1 for local publisher
)

type DevStackOptions struct {
Expand Down Expand Up @@ -118,15 +118,6 @@ func Setup(
stackConfig.NetworkType = networkType
}

// We will pre-allocate the potential maximum number of free ports we will need during setup
// to ensure that we can allocate them well before use and avoid any potential port clashes.
// Before this change it was possible to get the same port back from freeport.GetFreePort()
// if a previously allocated one was not used immediately
freePorts, err := freeport.GetFreePorts(totalNodeCount * portsPerNode)
if err != nil {
return nil, fmt.Errorf("failed to get free ports: %w", err)
}

for i := 0; i < totalNodeCount; i++ {
nodeID := fmt.Sprintf("node-%d", i)
ctx = logger.ContextWithNodeIDLogger(ctx, nodeID)
Expand Down Expand Up @@ -164,7 +155,9 @@ func Setup(
const startSwarmPort = 4222 // 4222 is the default NATS port
swarmPort = startSwarmPort + i
} else {
swarmPort, freePorts = freePorts[0], freePorts[1:]
if swarmPort, err = network.GetFreePort(); err != nil {
return nil, errors.Wrap(err, "failed to get free port for swarm port")
}
}
clusterConfig := node.NetworkConfig{
Type: stackConfig.NetworkType,
Expand All @@ -179,7 +172,9 @@ func Setup(
const startClusterPort = 6222
clusterPort = startClusterPort + i
} else {
clusterPort, freePorts = freePorts[0], freePorts[1:]
if clusterPort, err = network.GetFreePort(); err != nil {
return nil, errors.Wrap(err, "failed to get free port for cluster port")
}
}

if isRequesterNode {
Expand Down Expand Up @@ -246,7 +241,12 @@ func Setup(
if isComputeNode {
// We have multiple process on the same machine, all wanting to listen on a HTTP port
// and so we will give each compute node a random open port to listen on.
stackConfig.ComputeConfig.LocalPublisher.Port, freePorts = freePorts[0], freePorts[1:]
fport, err := network.GetFreePort()
if err != nil {
return nil, errors.Wrap(err, "failed to get free port for local publisher")
}

stackConfig.ComputeConfig.LocalPublisher.Port = fport
stackConfig.ComputeConfig.LocalPublisher.Address = "127.0.0.1" //nolint:gomnd
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/lib/network/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package network

import (
"net"
)

// GetFreePort returns a single available port by asking the operating
// system to pick one for us. Luckily ports are not re-used so after asking
// for a port number, we attempt to create a tcp listener.
//
// Essentially the same code as https://github.com/phayes/freeport but we bind
// to 0.0.0.0 to ensure the port is free on all interfaces, and not just localhost.GetFreePort
// Ports must be unique for an address, not an entire system and so checking just localhost
// is not enough.
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", ":0")
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

// GetFreePorts returns an array available ports by asking the operating
// system to pick one for us.
//
// Essentially the same code as https://github.com/phayes/freeport apart from
// the caveats described in GetFreePort.
func GetFreePorts(count int) ([]int, error) {
ports := []int{}

for i := 0; i < count; i++ {
port, err := GetFreePort()
if err != nil {
return nil, err
}
ports = append(ports, port)
}
return ports, nil
}
47 changes: 47 additions & 0 deletions pkg/lib/network/ports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build unit || !integration

package network_test

import (
"net"
"strconv"
"testing"

"github.com/bacalhau-project/bacalhau/pkg/lib/network"
"github.com/stretchr/testify/suite"
)

type FreePortTestSuite struct {
suite.Suite
}

func TestFreePortTestSuite(t *testing.T) {
suite.Run(t, new(FreePortTestSuite))
}

func (s *FreePortTestSuite) TestGetFreePort() {
port, err := network.GetFreePort()
s.Require().NoError(err)
s.NotEqual(0, port, "expected a non-zero port")

// Try to listen on the port
l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
s.Require().NoError(err)
defer l.Close()
}

func (s *FreePortTestSuite) TestGetFreePorts() {
count := 3
ports, err := network.GetFreePorts(count)
s.Require().NoError(err)
s.Equal(count, len(ports), "expected %d ports", count)

for _, port := range ports {
s.NotEqual(0, port, "expected a non-zero port")

// Try to listen on the port
l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
s.Require().NoError(err, "failed to listen on newly given port")
defer l.Close()
}
}
7 changes: 4 additions & 3 deletions pkg/libp2p/rcmgr/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"testing"
"time"

netwk "github.com/bacalhau-project/bacalhau/pkg/lib/network"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/phayes/freeport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
Expand All @@ -39,12 +40,12 @@ func TestMetricsReporter(t *testing.T) {
sdkmetric.WithReader(reader),
)

port1, err := freeport.GetFreePort()
port1, err := netwk.GetFreePort()
require.NoError(t, err)

host1 := startListener(t, port1, meterProvider)

port2, err := freeport.GetFreePort()
port2, err := netwk.GetFreePort()
require.NoError(t, err)

host2 := startListener(t, port2, meterProvider)
Expand Down
5 changes: 3 additions & 2 deletions pkg/libp2p/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"strings"
"time"

"github.com/bacalhau-project/bacalhau/pkg/lib/network"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/phayes/freeport"
"github.com/rs/zerolog/log"
)

Expand All @@ -33,7 +34,7 @@ func encapsulateP2pAddrs(peerInfo peer.AddrInfo) ([]multiaddr.Multiaddr, error)
}

func NewHostForTest(ctx context.Context, peers ...host.Host) (host.Host, error) {
port, err := freeport.GetFreePort()
port, err := network.GetFreePort()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/nats/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"testing"
"time"

"github.com/bacalhau-project/bacalhau/pkg/lib/network"
nats_helper "github.com/bacalhau-project/bacalhau/pkg/nats"
"github.com/bacalhau-project/bacalhau/pkg/pubsub"
"github.com/nats-io/nats-server/v2/server"
"github.com/phayes/freeport"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s *PubSubSuite) TearDownSuite() {
// createNatsServer creates a new nats server
func (s *PubSubSuite) createNatsServer() *server.Server {
ctx := context.Background()
port, err := freeport.GetFreePort()
port, err := network.GetFreePort()
s.Require().NoError(err)

serverOpts := server.Options{
Expand Down
4 changes: 2 additions & 2 deletions pkg/publicapi/test/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/phayes/freeport"
"github.com/stretchr/testify/require"

"github.com/bacalhau-project/bacalhau/pkg/lib/network"
"github.com/bacalhau-project/bacalhau/pkg/models"

"github.com/bacalhau-project/bacalhau/pkg/config"
Expand All @@ -35,7 +35,7 @@ func setupNodeForTestWithConfig(t *testing.T, apiCfg publicapi.Config) (*node.No
cm := system.NewCleanupManager()
t.Cleanup(func() { cm.Cleanup(context.Background()) })

libp2pPort, err := freeport.GetFreePort()
libp2pPort, err := network.GetFreePort()
require.NoError(t, err)

privKey, err := config.GetLibp2pPrivKey()
Expand Down
4 changes: 2 additions & 2 deletions pkg/test/compute/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/labstack/echo/v4"
"github.com/phayes/freeport"
"github.com/stretchr/testify/suite"

"github.com/bacalhau-project/bacalhau/pkg/authz"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/bacalhau-project/bacalhau/pkg/config"
"github.com/bacalhau-project/bacalhau/pkg/executor"
noop_executor "github.com/bacalhau-project/bacalhau/pkg/executor/noop"
"github.com/bacalhau-project/bacalhau/pkg/lib/network"
"github.com/bacalhau-project/bacalhau/pkg/lib/provider"
"github.com/bacalhau-project/bacalhau/pkg/libp2p"
"github.com/bacalhau-project/bacalhau/pkg/models"
Expand Down Expand Up @@ -70,7 +70,7 @@ func (s *ComputeSuite) SetupTest() {

func (s *ComputeSuite) setupNode() {
repo := repo2.SetupBacalhauRepoForTesting(s.T())
libp2pPort, err := freeport.GetFreePort()
libp2pPort, err := network.GetFreePort()
s.NoError(err)

privKey, err := config.GetLibp2pPrivKey()
Expand Down

0 comments on commit 0e3476b

Please sign in to comment.