protect filterset race

This commit is contained in:
Daisuke Maki 2016-06-23 04:14:44 -04:00
parent 464919f631
commit ca7a273d98
2 changed files with 16 additions and 3 deletions

View file

@ -17,20 +17,28 @@ import (
"golang.org/x/net/context"
)
func (fx *FilterSet) Reset() {
fx.current = 0
func (fs *FilterSet) Reset() {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.current = 0
}
func (fs *FilterSet) Size() int {
fs.mutex.Lock()
defer fs.mutex.Unlock()
return len(fs.filters)
}
func (fs *FilterSet) Add(lf LineFilter) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.filters = append(fs.filters, lf)
return nil
}
func (fs *FilterSet) Rotate() {
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.current++
if fs.current >= len(fs.filters) {
fs.current = 0
@ -41,6 +49,8 @@ func (fs *FilterSet) Rotate() {
}
func (fs *FilterSet) SetCurrentByName(name string) error {
fs.mutex.Lock()
defer fs.mutex.Unlock()
for i, f := range fs.filters {
if f.String() == name {
fs.current = i
@ -51,6 +61,8 @@ func (fs *FilterSet) SetCurrentByName(name string) error {
}
func (fs *FilterSet) Current() LineFilter {
fs.mutex.Lock()
defer fs.mutex.Unlock()
return fs.filters[fs.current]
}

View file

@ -405,8 +405,9 @@ type Query struct {
type FilterQuery Query
type FilterSet struct {
filters []LineFilter
current int
filters []LineFilter
mutex sync.Mutex
}
// Source implements pipline.Source, and is the buffer for the input