carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package activator
2
3import (
4 "context"
5 "errors"
6 "time"
7
8 "github.com/carlosframework/platform/internal/hostwrite"
9)
10
11type probeWindow struct {
12 policy startPolicy
13 opened time.Time
14 ready bool
15 now func() time.Time
16}
17
18func (b *probeWindow) result(err error) error {
19 if !b.policy.Probe || b.ready {
20 return err
21 }
22 now := b.now()
23 if (!b.policy.Deadline.IsZero() && !now.Before(b.policy.Deadline)) ||
24 (!b.opened.IsZero() && !now.Before(b.opened.Add(b.policy.Window))) {
25 return errors.Join(ErrAttemptOver, err)
26 }
27 return err
28}
29
30func (b *probeWindow) start(ctx context.Context, journal func(context.Context, time.Time) (time.Time, error)) error {
31 if !b.policy.Probe {
32 return ctx.Err()
33 }
34 if err := b.result(ctx.Err()); err != nil {
35 return err
36 }
37 if journal == nil {
38 return hostwrite.ErrUnavailable
39 }
40 proposed := b.now()
41 opened, err := journal(ctx, proposed)
42 if err != nil {
43 return err
44 }
45 if opened.IsZero() || opened.After(proposed) || (!b.opened.IsZero() && !b.opened.Equal(opened)) {
46 return hostwrite.ErrUnavailable
47 }
48 b.opened = opened
49 return b.result(ctx.Err())
50}
51