MCPcopy Create free account
hub / github.com/cortexproject/cortex / NewMemcached

Function NewMemcached

pkg/chunk/cache/memcached.go:53–104  ·  view source on GitHub ↗

NewMemcached makes a new Memcached.

(cfg MemcachedConfig, client MemcachedClient, name string, reg prometheus.Registerer, logger log.Logger)

Source from the content-addressed store, hash-verified

51
52// NewMemcached makes a new Memcached.
53func NewMemcached(cfg MemcachedConfig, client MemcachedClient, name string, reg prometheus.Registerer, logger log.Logger) *Memcached {
54 c := &Memcached{
55 cfg: cfg,
56 memcache: client,
57 name: name,
58 logger: logger,
59 requestDuration: instr.NewHistogramCollector(
60 promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{
61 Namespace: "cortex",
62 Name: "memcache_request_duration_seconds",
63 Help: "Total time spent in seconds doing memcache requests.",
64 // Memcached requests are very quick: smallest bucket is 16us, biggest is 1s
65 Buckets: prometheus.ExponentialBuckets(0.000016, 4, 8),
66 ConstLabels: prometheus.Labels{"name": name},
67 }, []string{"method", "status_code"}),
68 ),
69 }
70
71 if cfg.BatchSize == 0 || cfg.Parallelism == 0 {
72 return c
73 }
74
75 c.inputCh = make(chan *work)
76 c.quit = make(chan struct{})
77 c.wg.Add(cfg.Parallelism)
78
79 for i := 0; i < cfg.Parallelism; i++ {
80 go func() {
81 defer c.wg.Done()
82 for {
83 select {
84 case <-c.quit:
85 return
86 case input := <-c.inputCh:
87 res := &result{
88 batchID: input.batchID,
89 }
90 res.found, res.bufs, res.missed = c.fetch(input.ctx, input.keys)
91 // No-one will be reading from resultCh if we were asked to quit
92 // during the fetch, so check again before writing to it.
93 select {
94 case <-c.quit:
95 return
96 case input.resultCh <- res:
97 }
98 }
99 }
100 }()
101 }
102
103 return c
104}
105
106type work struct {
107 keys []string

Callers 4

TestMemcachedFunction · 0.92
TestMemcacheFailureFunction · 0.92
TestMemcacheStopFunction · 0.92
NewFunction · 0.70

Calls 3

fetchMethod · 0.95
DoneMethod · 0.80
AddMethod · 0.45

Tested by 3

TestMemcachedFunction · 0.74
TestMemcacheFailureFunction · 0.74
TestMemcacheStopFunction · 0.74