common, p2p: use AbsTime.Add instead of conversion (#25417)

This commit is contained in:
Daniel Liu 2024-12-17 16:49:30 +08:00
parent e3edcc8429
commit 32e033df3a
4 changed files with 12 additions and 12 deletions

View file

@ -58,7 +58,7 @@ func (s *Simulated) Run(d time.Duration) {
s.mu.Lock() s.mu.Lock()
s.init() s.init()
end := s.now + AbsTime(d) end := s.now.Add(d)
var do []func() var do []func()
for len(s.scheduled) > 0 && s.scheduled[0].at <= end { for len(s.scheduled) > 0 && s.scheduled[0].at <= end {
ev := heap.Pop(&s.scheduled).(*simTimer) ev := heap.Pop(&s.scheduled).(*simTimer)
@ -134,7 +134,7 @@ func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer {
func (s *Simulated) schedule(d time.Duration, fn func()) *simTimer { func (s *Simulated) schedule(d time.Duration, fn func()) *simTimer {
s.init() s.init()
at := s.now + AbsTime(d) at := s.now.Add(d)
ev := &simTimer{do: fn, at: at, s: s} ev := &simTimer{do: fn, at: at, s: s}
heap.Push(&s.scheduled, ev) heap.Push(&s.scheduled, ev)
s.cond.Broadcast() s.cond.Broadcast()

View file

@ -88,13 +88,13 @@ func (q *LazyQueue) Refresh() {
// refresh re-evaluates items in the older queue and swaps the two queues // refresh re-evaluates items in the older queue and swaps the two queues
func (q *LazyQueue) refresh(now mclock.AbsTime) { func (q *LazyQueue) refresh(now mclock.AbsTime) {
q.maxUntil = now + mclock.AbsTime(q.period) q.maxUntil = now.Add(q.period)
for q.queue[0].Len() != 0 { for q.queue[0].Len() != 0 {
q.Push(heap.Pop(q.queue[0]).(*item).value) q.Push(heap.Pop(q.queue[0]).(*item).value)
} }
q.queue[0], q.queue[1] = q.queue[1], q.queue[0] q.queue[0], q.queue[1] = q.queue[1], q.queue[0]
q.indexOffset = 1 - q.indexOffset q.indexOffset = 1 - q.indexOffset
q.maxUntil += mclock.AbsTime(q.period) q.maxUntil = q.maxUntil.Add(q.period)
} }
// Push adds an item to the queue // Push adds an item to the queue

View file

@ -107,7 +107,7 @@ func pongToTicket(localTime mclock.AbsTime, topics []Topic, node *Node, p *ingre
} }
// Convert wait periods to local absolute time. // Convert wait periods to local absolute time.
for i, wp := range wps { for i, wp := range wps {
t.regTime[i] = localTime + mclock.AbsTime(time.Second*time.Duration(wp)) t.regTime[i] = localTime.Add(time.Second * time.Duration(wp))
} }
return t, nil return t, nil
} }
@ -349,7 +349,7 @@ func (s *ticketStore) nextFilteredTicket() (*ticketRef, time.Duration) {
} }
log.Trace("Found discovery ticket to register", "node", ticket.t.node, "serial", ticket.t.serial, "wait", wait) log.Trace("Found discovery ticket to register", "node", ticket.t.node, "serial", ticket.t.serial, "wait", wait)
regTime := now + mclock.AbsTime(wait) regTime := now.Add(wait)
topic := ticket.t.topics[ticket.idx] topic := ticket.t.topics[ticket.idx]
if s.tickets[topic] != nil && regTime >= s.tickets[topic].nextReg { if s.tickets[topic] != nil && regTime >= s.tickets[topic].nextReg {
return ticket, wait return ticket, wait
@ -614,7 +614,7 @@ func (s *ticketStore) cleanupTopicQueries(now mclock.AbsTime) {
delete(s.queriesSent, n) delete(s.queriesSent, n)
} }
} }
s.nextTopicQueryCleanup = now + mclock.AbsTime(topicQueryTimeout) s.nextTopicQueryCleanup = now.Add(topicQueryTimeout)
} }
func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNode) (timeout bool) { func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNode) (timeout bool) {
@ -625,7 +625,7 @@ func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNod
return true return true
} }
q, ok := qq[hash] q, ok := qq[hash]
if !ok || now > q.sent+mclock.AbsTime(topicQueryTimeout) { if !ok || now > q.sent.Add(topicQueryTimeout) {
return true return true
} }
inside := float64(0) inside := float64(0)

View file

@ -194,7 +194,7 @@ func (t *topicTable) addEntry(node *Node, topic Topic) {
topic: topic, topic: topic,
fifoIdx: fifoIdx, fifoIdx: fifoIdx,
node: node, node: node,
expire: tm + mclock.AbsTime(fallbackRegistrationExpiry), expire: tm.Add(fallbackRegistrationExpiry),
} }
if printTestImgLogs { if printTestImgLogs {
fmt.Printf("*+ %d %v %016x %016x\n", tm/1000000, topic, t.self.sha[:8], node.sha[:8]) fmt.Printf("*+ %d %v %016x %016x\n", tm/1000000, topic, t.self.sha[:8], node.sha[:8])
@ -251,7 +251,7 @@ func (t *topicTable) useTicket(node *Node, serialNo uint32, topics []Topic, idx
} }
if serialNo != n.lastUsedTicket { if serialNo != n.lastUsedTicket {
n.lastUsedTicket = serialNo n.lastUsedTicket = serialNo
n.noRegUntil = tm + mclock.AbsTime(noRegTimeout()) n.noRegUntil = tm.Add(noRegTimeout())
t.storeTicketCounters(node) t.storeTicketCounters(node)
} }
@ -263,7 +263,7 @@ func (t *topicTable) useTicket(node *Node, serialNo uint32, topics []Topic, idx
t.addEntry(node, topics[idx]) t.addEntry(node, topics[idx])
} else { } else {
// if there is an active entry, don't move to the front of the FIFO but prolong expire time // if there is an active entry, don't move to the front of the FIFO but prolong expire time
e.expire = tm + mclock.AbsTime(fallbackRegistrationExpiry) e.expire = tm.Add(fallbackRegistrationExpiry)
} }
return true return true
} }
@ -293,7 +293,7 @@ func (topictab *topicTable) getTicket(node *Node, topics []Topic) *ticket {
waitPeriod = minWaitPeriod waitPeriod = minWaitPeriod
} }
t.regTime[i] = now + mclock.AbsTime(waitPeriod) t.regTime[i] = now.Add(waitPeriod)
} }
return t return t
} }