Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/regexpool/regexpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
// Pool is the representation of a pool of regular expression.
type Pool struct {
cache *memorycache.LRUCache
fails map[string]struct{}
fails map[string]error
mu sync.RWMutex
}

Expand All @@ -54,7 +54,7 @@ func NewPool(size int) (*Pool, error) {
return nil, err
}
return &Pool{
fails: make(map[string]struct{}),
fails: make(map[string]error),
cache: cache,
}, nil
}
Expand All @@ -69,10 +69,10 @@ func (p *Pool) Get(expr string) (*regexp.Regexp, error) {
// Check if the given expression was unable to compile before
// then return the error fast.
p.mu.RLock()
_, ok := p.fails[expr]
failErr, ok := p.fails[expr]
p.mu.RUnlock()
if ok {
return nil, fmt.Errorf("unable to compile: %s", expr)
return nil, fmt.Errorf("unable to compile %q: %w", expr, failErr)
}
Comment thread
anubhavsingh2106 marked this conversation as resolved.
// Compile the expression string and cache its result.
reg, err := regexp.Compile(expr)
Expand All @@ -81,7 +81,7 @@ func (p *Pool) Get(expr string) (*regexp.Regexp, error) {
return reg, nil
}
p.mu.Lock()
p.fails[expr] = struct{}{}
p.fails[expr] = err
p.mu.Unlock()
return nil, fmt.Errorf("unable to compile: %s", expr)
return nil, fmt.Errorf("unable to compile %q: %w", expr, err)
}
11 changes: 9 additions & 2 deletions pkg/regexpool/regexpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package regexpool

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -36,6 +35,14 @@ func TestPool(t *testing.T) {
assert.NotNil(t, regex)

regex, err = pool.Get("(abc")
assert.Equal(t, fmt.Errorf("unable to compile: (abc"), err)
assert.Error(t, err)
assert.Contains(t, err.Error(), `unable to compile "(abc":`)
assert.Contains(t, err.Error(), "error parsing regexp")
assert.Nil(t, regex)
Comment thread
anubhavsingh2106 marked this conversation as resolved.

regex, err = pool.Get("(abc")
assert.Error(t, err)
assert.Contains(t, err.Error(), `unable to compile "(abc":`)
assert.Contains(t, err.Error(), "error parsing regexp")
assert.Nil(t, regex)
}