package activator import ( "context" "errors" "time" "github.com/carlosframework/platform/internal/hostwrite" ) type probeWindow struct { policy startPolicy opened time.Time ready bool now func() time.Time } func (b *probeWindow) result(err error) error { if !b.policy.Probe || b.ready { return err } now := b.now() if (!b.policy.Deadline.IsZero() && !now.Before(b.policy.Deadline)) || (!b.opened.IsZero() && !now.Before(b.opened.Add(b.policy.Window))) { return errors.Join(ErrAttemptOver, err) } return err } func (b *probeWindow) start(ctx context.Context, journal func(context.Context, time.Time) (time.Time, error)) error { if !b.policy.Probe { return ctx.Err() } if err := b.result(ctx.Err()); err != nil { return err } if journal == nil { return hostwrite.ErrUnavailable } proposed := b.now() opened, err := journal(ctx, proposed) if err != nil { return err } if opened.IsZero() || opened.After(proposed) || (!b.opened.IsZero() && !b.opened.Equal(opened)) { return hostwrite.ErrUnavailable } b.opened = opened return b.result(ctx.Err()) }