Skip to content

Commit

Permalink
rpk: avoid duplicates in container port pool
Browse files Browse the repository at this point in the history
  • Loading branch information
r-vasquez committed May 12, 2022
1 parent ed76668 commit 762153c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/go/rpk/pkg/net/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ func getFreePort() (uint, error) {
}

func GetFreePortPool(n int) ([]uint, error) {
var ports []uint
for i := 0; i < n; i++ {
m := make(map[uint]struct{})
for len(m) != n {
p, err := getFreePort()
if err != nil {
return nil, err
}
ports = append(ports, p)
m[p] = struct{}{}
}
var ports []uint
for port := range m {
ports = append(ports, port)
}
return ports, nil
}

0 comments on commit 762153c

Please sign in to comment.