5
5
"sync"
6
6
"time"
7
7
8
+ "sync/atomic"
9
+
8
10
cfg "github.com/elastic/beats/filebeat/config"
9
11
"github.com/elastic/beats/filebeat/harvester"
10
12
"github.com/elastic/beats/filebeat/input"
@@ -14,14 +16,15 @@ import (
14
16
)
15
17
16
18
type Prospector struct {
17
- cfg * common.Config // Raw config
18
- config prospectorConfig
19
- prospectorer Prospectorer
20
- spoolerChan chan * input.Event
21
- harvesterChan chan * input.Event
22
- done chan struct {}
23
- states * file.States
24
- wg sync.WaitGroup
19
+ cfg * common.Config // Raw config
20
+ config prospectorConfig
21
+ prospectorer Prospectorer
22
+ spoolerChan chan * input.Event
23
+ harvesterChan chan * input.Event
24
+ done chan struct {}
25
+ states * file.States
26
+ wg sync.WaitGroup
27
+ harvesterCounter uint64
25
28
}
26
29
27
30
type Prospectorer interface {
@@ -155,6 +158,13 @@ func (p *Prospector) createHarvester(state file.State) (*harvester.Harvester, er
155
158
}
156
159
157
160
func (p * Prospector ) startHarvester (state file.State , offset int64 ) error {
161
+
162
+ if p .config .HarvesterLimit > 0 && atomic .LoadUint64 (& p .harvesterCounter ) >= p .config .HarvesterLimit {
163
+ return fmt .Errorf ("Harvester limit reached." )
164
+ }
165
+
166
+ atomic .AddUint64 (& p .harvesterCounter , 1 )
167
+
158
168
state .Offset = offset
159
169
// Create harvester with state
160
170
h , err := p .createHarvester (state )
@@ -164,7 +174,10 @@ func (p *Prospector) startHarvester(state file.State, offset int64) error {
164
174
165
175
p .wg .Add (1 )
166
176
go func () {
167
- defer p .wg .Done ()
177
+ defer func () {
178
+ p .wg .Done ()
179
+ atomic .AddUint64 (& p .harvesterCounter , ^ uint64 (0 ))
180
+ }()
168
181
// Starts harvester and picks the right type. In case type is not set, set it to defeault (log)
169
182
h .Harvest ()
170
183
}()
0 commit comments