go.uber.org/cadence@v1.2.9/workflow/context.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package workflow
    22  
    23  import (
    24  	"go.uber.org/cadence/internal"
    25  )
    26  
    27  // Context is a clone of context.Context with Done() returning Channel instead
    28  // of native channel.
    29  // A Context carries a deadline, a cancellation signal, and other values across
    30  // API boundaries.
    31  //
    32  // Context's methods may be called by multiple goroutines simultaneously.
    33  type Context = internal.Context
    34  
    35  // ErrCanceled is the error returned by Context.Err when the context is canceled.
    36  var ErrCanceled = internal.ErrCanceled
    37  
    38  // ErrDeadlineExceeded is the error returned by Context.Err when the context's
    39  // deadline passes.
    40  var ErrDeadlineExceeded = internal.ErrDeadlineExceeded
    41  
    42  // A CancelFunc tells an operation to abandon its work.
    43  // A CancelFunc does not wait for the work to stop.
    44  // After the first call, subsequent calls to a CancelFunc do nothing.
    45  type CancelFunc = internal.CancelFunc
    46  
    47  // WithCancel returns a copy of parent with a new Done channel. The returned
    48  // context's Done channel is closed when the returned cancel function is called
    49  // or when the parent context's Done channel is closed, whichever happens first.
    50  //
    51  // Canceling this context releases resources associated with it, so code should
    52  // call cancel as soon as the operations running in this Context complete.
    53  func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
    54  	return internal.WithCancel(parent)
    55  }
    56  
    57  // WithValue returns a copy of parent in which the value associated with key is
    58  // val.
    59  //
    60  // Use context Values only for request-scoped data that transits processes and
    61  // APIs, not for passing optional parameters to functions.
    62  func WithValue(parent Context, key interface{}, val interface{}) Context {
    63  	return internal.WithValue(parent, key, val)
    64  }
    65  
    66  // NewDisconnectedContext returns a new context that won't propagate parent's cancellation to the new child context.
    67  // One common use case is to do cleanup work after workflow is cancelled.
    68  //
    69  //	err := workflow.ExecuteActivity(ctx, ActivityFoo).Get(ctx, &activityFooResult)
    70  //	if err != nil && cadence.IsCanceledError(ctx.Err()) {
    71  //	  // activity failed, and workflow context is canceled
    72  //	  disconnectedCtx, _ := workflow.newDisconnectedContext(ctx);
    73  //	  workflow.ExecuteActivity(disconnectedCtx, handleCancellationActivity).Get(disconnectedCtx, nil)
    74  //	  return err // workflow return CanceledError
    75  //	}
    76  func NewDisconnectedContext(parent Context) (ctx Context, cancel CancelFunc) {
    77  	return internal.NewDisconnectedContext(parent)
    78  }