utils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package utils
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "net"
  6. "runtime"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/gravitl/netmaker/models"
  11. )
  12. // RetryStrategy specifies a strategy to retry an operation after waiting a while,
  13. // with hooks for successful and unsuccessful (>=max) tries.
  14. type RetryStrategy struct {
  15. Wait func(time.Duration)
  16. WaitTime time.Duration
  17. WaitTimeIncrease time.Duration
  18. MaxTries int
  19. Try func() error
  20. OnMaxTries func()
  21. OnSuccess func()
  22. }
  23. // DoStrategy does the retry strategy specified in the struct, waiting before retrying an operator,
  24. // up to a max number of tries, and if executes a success "finalizer" operation if a retry is successful
  25. func (rs RetryStrategy) DoStrategy() {
  26. err := rs.Try()
  27. if err == nil {
  28. rs.OnSuccess()
  29. return
  30. }
  31. tries := 1
  32. for {
  33. if tries >= rs.MaxTries {
  34. rs.OnMaxTries()
  35. return
  36. }
  37. rs.Wait(rs.WaitTime)
  38. if err := rs.Try(); err != nil {
  39. tries++ // we tried, increase count
  40. rs.WaitTime += rs.WaitTimeIncrease // for the next time, sleep more
  41. continue // retry
  42. }
  43. rs.OnSuccess()
  44. return
  45. }
  46. }
  47. func TraceCaller() {
  48. // Skip 1 frame to get the caller of this function
  49. pc, file, line, ok := runtime.Caller(2)
  50. if !ok {
  51. slog.Debug("Unable to get caller information")
  52. return
  53. }
  54. tracePc, _, _, ok := runtime.Caller(1)
  55. if !ok {
  56. slog.Debug("Unable to get caller information")
  57. return
  58. }
  59. traceFuncName := runtime.FuncForPC(tracePc).Name()
  60. // Get function name from the program counter (pc)
  61. funcName := runtime.FuncForPC(pc).Name()
  62. // Print trace details
  63. slog.Debug("## TRACE -> Called from function: ", "tracing-func-name", traceFuncName, "caller-func-name", funcName)
  64. slog.Debug("## TRACE -> Caller File Info", "file", file, "line-no", line)
  65. }
  66. // NoEmptyStringToCsv takes a bunch of strings, filters out empty ones and returns a csv version of the string
  67. func NoEmptyStringToCsv(strs ...string) string {
  68. var sb strings.Builder
  69. for _, str := range strs {
  70. trimmedStr := strings.TrimSpace(str)
  71. if trimmedStr != "" && trimmedStr != "<nil>" {
  72. if sb.Len() > 0 {
  73. sb.WriteString(", ")
  74. }
  75. sb.WriteString(str)
  76. }
  77. }
  78. return sb.String()
  79. }
  80. // GetExtClientEndpoint returns the external client endpoint in the format "host:port" or "[host]:port" for IPv6
  81. func GetExtClientEndpoint(hostIpv4Endpoint, hostIpv6Endpoint net.IP, hostListenPort int) string {
  82. if hostIpv4Endpoint.To4() == nil {
  83. return fmt.Sprintf("[%s]:%d", hostIpv6Endpoint.String(), hostListenPort)
  84. } else {
  85. return fmt.Sprintf("%s:%d", hostIpv4Endpoint.String(), hostListenPort)
  86. }
  87. }
  88. // SortIfacesByName sorts a slice of Iface by name in ascending order
  89. func SortIfacesByName(ifaces []models.Iface) {
  90. sort.Slice(ifaces, func(i, j int) bool {
  91. return ifaces[i].Name < ifaces[j].Name
  92. })
  93. }
  94. // CompareIfaces compares two slices of Iface and returns true if they are equal
  95. // Two slices are considered equal if they have the same length and all corresponding
  96. // elements have the same Name, AddressString, and IP address
  97. func CompareIfaces(ifaces1, ifaces2 []models.Iface) bool {
  98. // Check if lengths are different
  99. if len(ifaces1) != len(ifaces2) {
  100. return false
  101. }
  102. // Compare each element
  103. for i := range ifaces1 {
  104. if !CompareIface(ifaces1[i], ifaces2[i]) {
  105. return false
  106. }
  107. }
  108. return true
  109. }
  110. // CompareIface compares two individual Iface structs and returns true if they are equal
  111. func CompareIface(iface1, iface2 models.Iface) bool {
  112. // Compare Name
  113. if iface1.Name != iface2.Name {
  114. return false
  115. }
  116. // Compare AddressString
  117. if iface1.AddressString != iface2.AddressString {
  118. return false
  119. }
  120. // Compare IP addresses
  121. if !iface1.Address.IP.Equal(iface2.Address.IP) {
  122. return false
  123. }
  124. // Compare network masks
  125. if iface1.Address.Mask.String() != iface2.Address.Mask.String() {
  126. return false
  127. }
  128. return true
  129. }