Skip to content

Commit

Permalink
reservedipv6: refactor atomic and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
anitgandhi committed Dec 13, 2024
1 parent 6fd6317 commit 45f8c92
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
64 changes: 44 additions & 20 deletions internal/metadata/actioner/reserved_ipv6_actioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,81 @@ import (
)

const (
rip6LogPrefix string = "[Reserved IPv6 Actioner]"
logPrefix string = "[Reserved IPv6 Actioner]"
)

// NewReservedIPv6Actioner returns a new DigitalOcean Reserved IPv6 actioner
func NewReservedIPv6Actioner(mgr reservedipv6.Manager) MetadataActioner {
return &reservedIPv6Actioner{
mgr: mgr,
allDone: make(chan struct{}, 1),
mgr: mgr,
activeActions: &atomic.Uint32{},
closing: &atomic.Bool{},
allDone: make(chan struct{}, 1),
}
}

type reservedIPv6Actioner struct {
mgr reservedipv6.Manager
activeActions int32
closing uint32
activeActions *atomic.Uint32
closing *atomic.Bool
allDone chan struct{}
}

func (da *reservedIPv6Actioner) Do(md *metadata.Metadata) {
atomic.AddInt32(&da.activeActions, 1)
da.activeActions.Add(1)
defer func() {
ret := atomic.AddInt32(&da.activeActions, -1)
if ret == 0 && atomic.LoadUint32(&da.closing) == 1 {
// decrement active counter, then check shutdown state
ret := da.activeActions.Add(^uint32(0))
if ret == 0 && da.closing.Load() {
close(da.allDone)
}
}()

ipv6 := md.ReservedIP.IPv6

if ipv6.Active {
log.Info("%s Attempting to assign Reserved IPv6 address '%s'", rip6LogPrefix, ipv6.IPAddress)
logDebug("Attempting to assign Reserved IPv6 address '%s'", ipv6.IPAddress)
if err := da.mgr.Assign(ipv6.IPAddress); err != nil {
log.Error("%s failed to assign Reserved IPv6 address '%s': %v", rip6LogPrefix, ipv6.IPAddress, err)
logError("failed to assign Reserved IPv6 address '%s': %v", ipv6.IPAddress, err)
return
}
log.Info("%s Assigned Reserved IPv6 address '%s'", rip6LogPrefix, ipv6.IPAddress)
logInfo("Assigned Reserved IPv6 address '%s'", ipv6.IPAddress)
} else {
log.Info("%s Attempting to unassign all Reserved IPv6 addresses", rip6LogPrefix)
logDebug("Attempting to unassign all Reserved IPv6 addresses")
if err := da.mgr.Unassign(); err != nil {
log.Error("%s failed to unassign all Reserved IPv6 addresses: %v", rip6LogPrefix, err)
logError("failed to unassign all Reserved IPv6 addresses: %v", err)
return
}
log.Info("%s Unassigned all Reserved IPv6 addresses", rip6LogPrefix)
logInfo("Unassigned all Reserved IPv6 addresses")
}
}

func (da *reservedIPv6Actioner) Shutdown() {
log.Info("%s Shutting down", rip6LogPrefix)
atomic.StoreUint32(&da.closing, 1)
if atomic.LoadInt32(&da.activeActions) != 0 {
// if there are still jobs in progress, wait for them to finish
log.Debug("%s Waiting for jobs in progress", rip6LogPrefix)
logInfo("Shutting down")
da.closing.Store(true)

// if there are still jobs in progress, wait for them to finish
if da.activeActions.Load() > 0 {
logDebug("Waiting for jobs in progress")
<-da.allDone
}
log.Info("%s Bye-bye", rip6LogPrefix)
logInfo("Bye-bye")
}

// logInfo wraps log.Info with rip6LogPrefix
func logInfo(format string, params ...any) {
msg := logPrefix + " " + format
log.Info(msg, params)
}

// logDebug wraps log.Debug with rip6LogPrefix
func logDebug(format string, params ...any) {
msg := logPrefix + " " + format
log.Debug(msg, params)
}

// logError wraps log.Error with rip6LogPrefix
func logError(format string, params ...any) {
msg := logPrefix + " " + format
log.Error(msg, params)
}
4 changes: 1 addition & 3 deletions internal/reservedipv6/reserved_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
)

const (
logPrefix string = "[Reserved IPv6 Manager]"

loIface string = "lo"
eth0Iface string = "eth0"
prefixLen uint8 = 128
Expand Down Expand Up @@ -75,7 +73,7 @@ func (m *mgr) Assign(ip string) error {
func (m *mgr) Unassign() error {
addrs, err := m.nlConn.Address.List()
if err != nil {
fmt.Errorf("failed to list addreses: %w", err)
return fmt.Errorf("failed to list addreses: %w", err)
}

for _, a := range addrs {
Expand Down

0 comments on commit 45f8c92

Please sign in to comment.