当前位置: 首页>>代码示例>>Golang>>正文


Golang atomic.SwapInt64函数代码示例

本文整理汇总了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
}
开发者ID:pbnjay,项目名称:exphttp,代码行数:34,代码来源:moving_average.go

示例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
}
开发者ID:hqr,项目名称:surge,代码行数:11,代码来源:runner.go

示例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)
			}
		}
	}()
}
开发者ID:allmad,项目名称:madq,代码行数:12,代码来源:lfs.go

示例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)
	}
}
开发者ID:One-com,项目名称:gonelog,代码行数:7,代码来源:counter.go

示例5: requestCountReporter

func requestCountReporter() {
	for {
		time.Sleep(time.Second)
		cur := atomic.SwapInt64(&counter, int64(0))
		log.Printf("%v requests", cur)
	}
}
开发者ID:shawnburke,项目名称:tchannel,代码行数:7,代码来源:client.go

示例6: getQueryCount

func getQueryCount(reset bool) int64 {
	if reset {
		return atomic.SwapInt64(&queryCount, 0)
	} else {
		return atomic.LoadInt64(&queryCount)
	}
}
开发者ID:ljvblfz,项目名称:slot-golang,代码行数:7,代码来源:wsclient.go

示例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)
}
开发者ID:Psiphon-Labs,项目名称:psiphon-tunnel-core,代码行数:32,代码来源:throttled.go

示例8: getRecvCount

func getRecvCount(reset bool) int64 {
	if reset {
		return atomic.SwapInt64(&recvCount, 0)
	} else {
		return atomic.LoadInt64(&recvCount)
	}
}
开发者ID:ljvblfz,项目名称:slot-golang,代码行数:7,代码来源:wsclient.go

示例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)
		}
	}
}
开发者ID:better0332,项目名称:myproxy,代码行数:28,代码来源:serv.go

示例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)
}
开发者ID:Blogoslov,项目名称:readline,代码行数:8,代码来源:terminal.go

示例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)
	}
}
开发者ID:skaes,项目名称:logjam-tools,代码行数:9,代码来源:server.go

示例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()
}
开发者ID:fgrehm,项目名称:pucrs-simple-json-db,代码行数:8,代码来源:terminal.go

示例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
}
开发者ID:hqr,项目名称:surge,代码行数:27,代码来源:node.go

示例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()
}
开发者ID:chzyer,项目名称:godl,代码行数:8,代码来源:ratelimit.go

示例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
}
开发者ID:hqr,项目名称:surge,代码行数:9,代码来源:m4.go


注:本文中的sync/atomic.SwapInt64函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。