Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup dead code and incorrect use of go routine in context pkg #1320

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 9 additions & 95 deletions internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@ func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {

// NewDisconnectedContext returns a new context that won't propagate parent's cancellation to the new child context.
// One common use case is to do cleanup work after workflow is canceled.
// err := workflow.ExecuteActivity(ctx, ActivityFoo).Get(ctx, &activityFooResult)
// if err != nil && temporal.IsCanceledError(ctx.Err()) {
// // activity failed, and workflow context is canceled
// disconnectedCtx, _ := workflow.NewDisconnectedContext(ctx);
// workflow.ExecuteActivity(disconnectedCtx, handleCancellationActivity).Get(disconnectedCtx, nil)
// return err // workflow return CanceledError
// }
//
// err := workflow.ExecuteActivity(ctx, ActivityFoo).Get(ctx, &activityFooResult)
// if err != nil && temporal.IsCanceledError(ctx.Err()) {
// // activity failed, and workflow context is canceled
// disconnectedCtx, _ := workflow.NewDisconnectedContext(ctx);
// workflow.ExecuteActivity(disconnectedCtx, handleCancellationActivity).Get(disconnectedCtx, nil)
// return err // workflow return CanceledError
// }
func NewDisconnectedContext(parent Context) (ctx Context, cancel CancelFunc) {
c := newCancelCtx(parent)
return c, func() { c.cancel(true, ErrCanceled) }
Expand Down Expand Up @@ -236,14 +237,7 @@ func propagateCancel(parent Context, child canceler) {
p.childrenLock.Unlock()
}
} else {
go func() {
s := NewSelector(parent)
s.AddReceive(parent.Done(), func(c ReceiveChannel, more bool) {
child.cancel(false, parent.Err())
})
s.AddReceive(child.Done(), func(c ReceiveChannel, more bool) {})
s.Select(parent)
}()
panic("cancelCtx not found")
}
}

Expand All @@ -255,9 +249,6 @@ func parentCancelCtx(parent Context) (*cancelCtx, bool) {
switch c := parent.(type) {
case *cancelCtx:
return c, true
// TODO: Uncomment once timer story is implemented
//case *timerCtx:
// return c.cancelCtx, true
case *valueCtx:
parent = c.Context
default:
Expand Down Expand Up @@ -345,83 +336,6 @@ func (c *cancelCtx) cancel(removeFromParent bool, err error) {
}
}

// Commented out until workflow time API is exposed.
// WithDeadline returns a copy of the parent context with the deadline adjusted
// to be no later than d. If the parent's deadline is already earlier than d,
// WithDeadline(parent, d) is semantically equivalent to parent. The returned
// context's Done channel is closed when the deadline expires, when the returned
// cancel function is called, or when the parent context's Done channel is
// closed, whichever happens first.
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
//func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
// if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
// // The current deadline is already sooner than the new one.
// return WithCancel(parent)
// }
// c := &timerCtx{
// cancelCtx: newCancelCtx(parent),
// deadline: deadline,
// }
// propagateCancel(parent, c)
// d := deadline.Sub(time.Now())
// if d <= 0 {
// c.cancel(true, DeadlineExceeded) // deadline has already passed
// return c, func() { c.cancel(true, Canceled) }
// }
// if c.err == nil {
// c.timer = time.AfterFunc(d, func() {
// c.cancel(true, DeadlineExceeded)
// })
// }
// return c, func() { c.cancel(true, Canceled) }
//}
//
//// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
//// implement Done and Err. It implements cancel by stopping its timer then
//// delegating to cancelCtx.cancel.
//type timerCtx struct {
// *cancelCtx
// timer *time.Timer // Under cancelCtx.mu.
//
// deadline time.Time
//}
//
//func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
// return c.deadline, true
//}
//
//func (c *timerCtx) String() string {
// return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
//}
//
//func (c *timerCtx) cancel(removeFromParent bool, err error) {
// c.cancelCtx.cancel(false, err)
// if removeFromParent {
// // Remove this timerCtx from its parent cancelCtx's children.
// removeChild(c.cancelCtx.Context, c)
// }
// if c.timer != nil {
// c.timer.Stop()
// c.timer = nil
// }
//}
//
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete:
//
// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
// defer cancel() // releases resources if slowOperation completes before timeout elapses
// return slowOperation(ctx)
// }
//func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
// return WithDeadline(parent, time.Now().Add(timeout))
//}

// WithValue returns a copy of parent in which the value associated with key is
// val.
//
Expand Down
Loading