當前位置: 首頁>>代碼示例>>Golang>>正文


Golang atomic.SwapUint64函數代碼示例

本文整理匯總了Golang中sync/atomic.SwapUint64函數的典型用法代碼示例。如果您正苦於以下問題:Golang SwapUint64函數的具體用法?Golang SwapUint64怎麽用?Golang SwapUint64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了SwapUint64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ReadMeter

// ReadMeter returns the current count and resets the counter's value to 0. The
// returned value is normalized using the given delta to ensure that the value
// always represents a per second value.
func (counter *Counter) ReadMeter(delta time.Duration) map[string]float64 {
	result := make(map[string]float64)

	if value := atomic.SwapUint64(&counter.value, 0); value > 0 {
		result[""] = float64(value) * (float64(time.Second) / float64(delta))
	}

	return result
}
開發者ID:nativetouch,項目名稱:gometer,代碼行數:12,代碼來源:meter_counter.go

示例2: manage

func (rs *RateStat) manage() {
	for range time.Tick(rs.interval) {
		bucket := atomic.LoadInt32(&rs.curBucket)
		bucket++
		if bucket >= rs.length {
			bucket = 0
		}
		old := atomic.SwapUint64(&rs.Buckets[bucket], 0)
		atomic.StoreInt32(&rs.curBucket, bucket)
		atomic.AddUint64(&rs.curValue, -old)
	}
}
開發者ID:andyleap,項目名稱:ratestat,代碼行數:12,代碼來源:ratestat.go

示例3: Tick

// Tick the moving average
func (e *EWMA) Tick() {
	// Assume Tick is never called concurrently
	count := atomic.SwapUint64(&e.uncounted, 0)
	instantRate := float64(count) / e.interval.Seconds()
	rate := e.Rate()
	if e.initialized {
		rate += e.alpha * (instantRate - rate)
	} else {
		rate = instantRate
		e.initialized = true
	}
	atomic.StoreUint64(&e.rate, math.Float64bits(rate))
}
開發者ID:couchbasedeps,項目名稱:go-metrics-1,代碼行數:14,代碼來源:ewma.go

示例4: main

func main() {
	count := uint64(0)
	tick := time.NewTicker(time.Second)
	http.ListenAndServe(":6060", http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			select {
			case <-tick.C:
				log.Printf("Rate: %d", atomic.SwapUint64(&count, 0))
			default:
				atomic.AddUint64(&count, 1)
			}
		},
	))
}
開發者ID:aidylewis,項目名稱:go-test-server,代碼行數:14,代碼來源:server.go

示例5: Download

// Download downloads a file, identified by its nickname, to the destination
// specified.
func (r *Renter) Download(nickname, destination string) error {
	lockID := r.mu.Lock()
	// Lookup the file associated with the nickname.
	file, exists := r.files[nickname]
	if !exists {
		return errors.New("no file of that nickname")
	}

	// Create the download object and spawn the download process.
	d, err := newDownload(file, destination)
	if err != nil {
		return err
	}

	// Add the download to the download queue.
	r.downloadQueue = append(r.downloadQueue, d)
	r.mu.Unlock(lockID)

	// Download the file. We only need one piece, so iterate through the hosts
	// until a download succeeds.
	for i := 0; i < downloadAttempts; i++ {
		for _, piece := range d.pieces {
			downloadErr := d.downloadPiece(piece)
			if downloadErr == nil {
				// done
				d.complete = true
				d.file.Close()
				return nil
			}
			// Reset seek, since the file may have been partially written. The
			// next attempt will overwrite these bytes.
			d.file.Seek(0, 0)
			atomic.SwapUint64(&d.received, 0)
		}

		// This iteration failed, no hosts returned the piece. Try again
		// after waiting a random amount of time.
		randSource := make([]byte, 1)
		rand.Read(randSource)
		time.Sleep(time.Second * time.Duration(i*i) * time.Duration(randSource[0]))
	}

	// File could not be downloaded; delete the copy on disk.
	d.file.Close()
	os.Remove(destination)

	return errors.New("could not download any file pieces")
}
開發者ID:mm3,項目名稱:Sia,代碼行數:50,代碼來源:download.go

示例6: logMetrics

func (q *messageQueue) logMetrics() {
	for _ = range time.Tick(time.Second) {
		// Die with the handler
		if !q.running {
			break
		}

		// If this is the metrics queue - don't log metrics
		if q.Name == metricsQueueName {
			break
		}

		// Send various metrics
		currentMessageRate := atomic.SwapUint64(&q.messagesSentLastSecond, 0)

		q.metrics <- &Metric{Name: q.Name + ".messagerate", Value: int64(currentMessageRate), Type: "counter"}
		q.doLogGuages()
	}
}
開發者ID:FireEater64,項目名稱:gamq,代碼行數:19,代碼來源:messagequeue.go

示例7: startReporter

func (s *Scheme) startReporter() {
	s.done.Add(1)

	go func() {
		defer s.done.Done()

		ticker := time.NewTicker(s.reportCycle)
		for {
			<-ticker.C
			if s.isClosed() {
				break
			}

			report := Report{}
			report.MessageCount = atomic.SwapUint32(&s.messageCount, 0)
			report.ByteCount = atomic.SwapUint64(&s.byteCount, 0)
			report.ErrorCount = atomic.SwapUint32(&s.errorCount, 0)
			s.reporter.Report(report)
		}
	}()
}
開發者ID:myshkin5,項目名稱:netspel,代碼行數:21,代碼來源:scheme.go

示例8: calibrate

func (p *Pool) calibrate() {
	if !atomic.CompareAndSwapUint64(&p.calibrating, 0, 1) {
		return
	}

	a := make(callSizes, 0, steps)
	var callsSum uint64
	for i := uint64(0); i < steps; i++ {
		calls := atomic.SwapUint64(&p.calls[i], 0)
		callsSum += calls
		a = append(a, callSize{
			calls: calls,
			size:  minSize << i,
		})
	}
	sort.Sort(a)

	defaultSize := a[0].size
	maxSize := defaultSize

	maxSum := uint64(float64(callsSum) * maxPercentile)
	callsSum = 0
	for i := 0; i < steps; i++ {
		if callsSum > maxSum {
			break
		}
		callsSum += a[i].calls
		size := a[i].size
		if size > maxSize {
			maxSize = size
		}
	}

	atomic.StoreUint64(&p.defaultSize, defaultSize)
	atomic.StoreUint64(&p.maxSize, maxSize)

	atomic.StoreUint64(&p.calibrating, 0)
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:38,代碼來源:pool.go

示例9: Limit

// Limit returns true if rate was exceeded
func (rl *RateLimiter) Limit() bool {
	// Calculate the number of ns that have passed since our last call
	now := unixNano()
	passed := now - atomic.SwapUint64(&rl.lastCheck, now)

	// Add them to our allowance
	current := atomic.AddUint64(&rl.allowance, passed*rl.rate)

	// Ensure our allowance is not over maximum
	if current > rl.max {
		atomic.AddUint64(&rl.allowance, rl.max-current)
		current = rl.max
	}

	// If our allowance is less than one unit, rate-limit!
	if current < rl.unit {
		return true
	}

	// Not limited, subtract a unit
	atomic.AddUint64(&rl.allowance, -rl.unit)
	return false
}
開發者ID:ChrisYangLiu,項目名稱:fakewechat,代碼行數:24,代碼來源:ratelimit.go

示例10: monitor

func (m *Monitor) monitor() {
	m.t0 = time.Now()
	for {
		select {
		case <-m.exit:
			return
		default:
			// Use default and sleep instead of
			// a time.After case because extra
			// thread switching under heavy loads
			// makes a big performance difference.
			time.Sleep(m.period)

			// In case we missed an exit command
			// while we were sleeping; this technically
			// wouldn't invalidate the semantics,
			// but it'd still be dumb to unnecessarily
			// be doing stuff hundreds of milliseconds
			// after we were told to stop.
			select {
			case <-m.exit:
				return
			default:
			}

			t1 := time.Now()
			delta := t1.Sub(m.t0)
			m.t0 = t1

			nn := atomic.SwapUint64(&m.nn, 0)
			m.n += nn

			rate := float64(nn) / delta.Seconds()
			m.f(Rate{m.n, rate})
		}
	}
}
開發者ID:joshlf,項目名稱:rate,代碼行數:37,代碼來源:monitor.go

示例11: Swap

// Swap atomically swaps the wrapped uint64 and returns the old value.
func (i *Uint64) Swap(n uint64) uint64 {
	return atomic.SwapUint64(&i.v, n)
}
開發者ID:sgotti,項目名稱:stolon,代碼行數:4,代碼來源:atomic.go

示例12: SwapUint64

func SwapUint64(addr *AlignedUint64, new uint64) uint64 {
	return orig.SwapUint64(&(addr.data), new)
}
開發者ID:nimishzynga,項目名稱:go-couchbase,代碼行數:3,代碼來源:sync_386.go

示例13: GetWrites

func (self *CoordinatorMonitor) GetWrites() uint64 {
	return atomic.SwapUint64(&self.pointsWritten, 0)
}
開發者ID:Wikia,項目名稱:influxdb,代碼行數:3,代碼來源:coordinator_monitor.go

示例14: submitTraffic

func (mc *Conn) submitTraffic() {
	mc.m.submitTraffic(mc.ID,
		atomic.SwapUint64(&mc.BytesIn, 0),
		atomic.SwapUint64(&mc.BytesOut, 0))
}
開發者ID:getlantern,項目名稱:measured,代碼行數:5,代碼來源:measured.go

示例15: updateTime

func updateTime() {
	c := time.Tick(100 * time.Millisecond)
	for now := range c {
		atomic.SwapUint64(&unixTime, uint64(now.Unix()))
	}
}
開發者ID:Freeaqingme,項目名稱:Webjobber,代碼行數:6,代碼來源:main.go


注:本文中的sync/atomic.SwapUint64函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。