Skip to content

Commit

Permalink
Move worker pool reservations down stack to avoid starvation during r…
Browse files Browse the repository at this point in the history
…etry backoff (#36)
  • Loading branch information
Jeff Nadler authored Mar 1, 2022
1 parent 62e1efb commit 2e182d4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions node/elasticsearch/elastic_index_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ func (c *ElasticIndexClient) batch(ctx context.Context) {
}

func (c *ElasticIndexClient) retryBulkIndex(messages []*eventIndexRequest, retryCount int) {
c.metrics.AvailableBatchRoutines.Set(float64(len(c.pool)))
<-c.pool

// make the bulk index calls to ES on a goroutine so that the caller can continue with the next batch
go func() {
defer func() { c.pool <- 1 }()

// when the whole batch fails, it typically indicates that ES is unavailable, so we keep retrying forever
// and once all the pool workers are in use backpressure will flow up to the nodes above this one
for i := 0; ; i++ {
Expand All @@ -155,6 +151,10 @@ func (c *ElasticIndexClient) retryBulkIndex(messages []*eventIndexRequest, retry
}

func (c *ElasticIndexClient) doBulkIndex(requests []*eventIndexRequest, retryCount int) error {
c.metrics.AvailableBatchRoutines.Set(float64(len(c.pool)))
<-c.pool
defer func() { c.pool <- 1 }()

// nothing to do
if len(requests) == 0 {
return nil
Expand Down

0 comments on commit 2e182d4

Please sign in to comment.