Skip to content

Commit a209f84

Browse files
fix: race condition for async db queries (#661)
1 parent 91c4c93 commit a209f84

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

internal/inputs/database.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ func ProcessQueries(dataStore *[]interface{}, yml *load.Config, apiNo int) {
9797
// execute queries async else do synchronously
9898
if api.DBAsync {
9999
var wg sync.WaitGroup
100+
var mu sync.Mutex
100101
wg.Add(len(api.DBQueries))
101102
for _, query := range api.DBQueries {
102103
go func(query load.Command) {
103104
defer wg.Done()
104-
checkAndRunQuery(db, query, api, yml, dataStore)
105+
localStore := []interface{}{}
106+
checkAndRunQuery(db, query, api, yml, &localStore)
107+
mu.Lock()
108+
*dataStore = append(*dataStore, localStore...)
109+
mu.Unlock()
105110
}(query)
106111
}
107112
wg.Wait()

0 commit comments

Comments
 (0)