Skip to content

Commit

Permalink
libnet/ipam: un-embed mutex from addrSpace
Browse files Browse the repository at this point in the history
Embedding `sync.Mutex` into a struct is considered a bad practice
as it makes the mutex methods part of the embedding struct's API.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
  • Loading branch information
akerouanton committed Apr 26, 2024
1 parent 16b2c22 commit a047d4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 8 additions & 8 deletions libnetwork/ipam/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func (aSpace *addrSpace) updatePredefinedStartIndex(amt int) {
}

func (aSpace *addrSpace) allocatePredefinedPool(ipV6 bool) (netip.Prefix, error) {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

for i, nw := range aSpace.getPredefineds() {
if ipV6 != nw.Addr().Is6() {
Expand Down Expand Up @@ -263,8 +263,8 @@ func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[s
}

func (aSpace *addrSpace) requestAddress(nw, sub netip.Prefix, prefAddress netip.Addr, opts map[string]string) (netip.Addr, error) {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

p, ok := aSpace.subnets[nw]
if !ok {
Expand Down Expand Up @@ -314,8 +314,8 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
}

func (aSpace *addrSpace) releaseAddress(nw, sub netip.Prefix, address netip.Addr) error {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

p, ok := aSpace.subnets[nw]
if !ok {
Expand Down Expand Up @@ -390,8 +390,8 @@ func (a *Allocator) DumpDatabase() string {
}

func (aSpace *addrSpace) DumpDatabase() string {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

var b strings.Builder
for k, config := range aSpace.subnets {
Expand Down
10 changes: 5 additions & 5 deletions libnetwork/ipam/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type addrSpace struct {
predefined []netip.Prefix
predefinedStartIndex int

sync.Mutex
mu sync.Mutex
}

// PoolIDFromString creates a new PoolID and populates the SubnetKey object
Expand Down Expand Up @@ -85,8 +85,8 @@ func (p *PoolData) String() string {

// allocateSubnet adds the subnet k to the address space.
func (aSpace *addrSpace) allocateSubnet(nw, sub netip.Prefix) error {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

// Check if already allocated
if pool, ok := aSpace.subnets[nw]; ok {
Expand Down Expand Up @@ -133,8 +133,8 @@ func (aSpace *addrSpace) allocateSubnetL(nw, sub netip.Prefix) error {
}

func (aSpace *addrSpace) releaseSubnet(nw, sub netip.Prefix) error {
aSpace.Lock()
defer aSpace.Unlock()
aSpace.mu.Lock()
defer aSpace.mu.Unlock()

p, ok := aSpace.subnets[nw]
if !ok {
Expand Down

0 comments on commit a047d4b

Please sign in to comment.