go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/quota/module.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package quota 16 17 import ( 18 "context" 19 "flag" 20 21 "github.com/gomodule/redigo/redis" 22 23 "go.chromium.org/luci/common/errors" 24 "go.chromium.org/luci/common/logging" 25 26 "go.chromium.org/luci/server/module" 27 "go.chromium.org/luci/server/quota/quotapb" 28 "go.chromium.org/luci/server/redisconn" 29 ) 30 31 // ModuleName is the globally-unique name for this module. 32 // Useful for registering this module as a dependency of other modules. 33 var ModuleName = module.RegisterName("go.chromium.org/luci/server/quota") 34 35 // Ensure quotaModule implements server.Module at compile-time. 36 var _ module.Module = "aModule{} 37 38 // quotaModule implements module.Module. 39 type quotaModule struct { 40 opts *ModuleOptions 41 } 42 43 var stateKey = "holds a *quotaModuleState" 44 45 type quotaModuleState struct { 46 pool *redis.Pool 47 } 48 49 // Dependencies returns required and optional dependencies for this module. 50 // Implements module.Module. 51 func (*quotaModule) Dependencies() []module.Dependency { 52 return []module.Dependency{ 53 module.RequiredDependency(redisconn.ModuleName), 54 } 55 } 56 57 func withRedisConn(ctx context.Context, cb func(redis.Conn) error) (err error) { 58 conn, err := redisconn.Get(ctx) 59 if err != nil { 60 err = errors.Annotate(err, "quota: unable to get redis connection").Err() 61 return 62 } 63 defer func() { 64 if err := conn.Close(); err != nil { 65 logging.Errorf(ctx, "quota: unable to close redis connection: %s", err) 66 } 67 }() 68 return cb(conn) 69 } 70 71 // Initialize initializes this module by setting ModuleOptions in the context 72 // and optionally registering the admin service. 73 // 74 // Implements module.Module. 75 func (m *quotaModule) Initialize(ctx context.Context, host module.Host, opts module.HostOptions) (context.Context, error) { 76 quotapb.RegisterAdminServer(host, "aAdmin{}) 77 return ctx, nil 78 } 79 80 // Name returns the module.Name for this module. 81 // Implements module.Module. 82 func (*quotaModule) Name() module.Name { 83 return ModuleName 84 } 85 86 // ModuleOptions is a set of configuration options for the quota module. 87 type ModuleOptions struct { 88 // TODO(iannucci): add option to select alternate database? 89 } 90 91 // Register adds command line flags for these module options to the given 92 // *flag.FlagSet. Mutates module options by initializing defaults. 93 func (o *ModuleOptions) Register(f *flag.FlagSet) { 94 } 95 96 // NewModule returns a module.Module for the quota library initialized from the 97 // given *ModuleOptions. 98 func NewModule(opts *ModuleOptions) module.Module { 99 return "aModule{} 100 } 101 102 // NewModuleFromFlags returns a module.Module for the quota library which can be 103 // initialized from command line flags. 104 func NewModuleFromFlags() module.Module { 105 return NewModule(nil) 106 }