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:
95
internal/vrrp/history.go
Normal file
95
internal/vrrp/history.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package vrrp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// StateTransition represents a single state transition event.
|
||||
type StateTransition struct {
|
||||
From State
|
||||
To State
|
||||
Timestamp time.Time
|
||||
Reason string
|
||||
}
|
||||
|
||||
// StateHistory maintains a bounded history of state transitions.
|
||||
type StateHistory struct {
|
||||
transitions []StateTransition
|
||||
maxSize int
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewStateHistory creates a new StateHistory with the specified maximum size.
|
||||
func NewStateHistory(maxSize int) *StateHistory {
|
||||
return &StateHistory{
|
||||
transitions: make([]StateTransition, 0, maxSize),
|
||||
maxSize: maxSize,
|
||||
}
|
||||
}
|
||||
|
||||
// Add records a new state transition.
|
||||
func (sh *StateHistory) Add(from, to State, reason string) {
|
||||
sh.mu.Lock()
|
||||
defer sh.mu.Unlock()
|
||||
|
||||
transition := StateTransition{
|
||||
From: from,
|
||||
To: to,
|
||||
Timestamp: time.Now(),
|
||||
Reason: reason,
|
||||
}
|
||||
|
||||
sh.transitions = append(sh.transitions, transition)
|
||||
|
||||
// Maintain bounded size using ring buffer style
|
||||
if len(sh.transitions) > sh.maxSize {
|
||||
// Copy to new slice to allow garbage collection of old backing array
|
||||
newTransitions := make([]StateTransition, len(sh.transitions)-1, sh.maxSize)
|
||||
copy(newTransitions, sh.transitions[1:])
|
||||
sh.transitions = newTransitions
|
||||
}
|
||||
}
|
||||
|
||||
// GetRecent returns the most recent n transitions.
|
||||
func (sh *StateHistory) GetRecent(n int) []StateTransition {
|
||||
sh.mu.RLock()
|
||||
defer sh.mu.RUnlock()
|
||||
|
||||
if n > len(sh.transitions) {
|
||||
n = len(sh.transitions)
|
||||
}
|
||||
|
||||
start := len(sh.transitions) - n
|
||||
result := make([]StateTransition, n)
|
||||
copy(result, sh.transitions[start:])
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Len returns the number of recorded transitions.
|
||||
func (sh *StateHistory) Len() int {
|
||||
sh.mu.RLock()
|
||||
defer sh.mu.RUnlock()
|
||||
return len(sh.transitions)
|
||||
}
|
||||
|
||||
// String returns a formatted string representation of the history.
|
||||
func (sh *StateHistory) String() string {
|
||||
sh.mu.RLock()
|
||||
defer sh.mu.RUnlock()
|
||||
|
||||
if len(sh.transitions) == 0 {
|
||||
return "No state transitions"
|
||||
}
|
||||
|
||||
result := "State transition history:\n"
|
||||
for _, t := range sh.transitions {
|
||||
result += fmt.Sprintf(" %s: %s -> %s (%s)\n",
|
||||
t.Timestamp.Format("2006-01-02 15:04:05"),
|
||||
t.From, t.To, t.Reason)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -17,6 +17,7 @@ type Instance struct {
|
||||
AdvertInterval uint8
|
||||
Interface string
|
||||
VirtualIPs []net.IP
|
||||
VirtualIPCIDRs []string // preserve original CIDR notation
|
||||
AuthType uint8
|
||||
AuthPass string
|
||||
TrackScripts []string
|
||||
@@ -64,12 +65,14 @@ func NewInstance(
|
||||
}
|
||||
|
||||
virtualIPs := make([]net.IP, 0, len(vips))
|
||||
virtualIPCIDRs := make([]string, 0, len(vips))
|
||||
for _, vip := range vips {
|
||||
ip, _, err := net.ParseCIDR(vip)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid VIP %s: %w", vip, err)
|
||||
}
|
||||
virtualIPs = append(virtualIPs, ip)
|
||||
virtualIPCIDRs = append(virtualIPCIDRs, vip)
|
||||
}
|
||||
|
||||
var authTypeNum uint8
|
||||
@@ -94,6 +97,7 @@ func NewInstance(
|
||||
AdvertInterval: advertInt,
|
||||
Interface: iface,
|
||||
VirtualIPs: virtualIPs,
|
||||
VirtualIPCIDRs: virtualIPCIDRs,
|
||||
AuthType: authTypeNum,
|
||||
AuthPass: authPass,
|
||||
TrackScripts: trackScripts,
|
||||
@@ -192,8 +196,15 @@ func (inst *Instance) receiveLoop() {
|
||||
default:
|
||||
}
|
||||
|
||||
// Set read deadline to allow periodic check of stop channel
|
||||
inst.socket.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
|
||||
pkt, srcIP, err := inst.socket.Receive()
|
||||
if err != nil {
|
||||
// Check if it's a timeout error, which is expected
|
||||
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
inst.log.Debug("[%s] failed to receive packet: %v", inst.Name, err)
|
||||
continue
|
||||
}
|
||||
@@ -371,11 +382,7 @@ func (inst *Instance) removeVIPs() error {
|
||||
}
|
||||
|
||||
func (inst *Instance) getVIPsWithCIDR() []string {
|
||||
result := make([]string, len(inst.VirtualIPs))
|
||||
for i, ip := range inst.VirtualIPs {
|
||||
result[i] = ip.String() + "/32"
|
||||
}
|
||||
return result
|
||||
return inst.VirtualIPCIDRs
|
||||
}
|
||||
|
||||
func (inst *Instance) GetState() State {
|
||||
@@ -399,15 +406,17 @@ func (inst *Instance) AdjustPriority(delta int) {
|
||||
defer inst.mu.Unlock()
|
||||
|
||||
oldPriority := inst.priorityCalc.GetPriority()
|
||||
|
||||
|
||||
if delta < 0 {
|
||||
inst.priorityCalc.DecreasePriority(uint8(-delta))
|
||||
} else if delta > 0 {
|
||||
inst.priorityCalc.IncreasePriority(uint8(delta))
|
||||
}
|
||||
|
||||
|
||||
newPriority := inst.priorityCalc.GetPriority()
|
||||
|
||||
|
||||
if oldPriority != newPriority {
|
||||
inst.log.Info("[%s] priority adjusted: %d -> %d (delta=%d)",
|
||||
inst.log.Info("[%s] priority adjusted: %d -> %d (delta=%d)",
|
||||
inst.Name, oldPriority, newPriority, delta)
|
||||
}
|
||||
}
|
||||
|
||||
99
internal/vrrp/priority.go
Normal file
99
internal/vrrp/priority.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package vrrp
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PriorityCalculator manages VRRP priority with support for dynamic adjustment.
|
||||
type PriorityCalculator struct {
|
||||
basePriority uint8
|
||||
currentPriority uint8
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewPriorityCalculator creates a new PriorityCalculator with the specified base priority.
|
||||
func NewPriorityCalculator(basePriority uint8) *PriorityCalculator {
|
||||
return &PriorityCalculator{
|
||||
basePriority: basePriority,
|
||||
currentPriority: basePriority,
|
||||
}
|
||||
}
|
||||
|
||||
// GetPriority returns the current priority.
|
||||
func (pc *PriorityCalculator) GetPriority() uint8 {
|
||||
pc.mu.RLock()
|
||||
defer pc.mu.RUnlock()
|
||||
return pc.currentPriority
|
||||
}
|
||||
|
||||
// DecreasePriority decreases the current priority by the specified amount.
|
||||
// The priority will not go below 0.
|
||||
func (pc *PriorityCalculator) DecreasePriority(amount uint8) {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
|
||||
if pc.currentPriority > amount {
|
||||
pc.currentPriority -= amount
|
||||
} else {
|
||||
pc.currentPriority = 0
|
||||
}
|
||||
}
|
||||
|
||||
// IncreasePriority increases the current priority by the specified amount.
|
||||
// The priority will not exceed 255 or the base priority.
|
||||
func (pc *PriorityCalculator) IncreasePriority(amount uint8) {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
|
||||
newPriority := pc.currentPriority + amount
|
||||
if newPriority > pc.basePriority {
|
||||
newPriority = pc.basePriority
|
||||
}
|
||||
if newPriority < pc.currentPriority { // overflow check
|
||||
newPriority = pc.basePriority
|
||||
}
|
||||
pc.currentPriority = newPriority
|
||||
}
|
||||
|
||||
// ResetPriority resets the priority to the base value.
|
||||
func (pc *PriorityCalculator) ResetPriority() {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
pc.currentPriority = pc.basePriority
|
||||
}
|
||||
|
||||
// SetBasePriority sets a new base priority and resets current priority to match.
|
||||
func (pc *PriorityCalculator) SetBasePriority(priority uint8) {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
pc.basePriority = priority
|
||||
pc.currentPriority = priority
|
||||
}
|
||||
|
||||
// ShouldBecomeMaster determines if the local node should become master
|
||||
// based on priority comparison and IP address tie-breaking.
|
||||
func ShouldBecomeMaster(localPriority, remotePriority uint8, localIP, remoteIP string) bool {
|
||||
if localPriority > remotePriority {
|
||||
return true
|
||||
}
|
||||
|
||||
if localPriority == remotePriority {
|
||||
return localIP > remoteIP
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CalculateMasterDownInterval calculates the master down interval
|
||||
// according to VRRP specification: (3 * Advertisement_Interval).
|
||||
func CalculateMasterDownInterval(advertInt uint8) time.Duration {
|
||||
return time.Duration(3*int(advertInt)) * time.Second
|
||||
}
|
||||
|
||||
// CalculateSkewTime calculates the skew time for master down timer
|
||||
// according to VRRP specification: ((256 - Priority) / 256).
|
||||
func CalculateSkewTime(priority uint8) time.Duration {
|
||||
skew := float64(256-int(priority)) / 256.0
|
||||
return time.Duration(skew * float64(time.Second))
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
@@ -14,10 +15,11 @@ const (
|
||||
)
|
||||
|
||||
type Socket struct {
|
||||
conn *ipv4.RawConn
|
||||
iface *net.Interface
|
||||
localIP net.IP
|
||||
groupIP net.IP
|
||||
conn *ipv4.RawConn
|
||||
packetConn net.PacketConn
|
||||
iface *net.Interface
|
||||
localIP net.IP
|
||||
groupIP net.IP
|
||||
}
|
||||
|
||||
func NewSocket(ifaceName string) (*Socket, error) {
|
||||
@@ -56,9 +58,8 @@ func NewSocket(ifaceName string) (*Socket, error) {
|
||||
}
|
||||
|
||||
file := os.NewFile(uintptr(fd), "vrrp-socket")
|
||||
defer file.Close()
|
||||
|
||||
packetConn, err := net.FilePacketConn(file)
|
||||
file.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create packet connection: %w", err)
|
||||
}
|
||||
@@ -86,10 +87,11 @@ func NewSocket(ifaceName string) (*Socket, error) {
|
||||
}
|
||||
|
||||
return &Socket{
|
||||
conn: rawConn,
|
||||
iface: iface,
|
||||
localIP: localIP,
|
||||
groupIP: groupIP,
|
||||
conn: rawConn,
|
||||
packetConn: packetConn,
|
||||
iface: iface,
|
||||
localIP: localIP,
|
||||
groupIP: groupIP,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -133,6 +135,10 @@ func (s *Socket) Receive() (*VRRPPacket, net.IP, error) {
|
||||
return pkt, header.Src, nil
|
||||
}
|
||||
|
||||
func (s *Socket) SetReadDeadline(t time.Time) error {
|
||||
return s.packetConn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Socket) Close() error {
|
||||
if err := s.conn.LeaveGroup(s.iface, &net.IPAddr{IP: s.groupIP}); err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package vrrp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
import "sync"
|
||||
|
||||
// State represents the VRRP instance state.
|
||||
type State int
|
||||
|
||||
const (
|
||||
@@ -15,6 +12,7 @@ const (
|
||||
StateFault
|
||||
)
|
||||
|
||||
// String returns the string representation of the state.
|
||||
func (s State) String() string {
|
||||
switch s {
|
||||
case StateInit:
|
||||
@@ -30,33 +28,39 @@ func (s State) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// StateMachine manages VRRP state transitions with thread-safe callbacks.
|
||||
type StateMachine struct {
|
||||
currentState State
|
||||
previousState State
|
||||
mu sync.RWMutex
|
||||
currentState State
|
||||
mu sync.RWMutex
|
||||
stateChangeCallbacks []func(old, new State)
|
||||
}
|
||||
|
||||
// NewStateMachine creates a new StateMachine with the specified initial state.
|
||||
func NewStateMachine(initialState State) *StateMachine {
|
||||
return &StateMachine{
|
||||
currentState: initialState,
|
||||
previousState: StateInit,
|
||||
currentState: initialState,
|
||||
stateChangeCallbacks: make([]func(old, new State), 0),
|
||||
}
|
||||
}
|
||||
|
||||
// GetState returns the current state.
|
||||
func (sm *StateMachine) GetState() State {
|
||||
sm.mu.RLock()
|
||||
defer sm.mu.RUnlock()
|
||||
return sm.currentState
|
||||
}
|
||||
|
||||
// SetState transitions to a new state and triggers registered callbacks.
|
||||
func (sm *StateMachine) SetState(newState State) {
|
||||
sm.mu.Lock()
|
||||
oldState := sm.currentState
|
||||
sm.previousState = oldState
|
||||
if oldState == newState {
|
||||
sm.mu.Unlock()
|
||||
return
|
||||
}
|
||||
sm.currentState = newState
|
||||
callbacks := sm.stateChangeCallbacks
|
||||
callbacks := make([]func(old, new State), len(sm.stateChangeCallbacks))
|
||||
copy(callbacks, sm.stateChangeCallbacks)
|
||||
sm.mu.Unlock()
|
||||
|
||||
for _, callback := range callbacks {
|
||||
@@ -64,195 +68,9 @@ func (sm *StateMachine) SetState(newState State) {
|
||||
}
|
||||
}
|
||||
|
||||
// OnStateChange registers a callback to be invoked on state changes.
|
||||
func (sm *StateMachine) OnStateChange(callback func(old, new State)) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
sm.stateChangeCallbacks = append(sm.stateChangeCallbacks, callback)
|
||||
}
|
||||
|
||||
type Timer struct {
|
||||
duration time.Duration
|
||||
timer *time.Timer
|
||||
callback func()
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewTimer(duration time.Duration, callback func()) *Timer {
|
||||
return &Timer{
|
||||
duration: duration,
|
||||
callback: callback,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (t *Timer) Stop() {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
t.timer = nil
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (t *Timer) SetDuration(duration time.Duration) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.duration = duration
|
||||
}
|
||||
|
||||
type PriorityCalculator struct {
|
||||
basePriority uint8
|
||||
currentPriority uint8
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewPriorityCalculator(basePriority uint8) *PriorityCalculator {
|
||||
return &PriorityCalculator{
|
||||
basePriority: basePriority,
|
||||
currentPriority: basePriority,
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PriorityCalculator) GetPriority() uint8 {
|
||||
pc.mu.RLock()
|
||||
defer pc.mu.RUnlock()
|
||||
return pc.currentPriority
|
||||
}
|
||||
|
||||
func (pc *PriorityCalculator) DecreasePriority(amount uint8) {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
|
||||
if pc.currentPriority > amount {
|
||||
pc.currentPriority -= amount
|
||||
} else {
|
||||
pc.currentPriority = 0
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PriorityCalculator) ResetPriority() {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
pc.currentPriority = pc.basePriority
|
||||
}
|
||||
|
||||
func (pc *PriorityCalculator) SetBasePriority(priority uint8) {
|
||||
pc.mu.Lock()
|
||||
defer pc.mu.Unlock()
|
||||
pc.basePriority = priority
|
||||
pc.currentPriority = priority
|
||||
}
|
||||
|
||||
func ShouldBecomeMaster(localPriority, remotePriority uint8, localIP, remoteIP string) bool {
|
||||
if localPriority > remotePriority {
|
||||
return true
|
||||
}
|
||||
|
||||
if localPriority == remotePriority {
|
||||
return localIP > remoteIP
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func CalculateMasterDownInterval(advertInt uint8) time.Duration {
|
||||
return time.Duration(3*int(advertInt)) * time.Second
|
||||
}
|
||||
|
||||
func CalculateSkewTime(priority uint8) time.Duration {
|
||||
skew := float64(256-int(priority)) / 256.0
|
||||
return time.Duration(skew * float64(time.Second))
|
||||
}
|
||||
|
||||
type StateTransition struct {
|
||||
From State
|
||||
To State
|
||||
Timestamp time.Time
|
||||
Reason string
|
||||
}
|
||||
|
||||
type StateHistory struct {
|
||||
transitions []StateTransition
|
||||
maxSize int
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewStateHistory(maxSize int) *StateHistory {
|
||||
return &StateHistory{
|
||||
transitions: make([]StateTransition, 0, maxSize),
|
||||
maxSize: maxSize,
|
||||
}
|
||||
}
|
||||
|
||||
func (sh *StateHistory) Add(from, to State, reason string) {
|
||||
sh.mu.Lock()
|
||||
defer sh.mu.Unlock()
|
||||
|
||||
transition := StateTransition{
|
||||
From: from,
|
||||
To: to,
|
||||
Timestamp: time.Now(),
|
||||
Reason: reason,
|
||||
}
|
||||
|
||||
sh.transitions = append(sh.transitions, transition)
|
||||
|
||||
if len(sh.transitions) > sh.maxSize {
|
||||
sh.transitions = sh.transitions[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func (sh *StateHistory) GetRecent(n int) []StateTransition {
|
||||
sh.mu.RLock()
|
||||
defer sh.mu.RUnlock()
|
||||
|
||||
if n > len(sh.transitions) {
|
||||
n = len(sh.transitions)
|
||||
}
|
||||
|
||||
start := len(sh.transitions) - n
|
||||
result := make([]StateTransition, n)
|
||||
copy(result, sh.transitions[start:])
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (sh *StateHistory) String() string {
|
||||
sh.mu.RLock()
|
||||
defer sh.mu.RUnlock()
|
||||
|
||||
if len(sh.transitions) == 0 {
|
||||
return "No state transitions"
|
||||
}
|
||||
|
||||
result := "State transition history:\n"
|
||||
for _, t := range sh.transitions {
|
||||
result += fmt.Sprintf(" %s: %s -> %s (%s)\n",
|
||||
t.Timestamp.Format("2006-01-02 15:04:05"),
|
||||
t.From, t.To, t.Reason)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
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