forked from rpcpool/yellowstone-faithful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-range.go
100 lines (86 loc) · 2.31 KB
/
http-range.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
package main
import (
"io"
"path/filepath"
"strings"
"time"
"github.com/rpcpool/yellowstone-faithful/metrics"
"k8s.io/klog/v2"
)
type ReaderAtCloser interface {
io.ReaderAt
io.Closer
}
type readCloserWrapper struct {
rac ReaderAtCloser
isRemote bool
isSplitCar bool
name string
size int64
}
func (r *readCloserWrapper) Size() int64 {
return r.size
}
// when reading print a dot
func (r *readCloserWrapper) ReadAt(p []byte, off int64) (n int, err error) {
startedAt := time.Now()
defer func() {
took := time.Since(startedAt)
var isIndex, isCar bool
// Metrics want to always report
// if has suffix .index, then it's an index file
if strings.HasSuffix(r.name, ".index") {
isIndex = true
// get the index name, which is the part before the .index suffix, after the last .
indexName := strings.TrimSuffix(r.name, ".index")
// split the index name by . and get the last part
byDot := strings.Split(indexName, ".")
if len(byDot) > 0 {
indexName = byDot[len(byDot)-1]
}
// TODO: distinguish between remote and local index reads
metrics.IndexLookupHistogram.WithLabelValues(indexName).Observe(float64(took.Seconds()))
}
// if has suffix .car, then it's a car file
if strings.HasSuffix(r.name, ".car") || r.isSplitCar {
isCar = true
carName := filepath.Base(r.name)
// TODO: distinguish between remote and local index reads
metrics.CarLookupHistogram.WithLabelValues(carName).Observe(float64(took.Seconds()))
}
if klog.V(5).Enabled() {
var icon string
if r.isRemote {
// add internet icon
icon = "🌐 "
} else {
// add disk icon
icon = "💾 "
}
prefix := icon + "[READ-UNKNOWN]"
if isIndex {
prefix = icon + azureBG("[READ-INDEX]")
} else if isCar {
if r.isSplitCar {
prefix = icon + azureBG("[READ-SPLIT-CAR]")
} else {
prefix = icon + purpleBG("[READ-CAR]")
}
}
klog.V(5).Infof(prefix+" %s:%d+%d (%s)\n", (r.name), off, len(p), took)
}
}()
return r.rac.ReadAt(p, off)
}
func purpleBG(s string) string {
// blue bg, black fg
return "\033[48;5;4m\033[38;5;0m" + s + "\033[0m"
}
func azureBG(s string) string {
// azure bg, black fg
return "\033[48;5;6m\033[38;5;0m" + s + "\033[0m"
}
// when closing print a newline
func (r *readCloserWrapper) Close() error {
return r.rac.Close()
}