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

Unified Diff: common/lazyslot/lazyslot.go

Issue 1748933002: Retry RPC deadlines when fetching GAE settings. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@simplify-lazyslot
Patch Set: move retry to settings.go 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | server/settings/settings.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/lazyslot/lazyslot.go
diff --git a/common/lazyslot/lazyslot.go b/common/lazyslot/lazyslot.go
index 9cf59cee00592eb06abddff788c944c616bb7a43..12ba78656dd3f75d3115cd0c69e6878cc063ccdc 100644
--- a/common/lazyslot/lazyslot.go
+++ b/common/lazyslot/lazyslot.go
@@ -39,7 +39,7 @@ type Fetcher func(c context.Context, prev Value) (Value, error)
// stale copy of the value during the refresh.
type Slot struct {
Fetcher Fetcher // used to actually load the value on demand
- Timeout time.Duration // how long to allow to fetch, 5 sec by default.
+ Timeout time.Duration // how long to allow to fetch, 15 sec by default.
lock sync.Mutex // protects the guts below
current *Value // currently known value or nil if not fetched
@@ -80,7 +80,7 @@ func (s *Slot) Get(c context.Context) (result Value, err error) {
// there's nothing to return yet. All goroutines would have to wait for this
// initial fetch to complete. They'll all block on s.lock.Lock() above.
if s.current == nil {
- result, err = doFetch(c, s.Fetcher, Value{})
+ result, err = doFetch(s.makeFetcherCtx(c), s.Fetcher, Value{})
if err == nil {
s.current = &result
}
@@ -98,11 +98,7 @@ func (s *Slot) Get(c context.Context) (result Value, err error) {
// No one is fetching the value now, we should do it. Prepare a new context
// that will be used to do the fetch once lock is released.
- timeout := 5 * time.Second
- if s.Timeout != 0 {
- timeout = s.Timeout
- }
- s.currentFetcherCtx, _ = context.WithTimeout(c, timeout)
+ s.currentFetcherCtx = s.makeFetcherCtx(c)
// Copy lock-protected guts into local variables before releasing the lock.
state.C = s.currentFetcherCtx
@@ -129,6 +125,18 @@ func (s *Slot) Get(c context.Context) (result Value, err error) {
}()
}
+// makeFetcherCtx prepares a context to use for fetch operation.
+//
+// Must be called under the lock.
+func (s *Slot) makeFetcherCtx(c context.Context) context.Context {
+ timeout := 15 * time.Second
+ if s.Timeout != 0 {
+ timeout = s.Timeout
+ }
+ fetcherCtx, _ := clock.WithTimeout(c, timeout)
+ return fetcherCtx
+}
+
// doFetch calls fetcher callback and validates return value.
func doFetch(ctx context.Context, cb Fetcher, prev Value) (result Value, err error) {
result, err = cb(ctx, prev)
« no previous file with comments | « no previous file | server/settings/settings.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698