github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/circuitbreaker/counter.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package circuitbreaker
    16  
    17  import (
    18  	"runtime"
    19  	"sync/atomic"
    20  
    21  	"github.com/bytedance/gopkg/internal/runtimex"
    22  )
    23  
    24  type Counter interface {
    25  	Add(i int64)
    26  	Get() int64
    27  	Zero()
    28  }
    29  
    30  type atomicCounter struct {
    31  	x int64
    32  }
    33  
    34  func (c *atomicCounter) Add(i int64) {
    35  	atomic.AddInt64(&c.x, i)
    36  }
    37  
    38  func (c *atomicCounter) Get() int64 {
    39  	return atomic.LoadInt64(&c.x)
    40  }
    41  
    42  func (c *atomicCounter) Zero() {
    43  	atomic.StoreInt64(&c.x, 0)
    44  }
    45  
    46  const (
    47  	cacheLineSize = 64
    48  )
    49  
    50  var (
    51  	countersLen int
    52  )
    53  
    54  func init() {
    55  	countersLen = runtime.GOMAXPROCS(0)
    56  }
    57  
    58  type counterShard struct {
    59  	x int64
    60  	_ [cacheLineSize - 8]byte
    61  }
    62  
    63  type perPCounter []counterShard
    64  
    65  func newPerPCounter() perPCounter {
    66  	return make([]counterShard, countersLen)
    67  }
    68  
    69  func (c perPCounter) Add(i int64) {
    70  	tid := runtimex.Pid()
    71  	atomic.AddInt64(&c[tid%countersLen].x, i)
    72  }
    73  
    74  // Get is not precise, but it's ok in this scenario.
    75  func (c perPCounter) Get() int64 {
    76  	var n int64
    77  	for i := range c {
    78  		n += atomic.LoadInt64(&c[i].x)
    79  	}
    80  	return n
    81  }
    82  
    83  func (c perPCounter) Zero() {
    84  	for i := range c {
    85  		atomic.StoreInt64(&c[i].x, 0)
    86  	}
    87  }