utils.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Get function name from the program counter (pc)
  55. funcName := runtime.FuncForPC(pc).Name()
  56. // Print trace details
  57. slog.Debug("Called from function: %s\n", "func", funcName)
  58. slog.Debug("File: %s, Line: %d\n", "file", file, "line", line)
  59. }
  60. // NoEmptyStringToCsv takes a bunch of strings, filters out empty ones and returns a csv version of the string
  61. func NoEmptyStringToCsv(strs ...string) string {
  62. var sb strings.Builder
  63. for _, str := range strs {
  64. trimmedStr := strings.TrimSpace(str)
  65. if trimmedStr != "" && trimmedStr != "<nil>" {
  66. if sb.Len() > 0 {
  67. sb.WriteString(", ")
  68. }
  69. sb.WriteString(str)
  70. }
  71. }
  72. return sb.String()
  73. }
  74. // GetExtClientEndpoint returns the external client endpoint in the format "host:port" or "[host]:port" for IPv6
  75. func GetExtClientEndpoint(hostIpv4Endpoint, hostIpv6Endpoint net.IP, hostListenPort int) string {
  76. if hostIpv4Endpoint.To4() == nil {
  77. return fmt.Sprintf("[%s]:%d", hostIpv6Endpoint.String(), hostListenPort)
  78. } else {
  79. return fmt.Sprintf("%s:%d", hostIpv4Endpoint.String(), hostListenPort)
  80. }
  81. }
  82. // SortIfacesByName sorts a slice of Iface by name in ascending order
  83. func SortIfacesByName(ifaces []models.Iface) {
  84. sort.Slice(ifaces, func(i, j int) bool {
  85. return ifaces[i].Name < ifaces[j].Name
  86. })
  87. }
  88. // CompareIfaces compares two slices of Iface and returns true if they are equal
  89. // Two slices are considered equal if they have the same length and all corresponding
  90. // elements have the same Name, AddressString, and IP address
  91. func CompareIfaces(ifaces1, ifaces2 []models.Iface) bool {
  92. // Check if lengths are different
  93. if len(ifaces1) != len(ifaces2) {
  94. return false
  95. }
  96. // Compare each element
  97. for i := range ifaces1 {
  98. if !CompareIface(ifaces1[i], ifaces2[i]) {
  99. return false
  100. }
  101. }
  102. return true
  103. }
  104. // CompareIface compares two individual Iface structs and returns true if they are equal
  105. func CompareIface(iface1, iface2 models.Iface) bool {
  106. // Compare Name
  107. if iface1.Name != iface2.Name {
  108. return false
  109. }
  110. // Compare AddressString
  111. if iface1.AddressString != iface2.AddressString {
  112. return false
  113. }
  114. // Compare IP addresses
  115. if !iface1.Address.IP.Equal(iface2.Address.IP) {
  116. return false
  117. }
  118. // Compare network masks
  119. if iface1.Address.Mask.String() != iface2.Address.Mask.String() {
  120. return false
  121. }
  122. return true
  123. }