go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/count/mod.go (about) 1 // Copyright 2015 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 count 16 17 import ( 18 "context" 19 20 "go.chromium.org/luci/gae/service/module" 21 ) 22 23 // ModuleCounter is the counter object for the Module service. 24 type ModuleCounter struct { 25 List Entry 26 NumInstances Entry 27 SetNumInstances Entry 28 Versions Entry 29 DefaultVersion Entry 30 Start Entry 31 Stop Entry 32 } 33 34 type modCounter struct { 35 c *ModuleCounter 36 37 mod module.RawInterface 38 } 39 40 var _ module.RawInterface = (*modCounter)(nil) 41 42 func (m *modCounter) List() ([]string, error) { 43 ret, err := m.mod.List() 44 return ret, m.c.List.up(err) 45 } 46 47 func (m *modCounter) NumInstances(mod, ver string) (int, error) { 48 ret, err := m.mod.NumInstances(mod, ver) 49 return ret, m.c.NumInstances.up(err) 50 } 51 52 func (m *modCounter) SetNumInstances(mod, ver string, instances int) error { 53 return m.c.SetNumInstances.up(m.mod.SetNumInstances(mod, ver, instances)) 54 } 55 56 func (m *modCounter) Versions(mod string) ([]string, error) { 57 ret, err := m.mod.Versions(mod) 58 return ret, m.c.Versions.up(err) 59 } 60 61 func (m *modCounter) DefaultVersion(mod string) (string, error) { 62 ret, err := m.mod.DefaultVersion(mod) 63 return ret, m.c.DefaultVersion.up(err) 64 } 65 66 func (m *modCounter) Start(mod, ver string) error { 67 return m.c.Start.up(m.mod.Start(mod, ver)) 68 } 69 70 func (m *modCounter) Stop(mod, ver string) error { 71 return m.c.Stop.up(m.mod.Stop(mod, ver)) 72 } 73 74 // FilterModule installs a counter Module filter in the context. 75 func FilterModule(c context.Context) (context.Context, *ModuleCounter) { 76 state := &ModuleCounter{} 77 return module.AddFilters(c, func(ic context.Context, mod module.RawInterface) module.RawInterface { 78 return &modCounter{state, mod} 79 }), state 80 }