Bladeren bron

libnet/ipam: un-embed mutex from `addrSpace`

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>
Albin Kerouanton 1 jaar geleden
bovenliggende
commit
c75220eeff
2 gewijzigde bestanden met toevoegingen van 13 en 13 verwijderingen
  1. 8 8
      libnetwork/ipam/allocator.go
  2. 5 5
      libnetwork/ipam/structures.go

+ 8 - 8
libnetwork/ipam/allocator.go

@@ -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() {
@@ -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 {
@@ -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 {
@@ -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 {

+ 5 - 5
libnetwork/ipam/structures.go

@@ -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
@@ -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 {
@@ -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 {