@@ -458,6 +458,20 @@ type (
458458 // NOTE: Experimental
459459 Summary string
460460 }
461+
462+ // AwaitOptions are options set when creating an await.
463+ //
464+ // NOTE: Experimental
465+ AwaitOptions struct {
466+ // Timeout is the await timeout if the await condition is not met.
467+ //
468+ // NOTE: Experimental
469+ Timeout time.Duration
470+ // TimerOptions are options set for the underlying timer created.
471+ //
472+ // NOTE: Experimental
473+ TimerOptions TimerOptions
474+ }
461475)
462476
463477// Await blocks the calling thread until condition() returns true
@@ -485,34 +499,51 @@ func (wc *workflowEnvironmentInterceptor) Await(ctx Context, condition func() bo
485499 return nil
486500}
487501
488- // AwaitWithTimeout blocks the calling thread until condition() returns true
489- // Returns ok equals to false if timed out and err equals to CanceledError if the ctx is canceled.
490- func AwaitWithTimeout (ctx Context , timeout time.Duration , condition func () bool ) (ok bool , err error ) {
491- assertNotInReadOnlyState (ctx )
492- state := getState (ctx )
493- return state .dispatcher .interceptor .AwaitWithTimeout (ctx , timeout , condition )
494- }
495-
496- func (wc * workflowEnvironmentInterceptor ) AwaitWithTimeout (ctx Context , timeout time.Duration , condition func () bool ) (ok bool , err error ) {
502+ func (wc * workflowEnvironmentInterceptor ) awaitWithOptions (ctx Context , options AwaitOptions , condition func () bool , functionName string ) (ok bool , err error ) {
497503 state := getState (ctx )
498504 defer state .unblocked ()
499- timer := NewTimer (ctx , timeout )
505+ timer := NewTimerWithOptions (ctx , options . Timeout , options . TimerOptions )
500506 for ! condition () {
501507 doneCh := ctx .Done ()
502508 // TODO: Consider always returning a channel
503509 if doneCh != nil {
504510 if _ , more := doneCh .ReceiveAsyncWithMoreFlag (nil ); ! more {
505- return false , NewCanceledError ("AwaitWithTimeout context canceled" )
511+ return false , NewCanceledError ("%s context canceled" , functionName )
506512 }
507513 }
508514 if timer .IsReady () {
509515 return false , nil
510516 }
511- state .yield ("AwaitWithTimeout" )
517+ state .yield (functionName )
512518 }
513519 return true , nil
514520}
515521
522+ // AwaitWithTimeout blocks the calling thread until condition() returns true
523+ // Returns ok equals to false if timed out and err equals to CanceledError if the ctx is canceled.
524+ func AwaitWithTimeout (ctx Context , timeout time.Duration , condition func () bool ) (ok bool , err error ) {
525+ assertNotInReadOnlyState (ctx )
526+ state := getState (ctx )
527+ return state .dispatcher .interceptor .AwaitWithTimeout (ctx , timeout , condition )
528+ }
529+
530+ func (wc * workflowEnvironmentInterceptor ) AwaitWithTimeout (ctx Context , timeout time.Duration , condition func () bool ) (ok bool , err error ) {
531+ options := AwaitOptions {Timeout : timeout , TimerOptions : TimerOptions {Summary : "AwaitWithTimeout" }}
532+ return wc .awaitWithOptions (ctx , options , condition , "AwaitWithTimeout" )
533+ }
534+
535+ // AwaitWithOptions blocks the calling thread until condition() returns true
536+ // Returns ok equals to false if timed out and err equals to CanceledError if the ctx is canceled.
537+ func AwaitWithOptions (ctx Context , options AwaitOptions , condition func () bool ) (ok bool , err error ) {
538+ assertNotInReadOnlyState (ctx )
539+ state := getState (ctx )
540+ return state .dispatcher .interceptor .AwaitWithOptions (ctx , options , condition )
541+ }
542+
543+ func (wc * workflowEnvironmentInterceptor ) AwaitWithOptions (ctx Context , options AwaitOptions , condition func () bool ) (ok bool , err error ) {
544+ return wc .awaitWithOptions (ctx , options , condition , "AwaitWithOptions" )
545+ }
546+
516547// NewChannel create new Channel instance
517548func NewChannel (ctx Context ) Channel {
518549 state := getState (ctx )
@@ -1331,7 +1362,7 @@ func Sleep(ctx Context, d time.Duration) (err error) {
13311362}
13321363
13331364func (wc * workflowEnvironmentInterceptor ) Sleep (ctx Context , d time.Duration ) (err error ) {
1334- t := NewTimer (ctx , d )
1365+ t := NewTimerWithOptions (ctx , d , TimerOptions { Summary : "Sleep" } )
13351366 err = t .Get (ctx , nil )
13361367 return
13371368}
0 commit comments