CalculateReportOffset calculates a unique, deterministic offset for report timing based on the agent's api_id and the reporting interval. This ensures different agents report at staggered times to prevent overwhelming the server. For intervals >= 60 minutes: returns offset in minutes (0-59) For int
(apiID string, intervalMinutes int)
| 18 | // The same api_id will always produce the same offset, ensuring consistency |
| 19 | // across service restarts. |
| 20 | func CalculateReportOffset(apiID string, intervalMinutes int) time.Duration { |
| 21 | // Hash the api_id to get a consistent numeric value |
| 22 | hash := hashString(apiID) |
| 23 | |
| 24 | if intervalMinutes >= 60 { |
| 25 | // For hourly or longer intervals, offset in minutes (0-59) |
| 26 | // Example: api_id hash % 60 = 10 -> reports at :10 past each hour |
| 27 | offsetMinutes := hash % 60 |
| 28 | return time.Duration(offsetMinutes) * time.Minute |
| 29 | } |
| 30 | // For sub-hourly intervals, offset in seconds |
| 31 | // Example: 5-minute interval, hash % 300 = 7 -> reports at :07, :12, :17, etc. |
| 32 | maxOffsetSeconds := intervalMinutes * 60 |
| 33 | offsetSeconds := hash % uint64(maxOffsetSeconds) |
| 34 | return time.Duration(offsetSeconds) * time.Second |
| 35 | } |
| 36 | |
| 37 | // hashString creates a deterministic hash from a string using FNV-1a algorithm |
| 38 | // This ensures the same input always produces the same hash value |
nothing calls this directly
no test coverage detected