Skip to content

Commit a13b84c

Browse files
refactor: Simplifies hisRead concurrency
This depends on a single channel instead of two channels and a wait group.
1 parent 39255c1 commit a13b84c

1 file changed

Lines changed: 14 additions & 28 deletions

File tree

pkg/plugin/datasource.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sort"
88
"strconv"
99
"strings"
10-
"sync"
1110
"time"
1211

1312
"github.com/NeedleInAJayStack/haystack"
@@ -205,37 +204,24 @@ func (datasource *Datasource) query(ctx context.Context, pCtx backend.PluginCont
205204
return backend.ErrDataResponse(backend.StatusBadRequest, errMsg)
206205
}
207206

208-
// Function to read a single point and send it to a channel.
209-
readPoint := func(point haystack.Row, hisReadChannel chan haystack.Grid, wg *sync.WaitGroup) {
210-
hisRead, err := datasource.hisRead(point, query.TimeRange)
211-
if err != nil {
212-
log.DefaultLogger.Error(err.Error())
213-
}
214-
hisReadChannel <- hisRead // hisRead is empty under error condition
215-
wg.Done()
216-
}
217-
218-
// Start a goroutine to collect all the grids into a slice.
219-
hisReadChannel := make(chan haystack.Grid)
220-
combinedChannel := make(chan []haystack.Grid)
221-
go func() {
222-
grids := []haystack.Grid{}
223-
for grid := range hisReadChannel {
224-
grids = append(grids, grid)
225-
}
226-
combinedChannel <- grids
227-
}()
228-
229207
// Read all the points in parallel using goroutines.
230-
var wg sync.WaitGroup
231-
wg.Add(len(points))
208+
hisReadChannel := make(chan haystack.Grid)
232209
for _, point := range points {
233-
go readPoint(point, hisReadChannel, &wg)
210+
go func() {
211+
hisRead, err := datasource.hisRead(point, query.TimeRange)
212+
if err != nil {
213+
log.DefaultLogger.Error(err.Error())
214+
}
215+
hisReadChannel <- hisRead // hisRead is empty under error condition
216+
}()
217+
}
218+
219+
grids := []haystack.Grid{}
220+
for _ = range len(points) {
221+
grid := <-hisReadChannel
222+
grids = append(grids, grid)
234223
}
235-
wg.Wait()
236-
close(hisReadChannel)
237224

238-
grids := <-combinedChannel
239225
response := responseFromGrids(grids)
240226
// Make the display name on the "val" fields the names of the points.
241227
for _, frame := range response.Frames {

0 commit comments

Comments
 (0)