MCPcopy
hub / github.com/PatchMon/PatchMon / CalculateReportOffset

Function CalculateReportOffset

agent-source-code/internal/utils/offset.go:20–35  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

18// The same api_id will always produce the same offset, ensuring consistency
19// across service restarts.
20func 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

Callers

nothing calls this directly

Calls 1

hashStringFunction · 0.70

Tested by

no test coverage detected