-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context methods to Clock interface
This allows for using timeout/deadline functionality built in to context.Context with a custom clock implementation. Module Go version bumped to 1.19 due to use of atomic.Bool
- Loading branch information
1 parent
e868797
commit c4c9e58
Showing
10 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build go1.21 | ||
|
||
package clocks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (c defaultClock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithDeadlineCause(ctx, t, cause) | ||
} | ||
|
||
func (c defaultClock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithTimeoutCause(ctx, d, cause) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !go1.21 | ||
|
||
package clocks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (c defaultClock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithDeadline(ctx, t) | ||
} | ||
|
||
func (c defaultClock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(ctx, d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//go:build go1.20 | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (f *Clock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
cctx, cancelCause := context.WithCancelCause(ctx) | ||
dctx := &deadlineContext{ | ||
Context: cctx, | ||
deadline: t, | ||
} | ||
dur := f.Until(t) | ||
if dur <= 0 { | ||
dctx.timedOut.CompareAndSwap(false, true) | ||
cancelCause(cause) | ||
return dctx, func() { | ||
cancelCause(context.Canceled) | ||
} | ||
} | ||
stop := f.AfterFunc(dur, func() { | ||
if cctx.Err() == nil { | ||
dctx.timedOut.CompareAndSwap(false, true) | ||
} | ||
cancelCause(cause) | ||
}) | ||
cancel := func() { | ||
cancelCause(context.Canceled) | ||
stop.Stop() | ||
} | ||
return dctx, cancel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//go:build !go1.20 | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (f *Clock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
cctx, cancel := context.WithCancel(ctx) | ||
dctx := &deadlineContext{ | ||
Context: cctx, | ||
deadline: t, | ||
} | ||
dur := f.Until(t) | ||
if dur <= 0 { | ||
dctx.timedOut.CompareAndSwap(false, true) | ||
cancel() | ||
return dctx, func() { | ||
cancel() | ||
} | ||
} | ||
stop := f.AfterFunc(dur, func() { | ||
if cctx.Err() == nil { | ||
dctx.timedOut.CompareAndSwap(false, true) | ||
} | ||
cancel() | ||
}) | ||
cancelStop := func() { | ||
cancel() | ||
stop.Stop() | ||
} | ||
return dctx, cancelStop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/vimeo/go-clocks | ||
|
||
go 1.14 | ||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build go1.21 | ||
|
||
package offset | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (o *Clock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return o.inner.ContextWithDeadlineCause(ctx, t.Add(o.offset), cause) | ||
} | ||
|
||
func (o *Clock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return o.inner.ContextWithTimeoutCause(ctx, d+o.offset, cause) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !go1.21 | ||
|
||
package offset | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (o *Clock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return o.inner.ContextWithDeadline(ctx, t.Add(o.offset)) | ||
} | ||
|
||
func (o *Clock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return o.inner.ContextWithTimeout(ctx, d+o.offset) | ||
} |