Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: availableport enhancements #3621

Merged
merged 24 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Changes

- [#3621](https://github.com/ignite/cli/pull/3621) Refactor `ignite/pkg/availablePort` package to allow custom parametersin `Find` function.
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
- [#3581](https://github.com/ignite/cli/pull/3581) Bump cometbft and cometbft-db in the template
- [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1`
- [#3522](https://github.com/ignite/cli/pull/3522) Remove indentation from `chain serve` output
Expand Down
67 changes: 61 additions & 6 deletions ignite/pkg/availableport/availableport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,64 @@ import (
"time"
)

type availablePortOptions struct {
randomizer *rand.Rand
minPort uint
maxPort uint
}

type Options func(o *availablePortOptions)

func WithRandomizer(r *rand.Rand) Options {
return func(o *availablePortOptions) {
o.randomizer = r
}
}

func WithMaxPort(maxPort uint) Options {
return func(o *availablePortOptions) {
o.maxPort = maxPort
}
}

func WithMinPort(minPort uint) Options {
return func(o *availablePortOptions) {
o.minPort = minPort
}
}

// Find finds n number of unused ports.
// it is not guaranteed that these ports will not be allocated to
// another program in the time of calling Find().
func Find(n int) (ports []int, err error) {
min := 44000
max := 55000
func Find(n uint, options ...Options) (ports []uint, err error) {
// Defining them before so we can set a value depending on the AvailablePortOptions
opts := availablePortOptions{
minPort: 44000,
maxPort: 55000,
randomizer: rand.New(rand.NewSource(time.Now().UnixNano())),
}

for _, apply := range options {
apply(&opts)
}
// If the number of ports required is bigger than the range, this stops it
if opts.maxPort < opts.minPort {
return nil, fmt.Errorf("invalid ports range: max < min (%d < %d)", opts.maxPort, opts.minPort)
}

for i := 0; i < n; i++ {
// If the number of ports required is bigger than the range, this stops it
if n > (opts.maxPort - opts.minPort) {
return nil, fmt.Errorf("invalid amount of ports requested: limit is %d", opts.maxPort-opts.minPort)
}

// Marker to point if a port is already added in the list
registered := make(map[uint]bool)
for i := uint(0); i < n; i++ {
for {
Pantani marked this conversation as resolved.
Show resolved Hide resolved
Pantani marked this conversation as resolved.
Show resolved Hide resolved
rand.Seed(time.Now().UnixNano())
port := rand.Intn(max-min+1) + min
// Greater or equal to min and lower than max
totalPorts := opts.maxPort - opts.minPort + 1
randomPort := opts.randomizer.Intn(int(totalPorts))
port := uint(randomPort) + opts.minPort

conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port))
// if there is an error, this might mean that no one is listening from this port
Expand All @@ -26,7 +73,15 @@ func Find(n int) (ports []int, err error) {
conn.Close()
continue
}
defer conn.Close()

// if the port is already registered we skip it to the next one
// otherwise it's added to the ports list and pointed in our map
if registered[port] {
continue
}
ports = append(ports, port)
registered[port] = true
break
}
Pantani marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
83 changes: 83 additions & 0 deletions ignite/pkg/availableport/availableport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package availableport_test

import (
"fmt"
"math/rand"
"testing"

"github.com/ignite/cli/ignite/pkg/availableport"
"github.com/stretchr/testify/require"
)

func TestAvailablePort(t *testing.T) {
Pantani marked this conversation as resolved.
Show resolved Hide resolved

ports, err := availableport.Find(10)
require.Equal(t, nil, err)
require.Equal(t, 10, len(ports))

for idx := 0; idx < 9; idx++ {
for jdx := idx + 1; jdx < 10; jdx++ {
require.NotEqual(t, ports[idx], ports[jdx])
}
}

// Case no ports generated
options := availableport.OptionalParameters{
WithMinPort: 1,
WithMaxPort: 1,
}
ports, err = availableport.Find(10, options)
gfant marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, fmt.Errorf("invalid amount of ports requested: limit is 0"), err)
require.Equal(t, 0, len(ports))

// Case max < min
options = availableport.OptionalParameters{
WithMinPort: 5,
WithMaxPort: 1,
}
ports, err = availableport.Find(10, options)
require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 5)"), err)
require.Equal(t, 0, len(ports))

// Case max < min min restriction given
options = availableport.OptionalParameters{
WithMinPort: 55001,
}
ports, err = availableport.Find(10, options)
require.Equal(t, fmt.Errorf("invalid ports range: max < min (55000 < 55001)"), err)
require.Equal(t, 0, len(ports))

// Case max < min max restriction given
options = availableport.OptionalParameters{
WithMaxPort: 43999,
}
ports, err = availableport.Find(10, options)
require.Equal(t, fmt.Errorf("invalid ports range: max < min (43999 < 44000)"), err)
require.Equal(t, 0, len(ports))

// Case negative min
options = availableport.OptionalParameters{
WithMinPort: -10,
}
ports, err = availableport.Find(10, options)
require.Equal(t, fmt.Errorf("ports can't be negative (negative min port given)"), err)
require.Equal(t, 0, len(ports))

// Case negative max
options = availableport.OptionalParameters{
WithMaxPort: -10,
}
ports, err = availableport.Find(10, options)
require.Equal(t, fmt.Errorf("ports can't be negative (negative max port given)"), err)
require.Equal(t, 0, len(ports))

// Case randomizer given
options = availableport.OptionalParameters{
WithRandomizer: rand.New(rand.NewSource(2023)),
WithMinPort: 100,
WithMaxPort: 200,
}
ports, err = availableport.Find(10, options)
require.Equal(t, 10, len(ports))
require.Equal(t, []int{195, 129, 150, 108, 120, 169, 166, 121, 131, 160}, ports)
}