Query caching: add request deduplication middleware (#110892)
* secrets: update test to accept []byte(nil) and []byte{} (#110630)
Co-authored-by: Matheus Macabu <macabu.matheus@gmail.com>
* Query caching: add request deduplication middleware
* log error if unable to build cache key
* remove TODO
* always use req.PluginContext.DataSourceInstanceSettings.UID
* make update-workspace
---------
Co-authored-by: Matheus Macabu <macabu.matheus@gmail.com>
This commit is contained in:
@@ -4,7 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/handlertest"
|
||||
@@ -322,3 +325,51 @@ func TestCachingMiddleware(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestRequestDeduplicationMiddleware(t *testing.T) {
|
||||
t.Run("deduplicates requests issuing the same query", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := newMockMiddlewareHandler()
|
||||
middleware := newRequestDeduplicationMiddleware(nil, handler)
|
||||
|
||||
req := backend.QueryDataRequest{
|
||||
PluginContext: backend.PluginContext{
|
||||
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{
|
||||
UID: "uid",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
for range 2 {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resp, err := middleware.QueryData(t.Context(), &req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, &backend.QueryDataResponse{}, resp)
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
require.EqualValues(t, 1, handler.QueryDataCalls)
|
||||
})
|
||||
}
|
||||
|
||||
type mockMiddlewareHandler struct {
|
||||
backend.BaseHandler
|
||||
QueryDataCalls int32
|
||||
}
|
||||
|
||||
func newMockMiddlewareHandler() *mockMiddlewareHandler {
|
||||
return &mockMiddlewareHandler{}
|
||||
}
|
||||
|
||||
func (m *mockMiddlewareHandler) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
atomic.AddInt32(&m.QueryDataCalls, 1)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
return &backend.QueryDataResponse{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user