OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package featureBreaker | |
6 | |
7 import ( | |
8 "errors" | |
9 "fmt" | |
10 "runtime" | |
11 "strings" | |
12 "sync" | |
13 ) | |
14 | |
15 // FeatureBreaker is the state-access interface for all Filter* functions in | |
16 // this package. A feature is the Name of some method on the filtered service. | |
17 // So if you had: | |
18 // c, fb := FilterMC(...) | |
19 // mc := gae.GetMC(c) | |
20 // | |
21 // you could do: | |
22 // fb.BreakFeatures(memcache.ErrServerError, "Add", "Set") | |
23 // | |
24 // and then | |
25 // mc.Add(...) and mc.Set(...) | |
26 // | |
27 // would return the error. | |
28 // | |
29 // You may also pass nil as the error for BreakFeatures, and the fake will | |
30 // provide the DefaultError which you passed to the Filter function. | |
31 // | |
32 // This interface can only break features which return errors. | |
33 type FeatureBreaker interface { | |
34 BreakFeatures(err error, feature ...string) | |
35 UnbreakFeatures(feature ...string) | |
36 } | |
37 | |
38 // ErrBrokenFeaturesBroken is returned from RunIfNotBroken when BrokenFeatures | |
39 // itself isn't working correctly. | |
40 var ErrBrokenFeaturesBroken = errors.New("featureBreaker: Unable to retrieve cal
ler information") | |
41 | |
42 type state struct { | |
43 sync.Mutex | |
44 | |
45 broken map[string]error | |
46 | |
47 // defaultError is the default error to return when you call | |
48 // BreakFeatures(nil, ...). If this is unset and the user calls BreakFea
tures | |
49 // with nil, BrokenFeatures will return a generic error. | |
50 defaultError error | |
51 } | |
52 | |
53 func newState(dflt error) *state { | |
54 return &state{ | |
55 broken: map[string]error{}, | |
56 defaultError: dflt, | |
57 } | |
58 } | |
59 | |
60 // BreakFeatures allows you to specify an MCSingleReadWriter function name | |
61 // to cause it to return memcache.ErrServerError. e.g. | |
62 // | |
63 // m.SetBrokenFeatures("Add") | |
64 // | |
65 // would return memcache.ErrServerError. You can reverse this by calling | |
66 // UnbreakFeatures("Add"). | |
67 func (s *state) BreakFeatures(err error, feature ...string) { | |
68 s.Lock() | |
69 defer s.Unlock() | |
70 for _, f := range feature { | |
71 s.broken[f] = err | |
72 } | |
73 } | |
74 | |
75 // UnbreakFeatures is the inverse of BreakFeatures, and will return the named | |
76 // features back to their original functionality. | |
77 func (s *state) UnbreakFeatures(feature ...string) { | |
78 s.Lock() | |
79 defer s.Unlock() | |
80 for _, f := range feature { | |
81 delete(s.broken, f) | |
82 } | |
83 } | |
84 | |
85 func (s *state) run(f func() error) error { | |
86 if s.noBrokenFeatures() { | |
87 return f() | |
88 } | |
89 | |
90 pc, _, _, _ := runtime.Caller(1) | |
91 fullName := runtime.FuncForPC(pc).Name() | |
92 fullNameParts := strings.Split(fullName, ".") | |
93 name := fullNameParts[len(fullNameParts)-1] | |
94 | |
95 s.Lock() | |
96 err, ok := s.broken[name] | |
97 dflt := s.defaultError | |
98 s.Unlock() | |
99 | |
100 if ok { | |
101 if err != nil { | |
102 return err | |
103 } | |
104 if dflt != nil { | |
105 return dflt | |
106 } | |
107 return fmt.Errorf("feature %q is broken", name) | |
108 } | |
109 | |
110 return f() | |
111 } | |
112 | |
113 func (s *state) noBrokenFeatures() bool { | |
114 s.Lock() | |
115 defer s.Unlock() | |
116 return len(s.broken) == 0 | |
117 } | |
OLD | NEW |