package governance import ( "encoding/json" "errors" "time" ) // V1 per-source refresh schedule. The schedule JSONB lives on the ACTIVE // revision; next_refresh_at on source_connections is ALWAYS derived from it by // the server (clients can never push an arbitrary due time). const MinRefreshInterval = 5 * time.Minute type SourceSchedule struct { RefreshIntervalSeconds *int `json:"refresh_interval_seconds,omitempty"` } // ParseSourceSchedule accepts the stored JSON and normalises it. Missing, // zero, or sub-5-minute values are rejected as "not configured" (interval 0, // no error) rather than silently accepted: a source that could stampede the // portal every few seconds is worse than a manual-only source. func ParseSourceSchedule(raw []byte) (time.Duration, error) { if len(raw) == 0 || string(raw) == "{}" { return 0, nil } var schedule SourceSchedule if err := json.Unmarshal(raw, &schedule); err != nil { return 0, errors.New("invalid source schedule") } if schedule.RefreshIntervalSeconds == nil || *schedule.RefreshIntervalSeconds == 0 { return 0, nil } if *schedule.RefreshIntervalSeconds < 0 { return 0, errors.New("invalid source schedule") } interval := time.Duration(*schedule.RefreshIntervalSeconds) * time.Second if interval < MinRefreshInterval { return 0, errors.New("source refresh interval must be at least five minutes") } return interval, nil }