This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathslot_gauge.go
76 lines (60 loc) · 1.99 KB
/
slot_gauge.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
package txpool
import (
"sync/atomic"
"github.com/armon/go-metrics"
"github.com/0xPolygon/polygon-edge/types"
)
const (
highPressureMark = 80 // 80%
)
// Gauge for measuring pool capacity in slots
type slotGauge struct {
height uint64 // amount of slots currently occupying the pool
max uint64 // max limit
}
// read returns the current height of the gauge.
func (g *slotGauge) read() uint64 {
return atomic.LoadUint64(&g.height)
}
// increase increases the height of the gauge by the specified slots amount.
func (g *slotGauge) increase(slots uint64) {
newHeight := atomic.AddUint64(&g.height, slots)
metrics.SetGauge([]string{txPoolMetrics, "slots_used"}, float32(newHeight))
}
// increaseWithinLimit increases the height of the gauge by the specified slots amount only if the increased height is
// less than max. Returns true if the height is increased.
func (g *slotGauge) increaseWithinLimit(slots uint64) (updated bool) {
for {
old := g.read()
newHeight := old + slots
if newHeight > g.max {
return false
}
if atomic.CompareAndSwapUint64(&g.height, old, newHeight) {
metrics.SetGauge([]string{txPoolMetrics, "slots_used"}, float32(newHeight))
return true
}
}
}
// decrease decreases the height of the gauge by the specified slots amount.
func (g *slotGauge) decrease(slots uint64) {
newHeight := atomic.AddUint64(&g.height, ^(slots - 1))
metrics.SetGauge([]string{txPoolMetrics, "slots_used"}, float32(newHeight))
}
// highPressure checks if the gauge level
// is higher than the 0.8*max threshold
func (g *slotGauge) highPressure() bool {
return g.read() > (highPressureMark*g.max)/100
}
// free slots returns how many slots are currently available
func (g *slotGauge) freeSlots() uint64 {
return g.max - g.read()
}
// slotsRequired calculates the number of slots required for given transaction(s).
func slotsRequired(txs ...*types.Transaction) uint64 {
slots := uint64(0)
for _, tx := range txs {
slots += (tx.Size() + txSlotSize - 1) / txSlotSize
}
return slots
}