mirror of
https://github.com/weaveworks/scope.git
synced 2026-02-14 10:00:13 +00:00
multitenant: serialise report to buffer before sending
Seems to be faster
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/ugorji/go/codec"
|
||||
"github.com/weaveworks/scope/report"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -37,15 +39,20 @@ func respondWith(ctx context.Context, w http.ResponseWriter, code int, response
|
||||
|
||||
// Similar to the above function, but respect the request's Accept header.
|
||||
// Possibly we should do a complete parse of Accept, but for now just rudimentary check
|
||||
func respondWithReport(ctx context.Context, w http.ResponseWriter, req *http.Request, response interface{}) {
|
||||
func respondWithReport(ctx context.Context, w http.ResponseWriter, req *http.Request, response report.Report) {
|
||||
accept := req.Header.Get("Accept")
|
||||
if strings.HasPrefix(accept, "application/msgpack") {
|
||||
w.Header().Set("Content-Type", "application/msgpack")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
encoder := codec.NewEncoder(w, &codec.MsgpackHandle{})
|
||||
buf := bytes.Buffer{}
|
||||
encoder := codec.NewEncoder(&buf, &codec.MsgpackHandle{})
|
||||
if err := encoder.Encode(response); err != nil {
|
||||
log.Errorf("Error encoding response: %v", err)
|
||||
}
|
||||
if span := opentracing.SpanFromContext(ctx); span != nil {
|
||||
span.LogKV("encoded-size", len(buf.Bytes()))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/msgpack")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user