-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloader.go
More file actions
245 lines (217 loc) · 5.38 KB
/
Copy pathloader.go
File metadata and controls
245 lines (217 loc) · 5.38 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package pageseo
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"mime"
"net/http"
"sync"
"golang.org/x/sync/singleflight"
)
// Skip is a sentinel error that indicates a page resource
// should not be validated.
var Skip = errors.New("do not validate this resource")
// Loader fetches the resource from a given location.
// Implementations should handle caching and other
// performance optimizations.
//
// Loader should return [Skip] if a resource
// should not be validated without raising an error
// or a warning.
type Loader interface {
Load(context.Context, string) ([]byte, string, error)
}
type skipAllLoader struct{}
var skipAllLoadingSingleton Loader = skipAllLoader{}
func (skipAllLoader) Load(context.Context, string) ([]byte, string, error) {
return nil, "", Skip
}
type Resource struct {
URL string
ContentType string
Content []byte
Error error
}
type fsLoader struct {
fs.FS
}
func NewFS(fs fs.FS) Loader {
if fs == nil {
panic("nil file system")
}
return fsLoader{
FS: fs,
}
}
func (fs fsLoader) Load(_ context.Context, url string) ([]byte, string, error) {
r, err := fs.FS.Open(url)
if err != nil {
return nil, "", fmt.Errorf("unable to open <%s>: %w", url, err)
}
data, err := io.ReadAll(r)
if err != nil {
return nil, "", fmt.Errorf("unable to load <%s>: %w", url, err)
}
ct, _, err := mime.ParseMediaType(http.DetectContentType(data))
if err != nil {
return nil, "", fmt.Errorf("unable to parse media type: %w", err)
}
return data, ct, nil
}
type semaphoreLoader struct {
Loaders chan Loader
}
func NewSemaphore(loaders ...Loader) Loader {
total := len(loaders)
switch total {
case 1:
if loaders[0] == nil {
panic("nil loader in the semaphore")
}
return loaders[0]
case 0:
panic("no semaphore loaders")
}
stack := make(chan Loader, total)
for _, loader := range loaders {
if loader == nil {
panic("nil loader in the semaphore")
}
stack <- loader
}
return semaphoreLoader{
Loaders: stack,
}
}
func (s semaphoreLoader) Load(ctx context.Context, url string) (data []byte, contentType string, err error) {
select {
case <-ctx.Done():
return nil, "", ctx.Err()
case loader := <-s.Loaders:
data, contentType, err = loader.Load(ctx, url)
s.Loaders <- loader
return
}
}
type hotSwapLoader struct {
Cursor int
Preloaded []Resource
Loader Loader
}
func NewHotSwap(ctx context.Context, loader Loader, URLs []string) Loader {
resources := make([]Resource, len(URLs))
wg := sync.WaitGroup{}
for i, url := range URLs {
wg.Add(1)
go func(ctx context.Context, i int, url string) {
data, contentType, err := loader.Load(ctx, url)
resources[i] = Resource{
URL: url,
ContentType: contentType,
Content: data,
Error: err,
}
wg.Done()
}(ctx, i, url)
}
wg.Wait()
return hotSwapLoader{
Loader: loader,
Cursor: 0,
Preloaded: resources,
}
}
func (h hotSwapLoader) Load(ctx context.Context, URL string) ([]byte, string, error) {
var i int
// search forward from cursor
for i = h.Cursor; i < len(h.Preloaded); i++ {
r := h.Preloaded[i]
if r.URL == URL {
h.Cursor = i + 1 // next time begin iteration from same point
return r.Content, r.ContentType, r.Error
}
}
// search backward from cursor
for i = h.Cursor - 1; i >= 0; i-- {
r := h.Preloaded[i]
if r.URL == URL {
return r.Content, r.ContentType, r.Error
}
}
// fallback on loader
return h.Loader.Load(ctx, URL)
}
type singleFlightLoader struct {
Loader
*singleflight.Group
}
func NewSingleFlightLoader(loader Loader) Loader {
if loader == nil {
panic("nil loader")
}
return &singleFlightLoader{
Loader: loader,
Group: &singleflight.Group{},
}
}
func (l *singleFlightLoader) Load(ctx context.Context, URL string) ([]byte, string, error) {
result, err, _ := l.Group.Do(URL, func() (any, error) {
data, ct, err := l.Loader.Load(ctx, URL)
return Resource{
URL: URL,
ContentType: ct,
Content: data,
}, err
})
r := result.(Resource)
return r.Content, r.ContentType, err
}
type loaderHTTP struct {
*http.Client
Headers http.Header
}
func NewHTTPClient(client *http.Client, headers http.Header) Loader {
if client == nil {
panic("nil HTTP client")
}
return loaderHTTP{
Client: client,
Headers: headers,
}
}
func (web loaderHTTP) Load(ctx context.Context, url string) (data []byte, contentType string, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, "", fmt.Errorf("unable to open <%s>: %w", url, err)
}
for key, values := range web.Headers {
for _, value := range values {
req.Header.Set(key, value)
}
}
resp, err := web.Client.Do(req)
if err != nil {
return nil, "", fmt.Errorf("unable to load <%s>: %w", url, err)
}
defer func() {
err = errors.Join(err, resp.Body.Close())
}()
data, err = io.ReadAll(resp.Body)
if err != nil {
return nil, "", fmt.Errorf("unable to load <%s>: %w", url, err)
}
contentTypeRaw := resp.Header.Get(`Content-Type`)
contentType, _, err = mime.ParseMediaType(contentTypeRaw)
if err != nil {
return nil, "", fmt.Errorf("unable to parse header <Content-Type> <%s>: %w", contentTypeRaw, err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
err = fmt.Errorf("HTTP %d error: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
// if contentType == "" {
// contentType = "application/octet-stream"
// }
return data, contentType, err
}