本文整理匯總了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
}
示例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)
}
}
示例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))
}
示例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)
}
},
))
}
示例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")
}
示例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()
}
}
示例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)
}
}()
}
示例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)
}
示例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
}
示例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})
}
}
}
示例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)
}
示例12: SwapUint64
func SwapUint64(addr *AlignedUint64, new uint64) uint64 {
return orig.SwapUint64(&(addr.data), new)
}
示例13: GetWrites
func (self *CoordinatorMonitor) GetWrites() uint64 {
return atomic.SwapUint64(&self.pointsWritten, 0)
}
示例14: submitTraffic
func (mc *Conn) submitTraffic() {
mc.m.submitTraffic(mc.ID,
atomic.SwapUint64(&mc.BytesIn, 0),
atomic.SwapUint64(&mc.BytesOut, 0))
}
示例15: updateTime
func updateTime() {
c := time.Tick(100 * time.Millisecond)
for now := range c {
atomic.SwapUint64(&unixTime, uint64(now.Unix()))
}
}