Skip to content

Commit ab2e592

Browse files
committed
save
1 parent 1169e9a commit ab2e592

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

counter.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewCounter(name string, isGauge ...bool) *Counter {
2929
//
3030
// It may be used as a gauge if Dec and Set are called.
3131
type Counter struct {
32-
n uint64
32+
n atomic.Uint64
3333
isGauge atomic.Bool
3434
}
3535

@@ -39,12 +39,13 @@ func (c *Counter) IsGauge() bool {
3939

4040
// Inc increments c.
4141
func (c *Counter) Inc() {
42-
atomic.AddUint64(&c.n, 1)
42+
c.n.Add(1)
4343
}
4444

4545
// Dec decrements c.
4646
func (c *Counter) Dec() {
47-
atomic.AddUint64(&c.n, ^uint64(0))
47+
c.n.Add(-1)
48+
//atomic.AddUint64(&c.n, ^uint64(0))
4849
c.isGauge.Store(true)
4950
}
5051

@@ -53,21 +54,21 @@ func (c *Counter) Add(n int) {
5354
if n < 0 {
5455
c.isGauge.Store(true)
5556
}
56-
atomic.AddUint64(&c.n, uint64(n))
57+
c.n.Add(uint64(n))
5758
}
5859

5960
// Get returns the current value for c.
6061
func (c *Counter) Get() uint64 {
61-
return atomic.LoadUint64(&c.n)
62+
return c.n.Load()
6263
}
6364

6465
// Set sets c value to n.
6566
func (c *Counter) Set(n uint64) {
66-
if n < c.n {
67+
if n < c.n.Load() {
6768
c.isGauge.Store(true)
6869
}
6970

70-
atomic.StoreUint64(&c.n, n)
71+
c.n.Store(n)
7172
}
7273

7374
// marshalTo marshals c with the given prefix to w.

0 commit comments

Comments
 (0)