| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Package count contains 'counter' filters for all the gae services. This | 5 // Package count contains 'counter' filters for all the gae services. This |
| 6 // serves as a set of simple example filters, and also enables other filters | 6 // serves as a set of simple example filters, and also enables other filters |
| 7 // to test to see if certain underlying APIs are called when they should be | 7 // to test to see if certain underlying APIs are called when they should be |
| 8 // (e.g. for the datastore mcache filter, for example). | 8 // (e.g. for the datastore mcache filter, for example). |
| 9 package count | 9 package count |
| 10 | 10 |
| 11 import ( | 11 import ( |
| 12 "fmt" |
| 12 "sync/atomic" | 13 "sync/atomic" |
| 13 ) | 14 ) |
| 14 | 15 |
| 16 type counter struct { |
| 17 value int32 |
| 18 } |
| 19 |
| 20 func (c *counter) increment() { |
| 21 atomic.AddInt32(&c.value, 1) |
| 22 } |
| 23 |
| 24 func (c *counter) get() int { |
| 25 return int(atomic.LoadInt32(&c.value)) |
| 26 } |
| 27 |
| 15 // Entry is a success/fail pair for a single API method. It's returned | 28 // Entry is a success/fail pair for a single API method. It's returned |
| 16 // by the Counter interface. | 29 // by the Counter interface. |
| 17 type Entry struct { | 30 type Entry struct { |
| 18 » Successes int64 | 31 » successes counter |
| 19 » Errors int64 | 32 » errors counter |
| 33 } |
| 34 |
| 35 func (e *Entry) String() string { |
| 36 » return fmt.Sprintf("{Successes:%d, Errors:%d}", e.Successes(), e.Errors(
)) |
| 20 } | 37 } |
| 21 | 38 |
| 22 // Total is a convenience function for getting the total number of calls to | 39 // Total is a convenience function for getting the total number of calls to |
| 23 // this API. It's Successes+Errors. | 40 // this API. It's Successes+Errors. |
| 24 func (e Entry) Total() int64 { return e.Successes + e.Errors } | 41 func (e *Entry) Total() int64 { return int64(e.Successes()) + int64(e.Errors())
} |
| 42 |
| 43 func (e *Entry) Successes() int { |
| 44 » return e.successes.get() |
| 45 } |
| 46 |
| 47 func (e *Entry) Errors() int { |
| 48 » return e.errors.get() |
| 49 } |
| 25 | 50 |
| 26 func (e *Entry) up(errs ...error) error { | 51 func (e *Entry) up(errs ...error) error { |
| 27 err := error(nil) | 52 err := error(nil) |
| 28 if len(errs) > 0 { | 53 if len(errs) > 0 { |
| 29 err = errs[0] | 54 err = errs[0] |
| 30 } | 55 } |
| 31 if err == nil { | 56 if err == nil { |
| 32 » » atomic.AddInt64(&e.Successes, 1) | 57 » » e.successes.increment() |
| 33 } else { | 58 } else { |
| 34 » » atomic.AddInt64(&e.Errors, 1) | 59 » » e.errors.increment() |
| 35 } | 60 } |
| 36 return err | 61 return err |
| 37 } | 62 } |
| OLD | NEW |