oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build hostedlive
2
3package main
4
5import (
6 "context"
7 "flag"
8 "reflect"
9 "time"
10
11 "github.com/aws/smithy-go/middleware"
12)
13
14// maxPages bounds every paged list.
15const maxPages = 100
16
17func tooManyPages() *failure {
18 return &failure{exit: exitOther, Kind: "other", Message: "more than 100 pages; stopped reading"}
19}
20
21// failedState is a wait that reached a state it cannot leave for the one
22// it waits for (exit 6, the state as the code).
23func failedState(what, state string) *failure {
24 return &failure{exit: exitFailed, Kind: "failed_state", Code: state, Message: what + " reached a failed state"}
25}
26
27// wait holds a wait's -timeout and -interval.
28type wait struct{ timeout, interval *time.Duration }
29
30func waitFlags(fs *flag.FlagSet, def time.Duration) wait {
31 return wait{
32 timeout: fs.Duration("timeout", def, "give up after this long (at most 60m)"),
33 interval: fs.Duration("interval", defaultInterval, "time between reads (at least 1s)"),
34 }
35}
36
37// poll calls check until it reports done, returns an error, or the
38// timeout passes (exit 5). The first read is at once; reads are spaced
39// by the interval. The timeout is held two ways: on the wait's clock,
40// checked before each read and again after it, so an answer that comes
41// back after the deadline is never taken as success; and as a deadline on
42// e.ctx for the reads, so a call that stalls ends with the wait (each
43// call also has callTimeout of its own).
44func (w wait) poll(e *env, what string, check func() (bool, error)) error {
45 timeout, interval := *w.timeout, *w.interval
46 if timeout <= 0 || timeout > maxWait {
47 return usage("-timeout must be above 0 and at most %v", maxWait)
48 }
49 if interval < time.Second {
50 return usage("-interval must be at least 1s")
51 }
52 ranOut := &failure{exit: exitTimeout, Kind: "timeout", Message: "wait for " + what + " ran out after " + timeout.String()}
53 outer := e.ctx
54 ctx, cancel := context.WithTimeout(outer, timeout)
55 defer cancel()
56 e.ctx = ctx
57 defer func() { e.ctx = outer }()
58 deadline := e.clock.Now().Add(timeout)
59 for {
60 if !e.clock.Now().Before(deadline) || ctx.Err() != nil {
61 return ranOut
62 }
63 done, err := check()
64 if !e.clock.Now().Before(deadline) || ctx.Err() != nil {
65 return ranOut
66 }
67 if done || err != nil {
68 return err
69 }
70 e.clock.Sleep(min(interval, deadline.Sub(e.clock.Now())))
71 }
72}
73
74var metadataType = reflect.TypeOf(middleware.Metadata{})
75
76// metadataOf returns the ResultMetadata field of an SDK output struct.
77func metadataOf(v any) (middleware.Metadata, bool) {
78 rv := reflect.ValueOf(v)
79 if rv.Kind() == reflect.Pointer {
80 if rv.IsNil() {
81 return middleware.Metadata{}, false
82 }
83 rv = rv.Elem()
84 }
85 if rv.Kind() != reflect.Struct {
86 return middleware.Metadata{}, false
87 }
88 f := rv.FieldByName("ResultMetadata")
89 if !f.IsValid() || f.Type() != metadataType {
90 return middleware.Metadata{}, false
91 }
92 return f.Interface().(middleware.Metadata), true
93}
94