-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patheach.go
More file actions
192 lines (161 loc) · 3.86 KB
/
Copy patheach.go
File metadata and controls
192 lines (161 loc) · 3.86 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package xlog
import (
"context"
"log/slog"
"reflect"
"regexp"
"runtime"
"sync"
"golang.org/x/sync/errgroup"
)
// a List of directories that should be ignored by directory walking function.
// for example the versioning extension can register `.versions` directory to be
// ignored.
var ignoredPaths = []*regexp.Regexp{
regexp.MustCompile(`^\.`), // Ignore any hidden directory
}
// IgnorePath Register a pattern to be ignored when walking directories.
func IgnorePath(r *regexp.Regexp) {
ignoredPaths = append(ignoredPaths, r)
}
// IsIgnoredPath checks if a file path should be ignored according to the list
// of ignored paths. page source implementations can use it to ignore files from
// their sources.
func IsIgnoredPath(d string) bool {
for _, v := range ignoredPaths {
if v.MatchString(d) {
return true
}
}
return false
}
var (
pages []Page
pagesMutex sync.RWMutex
)
// Pages returns all pages in the xlog directory. The result is cached and the cache
// is populated on first call. To invalidate the cache, use InvalidatePagesCache.
func Pages(ctx context.Context) []Page {
pagesMutex.RLock()
cached := pages
pagesMutex.RUnlock()
if cached == nil {
populatePagesCache(ctx)
pagesMutex.RLock()
cached = pages
pagesMutex.RUnlock()
}
return cached
}
// EachPage iterates on all available pages. many extensions
// uses it to get all pages and maybe parse them and extract needed information.
func EachPage(ctx context.Context, f func(Page)) {
pagesMutex.RLock()
cached := pages
pagesMutex.RUnlock()
if cached == nil {
populatePagesCache(ctx)
pagesMutex.RLock()
cached = pages
pagesMutex.RUnlock()
}
for _, p := range cached {
select {
case <-ctx.Done():
return
default:
f(p)
}
}
}
var concurrency = runtime.NumCPU() * 4
// MapPage Similar to EachPage but iterates concurrently and accumulates
// returns in a slice.
func MapPage[T any](ctx context.Context, f func(Page) T) []T {
pagesMutex.RLock()
cached := pages
pagesMutex.RUnlock()
if cached == nil {
populatePagesCache(ctx)
pagesMutex.RLock()
cached = pages
pagesMutex.RUnlock()
}
grp, ctx := errgroup.WithContext(ctx)
grp.SetLimit(concurrency)
// Buffer must be large enough to hold all results to prevent deadlock
// when number of pages exceeds concurrency limit
ch := make(chan T, len(cached))
Loop:
for _, p := range cached {
select {
case <-ctx.Done():
break Loop
default:
grp.Go(func() (err error) {
val := f(p)
if isNil(val) {
return
}
ch <- val
return
})
}
}
// Close channel after all workers complete
go func() {
if err := grp.Wait(); err != nil {
slog.Error("Error during parallel page iteration", "error", err)
}
close(ch)
}()
// Collect results in main goroutine - eliminates race condition
output := make([]T, 0, len(cached))
for v := range ch {
output = append(output, v)
}
return output
}
// From https://stackoverflow.com/a/77341451/22401486
func isNil[T any](t T) bool {
v := reflect.ValueOf(t)
kind := v.Kind()
// Must be one of these types to be nillable
return !v.IsValid() || (kind == 22 || // reflect.Ptr
kind == reflect.Interface ||
kind == reflect.Slice ||
kind == reflect.Map ||
kind == reflect.Chan ||
kind == reflect.Func) &&
v.IsNil()
}
func clearPagesCache(p Page) (err error) {
pagesMutex.Lock()
pages = nil
pagesMutex.Unlock()
return nil
}
func populatePagesCache(ctx context.Context) {
pagesMutex.Lock()
defer pagesMutex.Unlock()
// Double-check after acquiring lock
if pages != nil {
return
}
pages = make([]Page, 0, 1000)
// Read sources with RLock
sourcesMutex.RLock()
sourcesSnapshot := make([]PageSource, len(sources))
copy(sourcesSnapshot, sources)
sourcesMutex.RUnlock()
for _, s := range sourcesSnapshot {
select {
case <-ctx.Done():
return
default:
s.Each(ctx, func(p Page) {
pages = append(pages, p)
})
}
}
}