Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 settings implements storage for infrequently changing global | 5 // Package settings implements storage for infrequently changing global |
| 6 // settings. | 6 // settings. |
| 7 // | 7 // |
| 8 // Settings are represented as (key, value) pairs, where value is JSON | 8 // Settings are represented as (key, value) pairs, where value is JSON |
| 9 // serializable struct. Settings are cached internally in the process memory to | 9 // serializable struct. Settings are cached internally in the process memory to |
| 10 // avoid hitting the storage all the time. | 10 // avoid hitting the storage all the time. |
| 11 package settings | 11 package settings |
| 12 | 12 |
| 13 import ( | 13 import ( |
| 14 "encoding/json" | 14 "encoding/json" |
| 15 "errors" | 15 "errors" |
| 16 "reflect" | 16 "reflect" |
| 17 "sync" | 17 "sync" |
| 18 "time" | 18 "time" |
| 19 | 19 |
| 20 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
| 21 | 21 |
| 22 "github.com/luci/luci-go/common/clock" | |
| 22 "github.com/luci/luci-go/common/lazyslot" | 23 "github.com/luci/luci-go/common/lazyslot" |
| 24 "github.com/luci/luci-go/common/retry" | |
| 23 ) | 25 ) |
| 24 | 26 |
| 25 var ( | 27 var ( |
| 26 // ErrNoSettings can be returned by Get and Set on fatal errors. | 28 // ErrNoSettings can be returned by Get and Set on fatal errors. |
| 27 ErrNoSettings = errors.New("settings: settings are not available") | 29 ErrNoSettings = errors.New("settings: settings are not available") |
| 28 | 30 |
| 29 // ErrBadType is returned if Get(...) receives unexpected type. | 31 // ErrBadType is returned if Get(...) receives unexpected type. |
| 30 ErrBadType = errors.New("settings: bad type") | 32 ErrBadType = errors.New("settings: bad type") |
| 31 ) | 33 ) |
| 32 | 34 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 storage Storage // used to load and save settings | 111 storage Storage // used to load and save settings |
| 110 values lazyslot.Slot // cached settings | 112 values lazyslot.Slot // cached settings |
| 111 } | 113 } |
| 112 | 114 |
| 113 // New creates new Settings object that uses given Storage to fetch and save | 115 // New creates new Settings object that uses given Storage to fetch and save |
| 114 // settings. | 116 // settings. |
| 115 func New(storage Storage) *Settings { | 117 func New(storage Storage) *Settings { |
| 116 return &Settings{ | 118 return &Settings{ |
| 117 storage: storage, | 119 storage: storage, |
| 118 values: lazyslot.Slot{ | 120 values: lazyslot.Slot{ |
| 121 RetryFactory: retry.TransientOnly(retry.Default), // ret ry transient errors | |
| 122 Timeout: 15 * time.Second, // ret ry for 15 sec at most | |
| 119 Fetcher: func(c context.Context, _ lazyslot.Value) (lazy slot.Value, error) { | 123 Fetcher: func(c context.Context, _ lazyslot.Value) (lazy slot.Value, error) { |
| 124 c, _ = clock.WithTimeout(c, 2*time.Second) // tr igger a retry after 2 sec RPC timeout | |
|
dnj
2016/03/01 02:21:40
WDYT about moving the retries into the Fetcher ins
Vadim Sh.
2016/03/02 22:52:04
Done.
| |
| 120 bundle, err := storage.FetchAllSettings(c) | 125 bundle, err := storage.FetchAllSettings(c) |
| 121 if err != nil { | 126 if err != nil { |
| 122 return lazyslot.Value{}, err | 127 return lazyslot.Value{}, err |
| 123 } | 128 } |
| 124 return lazyslot.Value{ | 129 return lazyslot.Value{ |
| 125 Value: bundle, | 130 Value: bundle, |
| 126 Expiration: bundle.Exp, | 131 Expiration: bundle.Exp, |
| 127 }, nil | 132 }, nil |
| 128 }, | 133 }, |
| 129 }, | 134 }, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 // | 169 // |
| 165 // New settings will apply only when existing in-memory cache expires. | 170 // New settings will apply only when existing in-memory cache expires. |
| 166 // In particular, Get() right after Set() may still return old value. | 171 // In particular, Get() right after Set() may still return old value. |
| 167 func (s *Settings) Set(c context.Context, key string, value interface{}, who, wh y string) error { | 172 func (s *Settings) Set(c context.Context, key string, value interface{}, who, wh y string) error { |
| 168 blob, err := json.Marshal(value) | 173 blob, err := json.Marshal(value) |
| 169 if err != nil { | 174 if err != nil { |
| 170 return err | 175 return err |
| 171 } | 176 } |
| 172 return s.storage.UpdateSetting(c, key, json.RawMessage(blob), who, why) | 177 return s.storage.UpdateSetting(c, key, json.RawMessage(blob), who, why) |
| 173 } | 178 } |
| OLD | NEW |