Skip to content

Commit

Permalink
fix: 单Topic限制并发请求优化
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetzhDing committed Jul 16, 2021
1 parent 2db1cff commit 3b58d86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tencent-cls-grafana-datasource",
"version": "1.0.5",
"version": "1.0.6",
"description": "Log Service datasource plugin for Grafana",
"author": "Tencent Cloud",
"license": "Apache-2.0",
Expand Down
16 changes: 15 additions & 1 deletion pkg/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,31 @@ type clsDatasource struct {
// The QueryDataResponse contains a map of RefID to the response for each query, and each response
// contains Frames ([]*Frame).

const (
MaxQueryDataConcurrentGoroutines = 20 // 每个TopicId拥有一个容量为20的并发池
)

// TopicId为key, 并发池为value
var topicIdGoroutinesMap = make(map[string]chan int)

func (td *clsDatasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
response := backend.NewQueryDataResponse()

apiOpts := GetApiOpts(*req.PluginContext.DataSourceInstanceSettings)

if topicIdGoroutinesMap[apiOpts.TopicId] == nil {
topicIdGoroutinesMap[apiOpts.TopicId] = make(chan int, MaxQueryDataConcurrentGoroutines)
}
queryDataGoroutines := topicIdGoroutinesMap[apiOpts.TopicId]

var wg sync.WaitGroup
for _, query := range req.Queries {
wg.Add(1)
go func(q backend.DataQuery) {
defer wg.Done()
queryDataGoroutines <- 1
response.Responses[q.RefID] = td.query(ctx, q, apiOpts)
wg.Done()
<-queryDataGoroutines
}(query)
}
wg.Wait()
Expand Down

0 comments on commit 3b58d86

Please sign in to comment.