OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package delegation | 5 package delegation |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "strings" | 9 "strings" |
10 "time" | 10 "time" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 cfg := &DelegationConfig{} | 90 cfg := &DelegationConfig{} |
91 switch err := ds.Get(c, cfg); { | 91 switch err := ds.Get(c, cfg); { |
92 case err == ds.ErrNoSuchEntity: | 92 case err == ds.ErrNoSuchEntity: |
93 return cfg, nil | 93 return cfg, nil |
94 case err != nil: | 94 case err != nil: |
95 return nil, errors.WrapTransient(err) | 95 return nil, errors.WrapTransient(err) |
96 } | 96 } |
97 return cfg, nil | 97 return cfg, nil |
98 } | 98 } |
99 | 99 |
100 // DelegationConfigLoader constructs a function that lazy-loads delegation | 100 // DelegationConfigLoader lazy-loads delegation config and keeps it cached in |
101 // config and keeps it cached in memory, refreshing the cached copy each minute. | 101 // memory, refreshing the cached copy each minute. |
102 // | 102 // |
103 // Used as MintDelegationTokenRPC.ConfigLoader implementation in prod. | 103 // Used as MintDelegationTokenRPC.ConfigLoader implementation in prod. |
104 func DelegationConfigLoader() func(context.Context) (*DelegationConfig, error) { | 104 var DelegationConfigLoader = delegationConfigLoader() |
| 105 |
| 106 // delegationConfigLoader constructs a function that lazy-loads delegation |
| 107 // config and keeps it cached in memory, refreshing the cached copy each minute. |
| 108 func delegationConfigLoader() func(context.Context) (*DelegationConfig, error) { |
105 slot := lazyslot.Slot{ | 109 slot := lazyslot.Slot{ |
106 Fetcher: func(c context.Context, prev lazyslot.Value) (lazyslot.
Value, error) { | 110 Fetcher: func(c context.Context, prev lazyslot.Value) (lazyslot.
Value, error) { |
107 newCfg, err := FetchDelegationConfig(c) | 111 newCfg, err := FetchDelegationConfig(c) |
108 if err != nil { | 112 if err != nil { |
109 return lazyslot.Value{}, err | 113 return lazyslot.Value{}, err |
110 } | 114 } |
111 | 115 |
112 // Reuse existing unpacked validated config if the revis
ion didn't change. | 116 // Reuse existing unpacked validated config if the revis
ion didn't change. |
113 prevCfg, _ := prev.Value.(*DelegationConfig) | 117 prevCfg, _ := prev.Value.(*DelegationConfig) |
114 if prevCfg != nil && prevCfg.Revision == newCfg.Revision
{ | 118 if prevCfg != nil && prevCfg.Revision == newCfg.Revision
{ |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 return false, nil | 303 return false, nil |
300 } | 304 } |
301 | 305 |
302 // Rule's allowed targets is superset of requested targets? | 306 // Rule's allowed targets is superset of requested targets? |
303 if !rule.services.IsSuperset(q.Services) { | 307 if !rule.services.IsSuperset(q.Services) { |
304 return false, nil | 308 return false, nil |
305 } | 309 } |
306 | 310 |
307 return true, nil | 311 return true, nil |
308 } | 312 } |
OLD | NEW |