當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。