-
Notifications
You must be signed in to change notification settings - Fork 145
/
time_pool.go
148 lines (126 loc) · 3.83 KB
/
time_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package mempool
import (
"time"
"golang.org/x/exp/slices"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/wasp/packages/isc"
)
// Maintains a pool of requests that have to be postponed until specified timestamp.
type TimePool interface {
AddRequest(timestamp time.Time, request isc.Request)
TakeTill(timestamp time.Time) []isc.Request
Has(reqID *isc.RequestRef) bool
Filter(predicate func(request isc.Request, ts time.Time) bool)
}
// Here we implement TimePool. We maintain the request in a list ordered by a timestamp.
// The list is organized in slots. Each slot contains a list of requests that fit to the
// slot boundaries.
type timePoolImpl struct {
requests *shrinkingmap.ShrinkingMap[isc.RequestRefKey, isc.Request] // All the requests in this pool.
slots *timeSlot // Structure to fetch them fast by their time.
}
type timeSlot struct {
from time.Time
till time.Time
reqs *shrinkingmap.ShrinkingMap[time.Time, []isc.Request]
next *timeSlot
}
const slotPrecision = time.Minute
var _ TimePool = &timePoolImpl{}
func NewTimePool() TimePool {
return &timePoolImpl{
requests: shrinkingmap.New[isc.RequestRefKey, isc.Request](),
slots: nil,
}
}
func (tpi *timePoolImpl) AddRequest(timestamp time.Time, request isc.Request) {
reqRefKey := isc.RequestRefFromRequest(request).AsKey()
if tpi.requests.Has(reqRefKey) {
return
}
tpi.requests.Set(reqRefKey, request)
reqFrom, reqTill := tpi.timestampSlotBounds(timestamp)
prevNext := &tpi.slots
for slot := tpi.slots; ; {
if slot == nil || slot.from.After(reqFrom) { // Add new slot (append or insert).
newRequests := shrinkingmap.New[time.Time, []isc.Request]()
newRequests.Set(timestamp, []isc.Request{request})
newSlot := &timeSlot{
from: reqFrom,
till: reqTill,
reqs: newRequests,
next: slot,
}
*prevNext = newSlot
return
}
if slot.from == reqFrom { // Add to existing slot.
requests, _ := slot.reqs.GetOrCreate(timestamp, func() []isc.Request { return make([]isc.Request, 0, 1) })
slot.reqs.Set(timestamp, append(requests, request))
return
}
prevNext = &slot.next
slot = slot.next
}
}
func (tpi *timePoolImpl) TakeTill(timestamp time.Time) []isc.Request {
resp := []isc.Request{}
for slot := tpi.slots; slot != nil; slot = slot.next {
if slot.from.After(timestamp) {
break
}
slot.reqs.ForEach(func(ts time.Time, tsReqs []isc.Request) bool {
if ts == timestamp || ts.Before(timestamp) {
resp = append(resp, tsReqs...)
for _, req := range tsReqs {
reqRefKey := isc.RequestRefFromRequest(req).AsKey()
tpi.requests.Delete(reqRefKey)
}
slot.reqs.Delete(ts)
}
return true
})
if slot.reqs.Size() != 0 {
break
}
tpi.slots = slot.next
}
return resp
}
func (tpi *timePoolImpl) Has(reqRef *isc.RequestRef) bool {
return tpi.requests.Has(reqRef.AsKey())
}
func (tpi *timePoolImpl) Filter(predicate func(request isc.Request, ts time.Time) bool) {
prevNext := &tpi.slots
for slot := tpi.slots; slot != nil; slot = slot.next {
slot.reqs.ForEach(func(ts time.Time, tsReqs []isc.Request) bool {
requests := tsReqs
for i, req := range requests {
if !predicate(req, ts) {
requests = slices.Delete(requests, i, i+1)
}
}
if len(requests) != 0 {
slot.reqs.Set(ts, slices.Clip(requests))
} else {
slot.reqs.Delete(ts)
}
return true
})
if slot.reqs.Size() == 0 {
// Drop the current slot, if it is empty, keep the prevNext the same.
*prevNext = slot.next
} else {
prevNext = &slot.next
}
}
}
func (tpi *timePoolImpl) timestampSlotBounds(timestamp time.Time) (time.Time, time.Time) {
from := timestamp.Truncate(slotPrecision)
if timestamp == from {
return from, from
}
return from, from.Add(slotPrecision)
}