Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: common/clock/external.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Actually upload the patch. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/clock/clockcontext_test.go ('k') | common/clock/external_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package clock 5 package clock
6 6
7 import ( 7 import (
8 "time" 8 "time"
9 9
10 "golang.org/x/net/context" 10 "golang.org/x/net/context"
11 ) 11 )
12 12
13 // Context key for clock.
14 type clockKeyType int
15
16 // Unique value for clock key. 13 // Unique value for clock key.
17 var clockKey clockKeyType 14 var clockKey = "clock.Clock"
18 15
19 // Factory is a generator function that produces a Clock instnace. 16 // Factory is a generator function that produces a Clock instnace.
20 type Factory func(context.Context) Clock 17 type Factory func(context.Context) Clock
21 18
22 // SetFactory creates a new Context using the supplied Clock factory. 19 // SetFactory creates a new Context using the supplied Clock factory.
23 func SetFactory(ctx context.Context, f Factory) context.Context { 20 func SetFactory(ctx context.Context, f Factory) context.Context {
24 » return context.WithValue(ctx, clockKey, f) 21 » return context.WithValue(ctx, &clockKey, f)
25 } 22 }
26 23
27 // Set creates a new Context using the supplied Clock. 24 // Set creates a new Context using the supplied Clock.
28 func Set(ctx context.Context, c Clock) context.Context { 25 func Set(ctx context.Context, c Clock) context.Context {
29 return SetFactory(ctx, func(context.Context) Clock { return c }) 26 return SetFactory(ctx, func(context.Context) Clock { return c })
30 } 27 }
31 28
32 // Get returns the Clock set in the supplied Context, defaulting to 29 // Get returns the Clock set in the supplied Context, defaulting to
33 // SystemClock() if none is set. 30 // SystemClock() if none is set.
34 func Get(ctx context.Context) (clock Clock) { 31 func Get(ctx context.Context) (clock Clock) {
35 » if v := ctx.Value(clockKey); v != nil { 32 » if v := ctx.Value(&clockKey); v != nil {
36 if f, ok := v.(Factory); ok { 33 if f, ok := v.(Factory); ok {
37 clock = f(ctx) 34 clock = f(ctx)
38 } 35 }
39 } 36 }
40 if clock == nil { 37 if clock == nil {
41 clock = GetSystemClock() 38 clock = GetSystemClock()
42 } 39 }
43 return 40 return
44 } 41 }
45 42
46 // Now calls Clock.Now on the Clock instance stored in the supplied Context. 43 // Now calls Clock.Now on the Clock instance stored in the supplied Context.
47 func Now(ctx context.Context) time.Time { 44 func Now(ctx context.Context) time.Time {
48 return Get(ctx).Now() 45 return Get(ctx).Now()
49 } 46 }
50 47
51 // Sleep calls Clock.Sleep on the Clock instance stored in the supplied Context. 48 // Sleep calls Clock.Sleep on the Clock instance stored in the supplied Context.
52 func Sleep(ctx context.Context, d time.Duration) { 49 func Sleep(ctx context.Context, d time.Duration) {
53 » Get(ctx).Sleep(d) 50 » Get(ctx).Sleep(ctx, d)
54 } 51 }
55 52
56 // NewTimer calls Clock.NewTimer on the Clock instance stored in the supplied 53 // NewTimer calls Clock.NewTimer on the Clock instance stored in the supplied
57 // Context. 54 // Context.
58 func NewTimer(ctx context.Context) Timer { 55 func NewTimer(ctx context.Context) Timer {
59 » return Get(ctx).NewTimer() 56 » return Get(ctx).NewTimer(ctx)
60 } 57 }
61 58
62 // After calls Clock.After on the Clock instance stored in the supplied Context. 59 // After calls Clock.After on the Clock instance stored in the supplied Context.
63 func After(ctx context.Context, d time.Duration) <-chan time.Time { 60 func After(ctx context.Context, d time.Duration) <-chan TimerResult {
64 » return Get(ctx).After(d) 61 » return Get(ctx).After(ctx, d)
65 } 62 }
OLDNEW
« no previous file with comments | « common/clock/clockcontext_test.go ('k') | common/clock/external_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698