Caching: Consolidate resource cache checking and updating in plugin middleware (#67002)

* Update the HandleResourceRequest function to mimic the HandleQueryRequest function

* Remove CacheResourceResponse function from interface

* revert additional thing I missed
This commit is contained in:
Michael Mandrus
2023-04-21 13:03:49 -04:00
committed by GitHub
parent d02aee2479
commit a29cfe5d46
9 changed files with 92 additions and 64 deletions

View File

@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/plugins/backendplugin"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/util/proxyutil"
"github.com/grafana/grafana/pkg/web"
)
@ -130,7 +129,7 @@ func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http
var flushStreamErr error
go func() {
flushStreamErr = hs.flushStream(req.Context(), crReq, stream, w)
flushStreamErr = hs.flushStream(stream, w)
wg.Done()
}()
@ -141,10 +140,8 @@ func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http
return flushStreamErr
}
func (hs *HTTPServer) flushStream(ctx context.Context, req *backend.CallResourceRequest, stream callResourceClientResponseStream, w http.ResponseWriter) error {
func (hs *HTTPServer) flushStream(stream callResourceClientResponseStream, w http.ResponseWriter) error {
processedStreams := 0
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for {
resp, err := stream.Recv()
if errors.Is(err, io.EOF) {
@ -200,12 +197,6 @@ func (hs *HTTPServer) flushStream(ctx context.Context, req *backend.CallResource
if _, err := w.Write(resp.Body); err != nil {
hs.log.Error("Failed to write resource response", "err", err)
} else if hs.Features.IsEnabled(featuremgmt.FlagUseCachingService) {
// Placing the new service implementation behind a feature flag until it is known to be stable
// The enterprise implementation of this function will use the headers and status of the first response,
// And append the body of any subsequent responses. It waits for the context to be canceled before caching the cumulative result.
hs.cachingService.CacheResourceResponse(ctx, req, resp)
}
if flusher, ok := w.(http.Flusher); ok {