oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runnerproto
2
3import (
4 "context"
5 "errors"
6 "time"
7
8 "amadan.net/oficina/equipo/internal/runner/provider"
9)
10
11var ErrTransient = errors.New("runner_transient")
12var ErrCleanupExpired = errors.New("runner_cleanup_expired")
13var ErrNotClaimed = errors.New("runner_not_claimed")
14var ErrClaimRefused = errors.New("runner_claim_refused")
15
16func RetryAuthority(ctx context.Context, call func() error) error {
17 delay := 200 * time.Millisecond
18 for {
19 if err := ctx.Err(); err != nil {
20 return err
21 }
22 err := call()
23 if !errors.Is(err, ErrTransient) && !errors.Is(err, provider.ErrClosed) {
24 return err
25 }
26 timer := time.NewTimer(delay)
27 select {
28 case <-ctx.Done():
29 timer.Stop()
30 return ctx.Err()
31 case <-timer.C:
32 }
33 if delay < 2*time.Second {
34 delay *= 2
35 }
36 }
37}
38
39func RemoteError(err error) error {
40 if errors.Is(err, provider.ErrUncertain) {
41 return ErrTransient
42 }
43 return err
44}
45
46// RefusalError preserves a coded instance response without response prose.
47type RefusalError struct {
48 Reason string
49 Status int
50 Claim bool
51}
52
53// CoordinatorDefinitive requires an explicit fence code, never just a status.
54func (e *RefusalError) CoordinatorDefinitive() bool {
55 switch e.Reason {
56 case "coordinator.fenced", "coordinator.reset", "coordinator.superseded", "coordinator.revoked":
57 return e.Status >= 400 && e.Status < 500
58 }
59 return false
60}
61
62func (e *RefusalError) Error() string { return e.Reason }
63func (e *RefusalError) Unwrap() error {
64 if e.Status == 401 || e.Status == 403 {
65 return provider.ErrIdentity
66 }
67 if e.Claim {
68 return ErrClaimRefused
69 }
70 return provider.ErrContract
71}
72func RefusalReason(key string) string {
73 switch key {
74 case "coordinator.fenced", "coordinator.reset", "coordinator.superseded", "coordinator.revoked", "runner.budget", "runner.quota", "runner.evidence", "runner.containment", "runner.replay", "runner.uncertain", "form.invalid", "form.conflict", "form.forbidden":
75 return key
76 default:
77 return "form.invalid"
78 }
79}
80
81// A consumed ticket permits one send. Without proof that no frame was written,
82// even a transport failure leaves the action unknown and fully charged.
83func SendAdmittedTurn(ctx context.Context, send func() error, lost func(context.Context) error) error {
84 err := send()
85 if err == nil {
86 return nil
87 }
88 cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
89 defer cancel()
90 return errors.Join(provider.ErrUncertain, err, lost(cleanup))
91}
92