-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
180 lines (148 loc) · 3.64 KB
/
engine.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
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
package core
import (
"context"
"errors"
"fmt"
"regexp"
"sync"
"time"
)
type (
Product struct {
Name string
GTIN string
URL string
}
Website struct {
URL string
}
Scraper interface {
Scrape(product string) ([]Product, error)
Info() Website
}
Config struct {
Debug bool
Logger Logger
MaxConcurrency int
Timeout time.Duration
}
Engine struct {
config Config
scrapers []Scraper
}
// Option configures the Engine
Option func(*Engine)
)
var (
ErrMissingScraper = errors.New("missing scraper")
ErrProductNotFound = errors.New("product not found")
gtinRegex = regexp.MustCompile(`^\d{13}$`)
)
func NewEngine(opts ...Option) *Engine {
e := &Engine{}
for _, opt := range opts {
opt(e)
}
return e
}
// WithTimeout sets the timeout for the Engine as a whole operation
func WithTimeout(d time.Duration) Option {
return func(e *Engine) {
e.config.Timeout = d
}
}
// WithLogger sets the logger for the Engine
func WithLogger(l Logger) Option {
return func(e *Engine) {
e.config.Logger = l
}
}
// WithDebug sets the debug mode for the Engine
func WithDebug() Option {
return func(e *Engine) {
e.config.Debug = true
}
}
func WithMaxConcurrency(n int) Option {
return func(e *Engine) {
e.config.MaxConcurrency = n
}
}
func WithScrapers(s ...Scraper) Option {
return func(e *Engine) {
e.scrapers = append(e.scrapers, s...)
}
}
func (e *Engine) setup() {
if e.config.Logger == nil {
e.config.Logger = &DefaultLogger{}
}
if e.config.MaxConcurrency == 0 {
e.config.MaxConcurrency = 10
}
}
func (e *Engine) Search(product string) ([]Product, error) {
e.setup()
if len(e.scrapers) == 0 {
return nil, ErrMissingScraper
}
var ctx context.Context
var cancel context.CancelFunc
if e.config.Timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), e.config.Timeout)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
defer cancel() // Ensure all paths cancel the context to avoid context leak
var wg sync.WaitGroup
results := []Product{}
resultsMutex := &sync.Mutex{}
// Create a buffered channel to limit the number of goroutines
semaphore := make(chan struct{}, e.config.MaxConcurrency)
for idx, scraper := range e.scrapers {
e.debug("Scraping", scraper.Info().URL)
select {
case semaphore <- struct{}{}: // Block if there are already MaxConcurrency goroutines running
case <-ctx.Done(): // Check if the context deadline has been reached
e.config.Logger.Error(fmt.Sprintf("timeout reached before starting all scrapers (exec: %d | non-exec: %d)", idx+1, len(e.scrapers[idx:])))
return results, ctx.Err()
}
wg.Add(1)
go func(s Scraper) {
defer wg.Done()
defer func() { <-semaphore }() // Release the spot in the semaphore when the goroutine completes
if ctx.Err() == nil { // Check context error to avoid executing if already timed out
products, err := s.Scrape(product)
if err == nil {
resultsMutex.Lock()
results = append(results, products...)
resultsMutex.Unlock()
} else {
e.config.Logger.Error(fmt.Errorf("scraper %T failed: %w", s, err))
}
}
}(scraper)
}
wg.Wait() // Wait for all goroutines to finish
if isGTIN(product) {
filteredResults := make([]Product, 0)
for _, result := range results {
if result.GTIN == product {
filteredResults = append(filteredResults, result)
}
}
results = filteredResults
}
return results, nil
}
func (e *Engine) AddScraper(s Scraper) {
e.scrapers = append(e.scrapers, s)
}
func (e *Engine) debug(args ...any) {
if e.config.Debug {
e.config.Logger.Debug(args...)
}
}
func isGTIN(s string) bool {
return gtinRegex.MatchString(s)
}