当前位置: 首页>>代码示例>>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;未经允许,请勿转载。