|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package counter |
| 19 | + |
| 20 | +import ( |
| 21 | + "sync" |
| 22 | + "sync/atomic" |
| 23 | +) |
| 24 | + |
| 25 | +type Counter struct { |
| 26 | + name string |
| 27 | + value atomic.Int64 |
| 28 | +} |
| 29 | + |
| 30 | +func NewCounter(name string) *Counter { |
| 31 | + return &Counter{name: name} |
| 32 | +} |
| 33 | + |
| 34 | +func (c *Counter) Get() int64 { |
| 35 | + return c.value.Load() |
| 36 | +} |
| 37 | + |
| 38 | +func (c *Counter) Increment() { |
| 39 | + c.value.Add(1) |
| 40 | +} |
| 41 | + |
| 42 | +func (c *Counter) Decrement() { |
| 43 | + for { |
| 44 | + current := c.value.Load() |
| 45 | + if current == 0 { |
| 46 | + return |
| 47 | + } |
| 48 | + if c.value.CompareAndSwap(current, current-1) { |
| 49 | + return |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (c *Counter) Reset() { |
| 55 | + c.value.Store(0) |
| 56 | +} |
| 57 | + |
| 58 | +type DistributionCounter struct { |
| 59 | + name string |
| 60 | + data map[string]int64 |
| 61 | + mu sync.RWMutex |
| 62 | +} |
| 63 | + |
| 64 | +func NewDistributionCounter(name string) *DistributionCounter { |
| 65 | + return &DistributionCounter{ |
| 66 | + name: name, |
| 67 | + data: make(map[string]int64), |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (c *DistributionCounter) Increment(key string) { |
| 72 | + c.mu.Lock() |
| 73 | + defer c.mu.Unlock() |
| 74 | + c.data[key]++ |
| 75 | +} |
| 76 | + |
| 77 | +func (c *DistributionCounter) Decrement(key string) { |
| 78 | + c.mu.Lock() |
| 79 | + defer c.mu.Unlock() |
| 80 | + if value, ok := c.data[key]; ok { |
| 81 | + value-- |
| 82 | + if value <= 0 { |
| 83 | + delete(c.data, key) |
| 84 | + } else { |
| 85 | + c.data[key] = value |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (c *DistributionCounter) GetAll() map[string]int64 { |
| 91 | + c.mu.RLock() |
| 92 | + defer c.mu.RUnlock() |
| 93 | + result := make(map[string]int64, len(c.data)) |
| 94 | + for k, v := range c.data { |
| 95 | + result[k] = v |
| 96 | + } |
| 97 | + return result |
| 98 | +} |
| 99 | + |
| 100 | +func (c *DistributionCounter) Reset() { |
| 101 | + c.mu.Lock() |
| 102 | + defer c.mu.Unlock() |
| 103 | + c.data = make(map[string]int64) |
| 104 | +} |
0 commit comments