mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
module.go
113 lines (98 loc) · 3.31 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redisconn
import (
"context"
"flag"
"fmt"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/tsmon"
"go.chromium.org/luci/server/caching"
"go.chromium.org/luci/server/module"
)
// ModuleOptions contain configuration of the Redis server module.
type ModuleOptions struct {
RedisAddr string // Redis server to connect to as "host:port"
RedisDB int // index of a logical Redis DB to use by default
}
// Register registers the command line flags.
func (o *ModuleOptions) Register(f *flag.FlagSet) {
f.StringVar(
&o.RedisAddr,
"redis-addr",
o.RedisAddr,
`Redis server to connect to as "host:port" (optional, Redis calls won't work if not given)`,
)
f.IntVar(
&o.RedisDB,
"redis-db",
o.RedisDB,
fmt.Sprintf("Index of a logical Redis DB to use by default (default is %d)", o.RedisDB),
)
}
// NewModule returns a server module that adds a Redis connection pool to the
// global server context and installs Redis as the default caching.BlobCache
// implementation.
//
// The Redis connection pool can be used through redisconn.Get(ctx).
//
// Does nothing if RedisAddr options is unset. In this case redisconn.Get(ctx)
// returns ErrNotConfigured.
func NewModule(opts *ModuleOptions) module.Module {
if opts == nil {
opts = &ModuleOptions{}
}
return &redisModule{opts: opts}
}
// NewModuleFromFlags is a variant of NewModule that initializes options through
// command line flags.
//
// Calling this function registers flags in flag.CommandLine. They are usually
// parsed in server.Main(...).
func NewModuleFromFlags() module.Module {
opts := &ModuleOptions{}
opts.Register(flag.CommandLine)
return NewModule(opts)
}
// redisModule implements module.Module.
type redisModule struct {
opts *ModuleOptions
}
// Name is part of module.Module interface.
func (*redisModule) Name() string {
return "go.chromium.org/luci/server/redisconn"
}
// Initialize is part of module.Module interface.
func (m *redisModule) Initialize(ctx context.Context, host module.Host, opts module.HostOptions) (context.Context, error) {
if m.opts.RedisAddr == "" {
return ctx, nil
}
pool := NewPool(m.opts.RedisAddr, m.opts.RedisDB)
ctx = UsePool(ctx, pool)
// Use Redis as caching.BlobCache provider.
ctx = caching.WithGlobalCache(ctx, func(namespace string) caching.BlobCache {
return &redisBlobCache{Prefix: fmt.Sprintf("luci.blobcache.%s:", namespace)}
})
// Close all connections when exiting gracefully.
host.RegisterCleanup(func(ctx context.Context) {
if err := pool.Close(); err != nil {
logging.Warningf(ctx, "Failed to close Redis pool - %s", err)
}
})
// Populate pool metrics on tsmon flush.
tsmon.RegisterCallbackIn(ctx, func(ctx context.Context) {
ReportStats(ctx, pool, "default")
})
return ctx, nil
}