fix: resolve critical bugs and refactor code structure
P0 Fixes: - Fix potential panic in factory.go due to unsafe type assertion - Fix VIP CIDR mask being lost during parsing (was hardcoded to /32) P1 Fixes: - Fix go.mod incorrect indirect dependency markers - Fix receiveLoop blocking issue preventing graceful shutdown Refactoring: - Split state.go into state.go, timer.go, priority.go, history.go - Split monitor.go into monitor.go and manager.go - Add IncreasePriority() method for complete priority adjustment - Fix go vet format string warning in test.go 🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
64
internal/vrrp/timer.go
Normal file
64
internal/vrrp/timer.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package vrrp
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Timer provides a thread-safe timer with callback support.
|
||||
type Timer struct {
|
||||
duration time.Duration
|
||||
timer *time.Timer
|
||||
callback func()
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewTimer creates a new Timer with the specified duration and callback.
|
||||
func NewTimer(duration time.Duration, callback func()) *Timer {
|
||||
return &Timer{
|
||||
duration: duration,
|
||||
callback: callback,
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts or restarts the timer.
|
||||
func (t *Timer) Start() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
}
|
||||
|
||||
t.timer = time.AfterFunc(t.duration, t.callback)
|
||||
}
|
||||
|
||||
// Stop stops the timer if it's running.
|
||||
func (t *Timer) Stop() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
t.timer = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Reset stops the current timer and starts a new one with the same duration.
|
||||
func (t *Timer) Reset() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
}
|
||||
|
||||
t.timer = time.AfterFunc(t.duration, t.callback)
|
||||
}
|
||||
|
||||
// SetDuration updates the timer's duration for future starts.
|
||||
func (t *Timer) SetDuration(duration time.Duration) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.duration = duration
|
||||
}
|
||||
Reference in New Issue
Block a user