Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package config | |
| 6 | |
| 7 import ( | |
| 8 "context" | |
| 9 "time" | |
| 10 | |
| 11 "github.com/luci/luci-go/common/data/caching/proccache" | |
| 12 "github.com/luci/luci-go/common/sync/mutexpool" | |
| 13 "github.com/luci/luci-go/luci_config/common/cfgtypes" | |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient" | |
| 15 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | |
| 16 | |
| 17 "github.com/golang/protobuf/proto" | |
| 18 ) | |
| 19 | |
| 20 // ProcCache is an in-memory configuration cache. Unlike the "proccache" config | |
| 21 // Backend, this stores the unmarshaled configuration object in-memory. | |
| 22 type ProcCache struct { | |
| 23 // Lifetime is the lifetime of the cached object. | |
|
iannucci
2017/01/21 00:27:05
of each cached object? This can cache more than on
dnj
2017/01/21 01:53:06
Yeah this is just the cache. Really, behind the sc
| |
| 24 Lifetime time.Duration | |
| 25 | |
| 26 mutexes mutexpool.P | |
| 27 } | |
| 28 | |
| 29 // GetTextProto returns an unmarshalled configuration service text protobuf | |
| 30 // message. | |
| 31 // | |
| 32 // If the message is not currently in the process cache, it will be fetched from | |
| 33 // the config service and cached prior to being returned. | |
| 34 func (pc *ProcCache) GetTextProto(c context.Context, cset cfgtypes.ConfigSet, pa th string, msg proto.Message) ( | |
| 35 proto.Message, error) { | |
| 36 | |
| 37 key := procCacheKey{cset, path} | |
| 38 | |
| 39 // Load the value from our cache. First, though, take out a lock on this | |
| 40 // specific config key. This will prevent multiple concurrent accesses f rom | |
| 41 // slamming the config service, particularly at startup. | |
| 42 var v interface{} | |
| 43 var err error | |
| 44 pc.mutexes.WithMutex(key, func() { | |
| 45 v, err = proccache.GetOrMake(c, key, func() (interface{}, time.D uration, error) { | |
| 46 // Not in cache or expired. Reload... | |
| 47 if err := cfgclient.Get(c, cfgclient.AsService, cset, pa th, textproto.Message(msg), nil); err != nil { | |
| 48 return nil, 0, err | |
| 49 } | |
| 50 return msg, pc.Lifetime, nil | |
| 51 }) | |
| 52 }) | |
| 53 | |
| 54 if err != nil { | |
| 55 return nil, err | |
| 56 } | |
| 57 return v.(proto.Message), nil | |
| 58 } | |
| 59 | |
| 60 type procCacheKey struct { | |
| 61 cset cfgtypes.ConfigSet | |
|
iannucci
2017/01/21 00:27:05
this is a `string` underlying type, right?
dnj
2017/01/21 01:53:06
Yep
| |
| 62 path string | |
| 63 } | |
| OLD | NEW |