OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "github.com/luci/gae/service/module" | 8 "github.com/luci/gae/service/module" |
9 "golang.org/x/net/context" | 9 "golang.org/x/net/context" |
10 ) | 10 ) |
11 | 11 |
12 type modContextKeyType int | 12 type modContextKeyType int |
13 | 13 |
14 var modContextKey modContextKeyType | 14 var modContextKey modContextKeyType |
15 | 15 |
16 type moduleVersion struct { | 16 type moduleVersion struct { |
17 module, version string | 17 module, version string |
18 } | 18 } |
19 | 19 |
20 type modImpl struct { | 20 type modImpl struct { |
21 c context.Context | |
22 numInstances map[moduleVersion]int | 21 numInstances map[moduleVersion]int |
23 } | 22 } |
24 | 23 |
25 // useMod adds a Module interface to the context | 24 // useMod adds a Module interface to the context |
26 func useMod(c context.Context) context.Context { | 25 func useMod(c context.Context) context.Context { |
27 » return module.SetFactory(c, func(ic context.Context) module.Interface { | 26 » modMap := map[moduleVersion]int{} |
28 » » return &modImpl{ic, map[moduleVersion]int{}} | 27 » return module.SetFactory(c, func(ic context.Context) module.RawInterface
{ |
| 28 » » return &modImpl{modMap} |
29 }) | 29 }) |
30 } | 30 } |
31 | 31 |
32 var _ = module.Interface((*modImpl)(nil)) | 32 var _ = module.RawInterface((*modImpl)(nil)) |
33 | 33 |
34 func (mod *modImpl) List() ([]string, error) { | 34 func (mod *modImpl) List() ([]string, error) { |
35 return []string{"testModule1", "testModule2"}, nil | 35 return []string{"testModule1", "testModule2"}, nil |
36 } | 36 } |
37 | 37 |
38 func (mod *modImpl) NumInstances(module, version string) (int, error) { | 38 func (mod *modImpl) NumInstances(module, version string) (int, error) { |
39 if ret, ok := mod.numInstances[moduleVersion{module, version}]; ok { | 39 if ret, ok := mod.numInstances[moduleVersion{module, version}]; ok { |
40 return ret, nil | 40 return ret, nil |
41 } | 41 } |
42 return 1, nil | 42 return 1, nil |
43 } | 43 } |
44 | 44 |
45 func (mod *modImpl) SetNumInstances(module, version string, instances int) error
{ | 45 func (mod *modImpl) SetNumInstances(module, version string, instances int) error
{ |
46 mod.numInstances[moduleVersion{module, version}] = instances | 46 mod.numInstances[moduleVersion{module, version}] = instances |
47 return nil | 47 return nil |
48 } | 48 } |
49 | 49 |
50 func (mod *modImpl) Versions(module string) ([]string, error) { | 50 func (mod *modImpl) Versions(module string) ([]string, error) { |
51 return []string{"testVersion1", "testVersion2"}, nil | 51 return []string{"testVersion1", "testVersion2"}, nil |
52 } | 52 } |
53 | 53 |
54 func (mod *modImpl) DefaultVersion(module string) (string, error) { | 54 func (mod *modImpl) DefaultVersion(module string) (string, error) { |
55 return "testVersion1", nil | 55 return "testVersion1", nil |
56 } | 56 } |
57 | 57 |
58 func (mod *modImpl) Start(module, version string) error { | 58 func (mod *modImpl) Start(module, version string) error { return nil } |
59 » return nil | 59 func (mod *modImpl) Stop(module, version string) error { return nil } |
60 } | |
61 | |
62 func (mod *modImpl) Stop(module, version string) error { | |
63 » return nil | |
64 } | |
OLD | NEW |