本文整理匯總了Golang中sync/atomic.StoreUint64函數的典型用法代碼示例。如果您正苦於以下問題:Golang StoreUint64函數的具體用法?Golang StoreUint64怎麽用?Golang StoreUint64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了StoreUint64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: bpsLoop
func bpsLoop(ch <-chan Report, dst *uint64) {
l := list.New()
for {
var tch <-chan time.Time
// Setup timer for front of list to become stale.
if e := l.Front(); e != nil {
dt := time.Second - time.Now().Sub(e.Value.(readerReport).t)
tch = time.After(dt)
}
select {
case q, ok := <-ch:
if !ok {
return
}
l.PushBack(q)
case <-tch:
l.Remove(l.Front())
}
// Compute new bps
if l.Len() == 0 {
atomic.StoreUint64(dst, 0)
} else {
f := l.Front().Value.(readerReport)
b := l.Back().Value.(readerReport)
atomic.StoreUint64(dst, uint64(b.pos-f.pos))
}
}
}
示例2: read
func read(r io.ReadCloser, lines chan<- string) {
var drops uint64 = 0
var reads uint64 = 0
go func() {
for _ = range time.Tick(time.Second) {
d := atomic.LoadUint64(&drops)
r := atomic.LoadUint64(&reads)
atomic.StoreUint64(&drops, 0)
atomic.StoreUint64(&reads, 0)
fmt.Fprintf(os.Stdout, "reads=%d drops=%d\n", r, d)
}
}()
rdr := bufio.NewReader(r)
for {
line, err := rdr.ReadString('\n')
//Drop the line if the lines buffer is full.
//Set buffSize to reduce drops.
if err == nil {
select {
case lines <- line:
atomic.AddUint64(&reads, 1)
default:
atomic.AddUint64(&drops, 1)
}
} else {
r.Close()
return
}
}
}
示例3: SendEvent
// SendEvent queues the given event.
// The event's `Id` field will be updated with a value that can be used by
// Sync(). This value is also provided as the return value for convenience.
func (logger *Logger) SendEvent(logEvent *event.Event) uint64 {
logEvent.Id = atomic.AddUint64(&logger.lastEventId, 1)
logger.mutex.RLock()
for _, eventHandlerSpec := range logger.eventHandlerMap {
if true { //TODO make dropping configurable per-handler
select {
case eventHandlerSpec.eventChannel <- logEvent:
atomic.StoreUint64(&eventHandlerSpec.lastSentEventId, logEvent.Id)
default:
fmt.Fprintf(os.Stderr, "Unable to send event to handler. Buffer full. handler=%s\n", eventHandlerSpec.name)
//TODO generate an event for this, but put in a time-last-dropped so we don't send the message to the handler which is dropping
// basically if we are dropping, and we last dropped < X seconds ago, don't generate another "event dropped" message
}
} else {
eventHandlerSpec.eventChannel <- logEvent
atomic.StoreUint64(&eventHandlerSpec.lastSentEventId, logEvent.Id)
}
}
logger.mutex.RUnlock()
if logger.GetSync() {
logger.Sync(logEvent.Id)
}
return logEvent.Id
}
示例4: InitialState
// InitialState implements the raft.Storage interface.
func (r *Range) InitialState() (raftpb.HardState, raftpb.ConfState, error) {
var hs raftpb.HardState
found, err := engine.MVCCGetProto(r.rm.Engine(), keys.RaftHardStateKey(r.Desc().RaftID),
proto.ZeroTimestamp, true, nil, &hs)
if err != nil {
return raftpb.HardState{}, raftpb.ConfState{}, err
}
if !found {
// We don't have a saved HardState, so set up the defaults.
if r.isInitialized() {
// Set the initial log term.
hs.Term = raftInitialLogTerm
hs.Commit = raftInitialLogIndex
atomic.StoreUint64(&r.lastIndex, raftInitialLogIndex)
} else {
// This is a new range we are receiving from another node. Start
// from zero so we will receive a snapshot.
atomic.StoreUint64(&r.lastIndex, 0)
}
}
var cs raftpb.ConfState
// For uninitalized ranges, membership is unknown at this point.
if found || r.isInitialized() {
for _, rep := range r.Desc().Replicas {
cs.Nodes = append(cs.Nodes, uint64(proto.MakeRaftNodeID(rep.NodeID, rep.StoreID)))
}
}
return hs, cs, nil
}
示例5: apply
// apply takes entries received from Raft (after it has been committed) and
// applies them to the current state of the EtcdServer.
// The given entries should not be empty.
func (s *EtcdServer) apply(es []raftpb.Entry, confState *raftpb.ConfState) (uint64, bool) {
var applied uint64
for i := range es {
e := es[i]
switch e.Type {
case raftpb.EntryNormal:
var r pb.Request
pbutil.MustUnmarshal(&r, e.Data)
s.w.Trigger(r.ID, s.applyRequest(r))
case raftpb.EntryConfChange:
var cc raftpb.ConfChange
pbutil.MustUnmarshal(&cc, e.Data)
shouldstop, err := s.applyConfChange(cc, confState)
s.w.Trigger(cc.ID, err)
if shouldstop {
return applied, true
}
default:
log.Panicf("entry type should be either EntryNormal or EntryConfChange")
}
atomic.StoreUint64(&s.raftIndex, e.Index)
atomic.StoreUint64(&s.raftTerm, e.Term)
applied = e.Index
}
return applied, false
}
示例6: NewRange
// NewRange initializes the range using the given metadata.
func NewRange(desc *proto.RangeDescriptor, rm rangeManager) (*Range, error) {
r := &Range{
rm: rm,
cmdQ: NewCommandQueue(),
tsCache: NewTimestampCache(rm.Clock()),
respCache: NewResponseCache(desc.RaftID, rm.Engine()),
pendingCmds: map[cmdIDKey]*pendingCmd{},
}
r.setDescWithoutProcessUpdate(desc)
lastIndex, err := r.loadLastIndex()
if err != nil {
return nil, err
}
atomic.StoreUint64(&r.lastIndex, lastIndex)
appliedIndex, err := r.loadAppliedIndex(r.rm.Engine())
if err != nil {
return nil, err
}
atomic.StoreUint64(&r.appliedIndex, appliedIndex)
lease, err := loadLeaderLease(r.rm.Engine(), desc.RaftID)
if err != nil {
return nil, err
}
atomic.StorePointer(&r.lease, unsafe.Pointer(lease))
if r.stats, err = newRangeStats(desc.RaftID, rm.Engine()); err != nil {
return nil, err
}
return r, nil
}
示例7: apply
// apply takes entries received from Raft (after it has been committed) and
// applies them to the current state of the EtcdServer.
// The given entries should not be empty.
func (s *EtcdServer) apply(es []raftpb.Entry, confState *raftpb.ConfState) (uint64, bool) {
var applied uint64
var shouldstop bool
var err error
for i := range es {
e := es[i]
switch e.Type {
case raftpb.EntryNormal:
// raft state machine may generate noop entry when leader confirmation.
// skip it in advance to avoid some potential bug in the future
if len(e.Data) == 0 {
select {
case s.forceVersionC <- struct{}{}:
default:
}
break
}
var r pb.Request
pbutil.MustUnmarshal(&r, e.Data)
s.w.Trigger(r.ID, s.applyRequest(r))
case raftpb.EntryConfChange:
var cc raftpb.ConfChange
pbutil.MustUnmarshal(&cc, e.Data)
shouldstop, err = s.applyConfChange(cc, confState)
s.w.Trigger(cc.ID, err)
default:
plog.Panicf("entry type should be either EntryNormal or EntryConfChange")
}
atomic.StoreUint64(&s.r.index, e.Index)
atomic.StoreUint64(&s.r.term, e.Term)
applied = e.Index
}
return applied, shouldstop
}
示例8: ResetStats
func (c *Conn) ResetStats() {
atomic.StoreUint64(&c.s.Recv, 0)
atomic.StoreUint64(&c.s.Sent, 0)
c.s.FirstRecv = time.Time{}
c.s.FirstSend = time.Time{}
c.s.LastRecv = time.Time{}
c.s.LastSend = time.Time{}
return
}
示例9: Reset
// Reset resets the cache
func (c *Cache) Reset() {
c.Lock()
defer c.Unlock()
// drop the old map for GC and reset counters
c.m = make(map[string]*Item, c.capacity)
atomic.StoreUint64(&c.hits, 0)
atomic.StoreUint64(&c.misses, 0)
}
示例10: Write
func (f *fakeWriter) Write(message *events.Envelope) {
if message.GetEventType() == events.Envelope_ValueMetric {
switch message.GetValueMetric().GetName() {
case "Uptime":
atomic.StoreUint64(&f.lastUptime, uint64(message.GetValueMetric().GetValue()))
case "LinuxFileDescriptor":
atomic.StoreUint64(&f.openFileDescriptors, uint64(message.GetValueMetric().GetValue()))
}
}
}
示例11: RLock
// RLock readlock resurs from thread
// uses double check
func (t *TControl) RLock(threadId uint16, resursId uint64) {
for {
if atomic.LoadUint64(&t.writer) != resursId {
atomic.StoreUint64(&t.readers[threadId], resursId)
if atomic.LoadUint64(&t.writer) != resursId {
return
}
atomic.StoreUint64(&t.readers[threadId], unlocked)
}
t.sleep()
}
}
示例12: update
func (t *VBMeta) update(from *VBMeta) *VBMeta {
t.State = parseVBState(from.State).String()
fromCas := atomic.LoadUint64(&from.LastCas)
if atomic.LoadUint64(&t.LastCas) < fromCas {
atomic.StoreUint64(&t.LastCas, fromCas)
}
metaCas := atomic.LoadUint64(&from.MetaCas)
if atomic.LoadUint64(&t.MetaCas) < metaCas {
atomic.StoreUint64(&t.MetaCas, metaCas)
}
return t
}
示例13: loop
func (s *Sniffer) loop(src gopacket.ZeroCopyPacketDataSource, on, off time.Duration) {
var (
process = uint64(1) // initially enabled
total = uint64(0) // total packets seen
count = uint64(0) // count of packets captured
packets = make(chan Packet, 1024) // decoded packets
rpt = report.MakeReport() // the report we build
turnOn = (<-chan time.Time)(nil) // signal to start capture (initially enabled)
turnOff = time.After(on) // signal to stop capture
done = make(chan struct{}) // when src is finished, we're done too
)
// As a special case, if our off duty cycle is zero, i.e. 100% sample
// rate, we simply disable the turn-off signal channel.
if off == 0 {
turnOff = nil
}
go func() {
s.read(src, packets, &process, &total, &count)
close(done)
}()
for {
select {
case p := <-packets:
s.Merge(p, &rpt)
case <-turnOn:
atomic.StoreUint64(&process, 1) // enable packet capture
turnOn = nil // disable the on switch
turnOff = time.After(on) // enable the off switch
case <-turnOff:
atomic.StoreUint64(&process, 0) // disable packet capture
turnOn = time.After(off) // enable the on switch
turnOff = nil // disable the off switch
case c := <-s.reports:
rpt.Sampling.Count = atomic.LoadUint64(&count)
rpt.Sampling.Total = atomic.LoadUint64(&total)
interpolateCounts(rpt)
c <- rpt
atomic.StoreUint64(&count, 0)
atomic.StoreUint64(&total, 0)
rpt = report.MakeReport()
case <-done:
return
}
}
}
示例14: Validate
func (r *Rule) Validate(ctx *fasthttp.RequestCtx, rs types.ResponseState) types.ResponseState {
curTime := uint64(time.Now().Unix())
if r.Limit != 0 && curTime-r.lastTick >= r.Interval {
r.filterMatchCount = 0
atomic.StoreUint64(&r.filterMatchCount, 0)
atomic.StoreUint64(&r.lastTick, curTime)
for _, a := range r.Aggregations {
a.Lock()
a.Values = make(map[string]uint64)
a.Unlock()
}
}
for _, t := range r.Filters {
if _, found := t.Match(ctx); !found {
return types.UNTOUCHED
}
}
matched := false
state := rs
if len(r.Aggregations) == 0 {
atomic.AddUint64(&r.filterMatchCount, 1)
if r.filterMatchCount > r.Limit {
matched = true
}
} else {
for _, a := range r.Aggregations {
if a.Get(ctx) > r.Limit {
matched = true
}
}
}
if matched {
atomic.AddUint64(&r.MatchCount, 1)
for _, a := range r.Actions {
// Skip serving actions if we already had one
s := a.GetResponseState()
if state == types.SERVED && s == types.SERVED {
continue
}
err := a.Act(r.Name, ctx)
// TODO error handling
if err != nil {
fmt.Println("meh", err)
}
if s > state {
state = s
}
}
}
validateRuleList(&r.SubRules, &state, ctx)
return state
}
示例15: apply
// apply takes entries received from Raft (after it has been committed) and
// applies them to the current state of the EtcdServer.
// The given entries should not be empty.
func (s *EtcdServer) apply(es []raftpb.Entry, confState *raftpb.ConfState) (uint64, bool) {
var applied uint64
var shouldstop bool
for i := range es {
e := es[i]
// set the consistent index of current executing entry
s.consistIndex.setConsistentIndex(e.Index)
switch e.Type {
case raftpb.EntryNormal:
// raft state machine may generate noop entry when leader confirmation.
// skip it in advance to avoid some potential bug in the future
if len(e.Data) == 0 {
select {
case s.forceVersionC <- struct{}{}:
default:
}
break
}
var raftReq pb.InternalRaftRequest
if !pbutil.MaybeUnmarshal(&raftReq, e.Data) { // backward compatible
var r pb.Request
pbutil.MustUnmarshal(&r, e.Data)
s.w.Trigger(r.ID, s.applyRequest(r))
} else {
switch {
case raftReq.V2 != nil:
req := raftReq.V2
s.w.Trigger(req.ID, s.applyRequest(*req))
default:
s.w.Trigger(raftReq.ID, s.applyV3Request(&raftReq))
}
}
case raftpb.EntryConfChange:
var cc raftpb.ConfChange
pbutil.MustUnmarshal(&cc, e.Data)
removedSelf, err := s.applyConfChange(cc, confState)
shouldstop = shouldstop || removedSelf
s.w.Trigger(cc.ID, err)
default:
plog.Panicf("entry type should be either EntryNormal or EntryConfChange")
}
atomic.StoreUint64(&s.r.index, e.Index)
atomic.StoreUint64(&s.r.term, e.Term)
applied = e.Index
}
return applied, shouldstop
}