forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
63 lines (49 loc) · 1.25 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tsdb
import (
"context"
)
type HandleRequestFunc func(ctx context.Context, req *Request) (*Response, error)
func HandleRequest(ctx context.Context, req *Request) (*Response, error) {
context := NewQueryContext(req.Queries, req.TimeRange)
batches, err := getBatches(req)
if err != nil {
return nil, err
}
currentlyExecuting := 0
for _, batch := range batches {
if len(batch.Depends) == 0 {
currentlyExecuting += 1
batch.Started = true
go batch.process(ctx, context)
}
}
response := &Response{}
for currentlyExecuting != 0 {
select {
case batchResult := <-context.ResultsChan:
currentlyExecuting -= 1
response.BatchTimings = append(response.BatchTimings, batchResult.Timings)
if batchResult.Error != nil {
return nil, batchResult.Error
}
for refId, result := range batchResult.QueryResults {
context.Results[refId] = result
}
for _, batch := range batches {
// not interested in started batches
if batch.Started {
continue
}
if batch.allDependenciesAreIn(context) {
currentlyExecuting += 1
batch.Started = true
go batch.process(ctx, context)
}
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
response.Results = context.Results
return response, nil
}