Skip to content

Commit

Permalink
move the lock down in the Diff method
Browse files Browse the repository at this point in the history
  • Loading branch information
electron0zero committed Sep 27, 2024
1 parent 0aaee06 commit 4239289
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pkg/collector/distinct_string_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ func (d *DistinctString) Size() int {

// Diff returns all new strings collected since the last time diff was called
func (d *DistinctString) Diff() ([]string, error) {
d.mtx.Lock()
defer d.mtx.Unlock()

// can check diffEnabled without lock because it is not modified after creation
if !d.diffEnabled {
return nil, errDiffNotEnabled
}

d.mtx.Lock()
defer d.mtx.Unlock()

ss := make([]string, 0, len(d.new))

for k := range d.new {
Expand Down
7 changes: 4 additions & 3 deletions pkg/collector/distinct_value_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ func (d *DistinctValue[T]) Size() int {
// Diff returns all new strings collected since the last time diff was called
// returns nil if diff is not enabled
func (d *DistinctValue[T]) Diff() ([]T, error) {
d.mtx.Lock()
defer d.mtx.Unlock()

// can check diffEnabled without lock because it is not modified after creation
if !d.diffEnabled {
return nil, errDiffNotEnabled
}

d.mtx.Lock()
defer d.mtx.Unlock()

ss := make([]T, 0, len(d.new))
for k := range d.new {
ss = append(ss, k)
Expand Down
6 changes: 3 additions & 3 deletions pkg/collector/scoped_distinct_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func (d *ScopedDistinctString) Exceeded() bool {

// Diff returns all new strings collected since the last time Diff was called
func (d *ScopedDistinctString) Diff() (map[string][]string, error) {
d.mtx.Lock()
defer d.mtx.Unlock()

if !d.diffEnabled {
return nil, errDiffNotEnabled
}

d.mtx.Lock()
defer d.mtx.Unlock()

ss := map[string][]string{}

for k, v := range d.cols {
Expand Down

0 comments on commit 4239289

Please sign in to comment.