go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/impl/memory/module.go (about) 1 // Copyright 2016 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 memory 16 17 import ( 18 "context" 19 20 "go.chromium.org/luci/gae/service/module" 21 ) 22 23 type moduleVersion struct { 24 module, version string 25 } 26 27 type modImpl struct { 28 numInstances map[moduleVersion]int 29 } 30 31 // useMod adds a Module interface to the context 32 func useMod(c context.Context) context.Context { 33 modMap := map[moduleVersion]int{} 34 return module.SetFactory(c, func(ic context.Context) module.RawInterface { 35 return &modImpl{modMap} 36 }) 37 } 38 39 var _ = module.RawInterface((*modImpl)(nil)) 40 41 func (mod *modImpl) List() ([]string, error) { 42 return []string{"testModule1", "testModule2"}, nil 43 } 44 45 func (mod *modImpl) NumInstances(module, version string) (int, error) { 46 if ret, ok := mod.numInstances[moduleVersion{module, version}]; ok { 47 return ret, nil 48 } 49 return 1, nil 50 } 51 52 func (mod *modImpl) SetNumInstances(module, version string, instances int) error { 53 mod.numInstances[moduleVersion{module, version}] = instances 54 return nil 55 } 56 57 func (mod *modImpl) Versions(module string) ([]string, error) { 58 return []string{"testVersion1", "testVersion2"}, nil 59 } 60 61 func (mod *modImpl) DefaultVersion(module string) (string, error) { 62 return "testVersion1", nil 63 } 64 65 func (mod *modImpl) Start(module, version string) error { return nil } 66 func (mod *modImpl) Stop(module, version string) error { return nil }