netmaker/utils/utils.go
Abhishek K e13bf2c0eb
NET-1923: Add Metric Port to server config (#3306)
* set default metrics port 8889

* set default metrics port 51821

* add metrics port to server config

* bind caddy only on tcp

* add var for pulling files

* add new line

* update peer update model

* check if port is not zero

* set replace peer to false on pull

* do not replace peers on failover sync

* remove debug log

* add old peer update fields for backwards compatibility

* add old json tag

* add debug log in caller trace func
2025-02-04 08:44:24 +04:00

61 lines
1.5 KiB
Go

package utils
import (
"log/slog"
"runtime"
"time"
)
// RetryStrategy specifies a strategy to retry an operation after waiting a while,
// with hooks for successful and unsuccessful (>=max) tries.
type RetryStrategy struct {
Wait func(time.Duration)
WaitTime time.Duration
WaitTimeIncrease time.Duration
MaxTries int
Try func() error
OnMaxTries func()
OnSuccess func()
}
// DoStrategy does the retry strategy specified in the struct, waiting before retrying an operator,
// up to a max number of tries, and if executes a success "finalizer" operation if a retry is successful
func (rs RetryStrategy) DoStrategy() {
err := rs.Try()
if err == nil {
rs.OnSuccess()
return
}
tries := 1
for {
if tries >= rs.MaxTries {
rs.OnMaxTries()
return
}
rs.Wait(rs.WaitTime)
if err := rs.Try(); err != nil {
tries++ // we tried, increase count
rs.WaitTime += rs.WaitTimeIncrease // for the next time, sleep more
continue // retry
}
rs.OnSuccess()
return
}
}
func TraceCaller() {
// Skip 1 frame to get the caller of this function
pc, file, line, ok := runtime.Caller(2)
if !ok {
slog.Debug("Unable to get caller information")
return
}
// Get function name from the program counter (pc)
funcName := runtime.FuncForPC(pc).Name()
// Print trace details
slog.Debug("Called from function: %s\n", "func-name", funcName)
slog.Debug("File: %s, Line: %d\n", "file", file, "line-no", line)
}