7
7
"time"
8
8
9
9
"github.com/benbjohnson/clock"
10
+ "github.com/cenkalti/backoff/v4"
10
11
"github.com/cschleiden/go-workflows/backend"
11
12
a "github.com/cschleiden/go-workflows/internal/args"
12
13
"github.com/cschleiden/go-workflows/internal/converter"
@@ -125,13 +126,21 @@ func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow
125
126
timeout = time .Second * 20
126
127
}
127
128
128
- ticker := c .clock .Ticker (time .Second )
129
- defer ticker .Stop ()
129
+ b := backoff.ExponentialBackOff {
130
+ InitialInterval : time .Millisecond * 1 ,
131
+ MaxInterval : time .Second * 1 ,
132
+ Multiplier : 1.5 ,
133
+ RandomizationFactor : 0.5 ,
134
+ MaxElapsedTime : timeout ,
135
+ Stop : backoff .Stop ,
136
+ Clock : c .clock ,
137
+ }
138
+ b .Reset ()
130
139
131
- ctx , cancel := c . clock . WithTimeout ( ctx , timeout )
132
- defer cancel ()
140
+ ticker := backoff . NewTicker ( & b )
141
+ defer ticker . Stop ()
133
142
134
- for {
143
+ for range ticker . C {
135
144
s , err := c .backend .GetWorkflowInstanceState (ctx , instance )
136
145
if err != nil {
137
146
return fmt .Errorf ("getting workflow state: %w" , err )
@@ -140,16 +149,9 @@ func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow
140
149
if s == backend .WorkflowStateFinished {
141
150
return nil
142
151
}
143
-
144
- ticker .Reset (time .Second )
145
- select {
146
- case <- ticker .C :
147
- continue
148
-
149
- case <- ctx .Done ():
150
- return errors .New ("workflow did not finish in specified timeout" )
151
- }
152
152
}
153
+
154
+ return errors .New ("workflow did not finish in specified timeout" )
153
155
}
154
156
155
157
// GetWorkflowResult gets the workflow result for the given workflow result. It first waits for the workflow to finish or until
0 commit comments