本文整理匯總了Golang中sync/atomic.SwapInt64函數的典型用法代碼示例。如果您正苦於以下問題:Golang SwapInt64函數的具體用法?Golang SwapInt64怎麽用?Golang SwapInt64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SwapInt64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewMovingAverageWithGranularity
// NewMovingAverageWithGranularity makes a new MovingAverage
// using the interval and granularity settings provided. Granularity controls
// how accurate the moving average is within an interval, at the expense of
// increased memory usage (two int64 per gran number of "buckets").
func NewMovingAverageWithGranularity(interval time.Duration, gran int) *MovingAverage {
if interval <= time.Duration(0) || gran <= 1 {
return &MovingAverage{
sums: []int64{0},
counts: []int64{0},
}
}
r := &MovingAverage{
sums: make([]int64, gran),
counts: make([]int64, gran),
}
go func() {
i := 0
t := time.NewTicker(interval / time.Duration(gran))
for range t.C {
i = r.index
r.index = (r.index + 1) % gran
// this is "as atomic" as easily possible...
s := atomic.SwapInt64(&r.sums[r.index], 0)
n := atomic.SwapInt64(&r.counts[r.index], 0)
r.otherSums += r.sums[i] - s
r.otherCounts += r.counts[i] - n
}
}()
return r
}
示例2: GetStats
func (r *NodeRunnerBase) GetStats(reset bool) RunnerStats {
s := r.rxqueue.GetStats(reset)
if reset {
s["txbytes"] = atomic.SwapInt64(&r.txbytestats, 0)
s["rxbytes"] = atomic.SwapInt64(&r.rxbytestats, 0)
} else {
s["txbytes"] = atomic.LoadInt64(&r.txbytestats)
s["rxbytes"] = atomic.LoadInt64(&r.rxbytestats)
}
return s
}
示例3: init
func init() {
go func() {
for _ = range time.Tick(time.Second) {
a := atomic.SwapInt64(&a, 0)
b := atomic.SwapInt64(&b, 0)
c := atomic.SwapInt64(&c, 0)
if b != 0 {
println(time.Duration(a/b).String(), c/b, b)
}
}
}()
}
示例4: Flush
func (c *Counter) Flush(f FlusherSink) {
val := atomic.SwapInt64(&c.val, 0)
if val != 0 {
n := Numeric64{Type: Int64, value: uint64(val)}
f.EmitNumeric64(c.name, MeterCounter, n)
}
}
示例5: requestCountReporter
func requestCountReporter() {
for {
time.Sleep(time.Second)
cur := atomic.SwapInt64(&counter, int64(0))
log.Printf("%v requests", cur)
}
}
示例6: getQueryCount
func getQueryCount(reset bool) int64 {
if reset {
return atomic.SwapInt64(&queryCount, 0)
} else {
return atomic.LoadInt64(&queryCount)
}
}
示例7: Write
func (conn *ThrottledConn) Write(buffer []byte) (int, error) {
// See comments in Read.
conn.writeLock.Lock()
defer conn.writeLock.Unlock()
if atomic.LoadInt64(&conn.writeUnthrottledBytes) > 0 {
n, err := conn.Conn.Write(buffer)
atomic.AddInt64(&conn.writeUnthrottledBytes, -int64(n))
return n, err
}
if atomic.LoadInt32(&conn.closeAfterExhausted) == 1 {
conn.Conn.Close()
return 0, errors.New("throttled conn exhausted")
}
rate := atomic.SwapInt64(&conn.writeBytesPerSecond, -1)
if rate != -1 {
if rate == 0 {
conn.throttledWriter = conn.Conn
} else {
conn.throttledWriter = ratelimit.Writer(
conn.Conn,
ratelimit.NewBucketWithRate(float64(rate), rate))
}
}
return conn.throttledWriter.Write(buffer)
}
示例8: getRecvCount
func getRecvCount(reset bool) int64 {
if reset {
return atomic.SwapInt64(&recvCount, 0)
} else {
return atomic.LoadInt64(&recvCount)
}
}
示例9: cycleHandle
func cycleHandle() {
var t1, t2 int64
t2 = time.Now().UnixNano()
for {
time.Sleep(1 * time.Minute)
t1 = t2
t2 = time.Now().UnixNano()
infoArray := proxy.HandleAccountInfo(t2 - t1)
b, _ := json.Marshal(&infoArray)
resp, err := client.Post("https://speedmao.com/userinfo", "application/json", bytes.NewReader(b))
if err != nil {
log.Println(err)
continue
}
resp.Body.Close()
if resp.StatusCode == 200 {
for _, info := range infoArray {
atomic.SwapInt64(&info.Transfer, 0)
}
} else {
log.Println("post user info fail:", resp.Status)
}
}
}
示例10: Close
func (t *Terminal) Close() error {
if atomic.SwapInt64(&t.closed, 1) != 0 {
return nil
}
t.stopChan <- struct{}{}
t.wg.Wait()
return Restore(syscall.Stdin, t.state)
}
示例11: statsReporter
// report number of incoming zmq messages every second
func statsReporter() {
for !interrupted {
time.Sleep(1 * time.Second)
msg_count := atomic.SwapInt64(&processed, 0)
conn_count := atomic.LoadInt64(&ws_connections)
logInfo("processed: %d, ws connections: %d", msg_count, conn_count)
}
}
示例12: Close
func (t *Terminal) Close() error {
if atomic.SwapInt64(&t.closed, 1) != 0 {
return nil
}
t.stopChan <- struct{}{}
t.wg.Wait()
return t.ExitRawMode()
}
示例13: GetStats
//
// stats
//
// GetStats implements the corresponding NodeRunnerInterface method for the
// GatewayUch common counters. Some of them are inc-ed inside this module,
// others - elsewhere, for instance in the concrete gateway's instance
// that embeds this GatewayUch
// The caller (such as, e.g., stats.go) will typically collect all the
// atomic counters and reset them to zeros to collect new values with the
// next iteration..
func (r *GatewayCommon) GetStats(reset bool) RunnerStats {
var s = make(map[string]int64, 8)
if reset {
s["txbytes"] = atomic.SwapInt64(&r.txbytestats, 0)
s["rxbytes"] = atomic.SwapInt64(&r.rxbytestats, 0)
s["tio"] = atomic.SwapInt64(&r.tiostats, 0)
s["chunk"] = atomic.SwapInt64(&r.chunkstats, 0)
s["replica"] = atomic.SwapInt64(&r.replicastats, 0)
} else {
s["txbytes"] = atomic.LoadInt64(&r.txbytestats)
s["rxbytes"] = atomic.LoadInt64(&r.rxbytestats)
s["tio"] = atomic.LoadInt64(&r.tiostats)
s["chunk"] = atomic.LoadInt64(&r.chunkstats)
s["replica"] = atomic.LoadInt64(&r.replicastats)
}
return s
}
示例14: Reset
func (r *RateLimit) Reset() {
r.Lock()
if atomic.SwapInt64(&r.waiting, 0) != 0 {
r.wg.Done()
}
atomic.StoreInt64(&r.written, 0)
r.Unlock()
}
示例15: GetStats
func (r *gatewayFour) GetStats(reset bool) RunnerStats {
s := r.NodeRunnerBase.GetStats(true)
if reset {
s["tio"] = atomic.SwapInt64(&r.tiostats, 0)
} else {
s["tio"] = atomic.LoadInt64(&r.tiostats)
}
return s
}