From e764812efe451e47b428c22ce0c8815a1bbef281 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 1 Mar 2024 11:34:34 +0100 Subject: [PATCH] bump go-bits --- go.mod | 2 +- go.sum | 4 ++-- internal/api/removed_test.go | 3 ++- .../github.com/sapcc/go-bits/assert/http.go | 6 +++++ .../sapcc/go-bits/audittools/rabbitmq.go | 5 +++-- .../sapcc/go-bits/httpext/server.go | 11 +++++++++- .../sapcc/go-bits/respondwith/pkg.go | 22 +++++++++++++------ vendor/modules.txt | 2 +- 8 files changed, 40 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 7122a7817..226d1f466 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/prometheus/common v0.49.0 github.com/rs/cors v1.10.1 github.com/sapcc/go-api-declarations v1.10.9 - github.com/sapcc/go-bits v0.0.0-20240222221204-90b493ffdee9 + github.com/sapcc/go-bits v0.0.0-20240301103239-440d8f9152e0 go.uber.org/automaxprocs v1.5.3 golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index bd7248fac..842370e3b 100644 --- a/go.sum +++ b/go.sum @@ -153,8 +153,8 @@ github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo= github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/sapcc/go-api-declarations v1.10.9 h1:k+F3W0FTyYLazII4hdFaWLJ7L+MQRcFBD9I9P/hUNBs= github.com/sapcc/go-api-declarations v1.10.9/go.mod h1:83R3hTANhuRXt/pXDby37IJetw8l7DG41s33Tp9NXxI= -github.com/sapcc/go-bits v0.0.0-20240222221204-90b493ffdee9 h1:rbg802tNP3trhxlzQCAQHHnd1UAfjiSB9UR7JqdRxTU= -github.com/sapcc/go-bits v0.0.0-20240222221204-90b493ffdee9/go.mod h1:w+u3Y/Dt7/1F/Km1skkratCVrexj5o0zes1Ds7aJFhE= +github.com/sapcc/go-bits v0.0.0-20240301103239-440d8f9152e0 h1:7RWqq3EEimbvrDX0s1McR/nk02QN3Dx1vURS5zh2Y8c= +github.com/sapcc/go-bits v0.0.0-20240301103239-440d8f9152e0/go.mod h1:F26a4iH+yHhpQxn+jCe8gQvtr7KdMnkyqLLVoNNgFt8= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= diff --git a/internal/api/removed_test.go b/internal/api/removed_test.go index bab0f0b1f..e797d0a5f 100644 --- a/internal/api/removed_test.go +++ b/internal/api/removed_test.go @@ -19,6 +19,7 @@ package api import ( + "bytes" "net/http" "testing" @@ -56,7 +57,7 @@ func TestForbidClusterIDHeader(t *testing.T) { Path: "/v1/clusters/current", Header: map[string]string{"X-Limes-Cluster-Id": "current"}, ExpectStatus: http.StatusOK, - ExpectBody: assert.ByteData(respBody), + ExpectBody: assert.ByteData(bytes.TrimSpace(respBody)), }.Check(t, s.Handler) // same request with X-Limes-Cluster-Id is rejected diff --git a/vendor/github.com/sapcc/go-bits/assert/http.go b/vendor/github.com/sapcc/go-bits/assert/http.go index 78db514f1..084c5119c 100644 --- a/vendor/github.com/sapcc/go-bits/assert/http.go +++ b/vendor/github.com/sapcc/go-bits/assert/http.go @@ -20,6 +20,7 @@ package assert import ( + "bytes" "fmt" "io" "net/http" @@ -114,6 +115,11 @@ func (r HTTPRequest) Check(t *testing.T, handler http.Handler) (resp *http.Respo } if r.ExpectBody != nil { + //json.Encoder.Encode() adds a stupid extra newline that we want to ignore + if response.Header.Get("Content-Type") == "application/json" { + responseBytes = bytes.TrimSuffix(responseBytes, []byte("\n")) + } + requestInfo := fmt.Sprintf("%s %s", r.Method, r.Path) if !r.ExpectBody.AssertResponseBody(t, requestInfo, responseBytes) { hadErrors = true diff --git a/vendor/github.com/sapcc/go-bits/audittools/rabbitmq.go b/vendor/github.com/sapcc/go-bits/audittools/rabbitmq.go index 211bbe6de..3bd29f32c 100644 --- a/vendor/github.com/sapcc/go-bits/audittools/rabbitmq.go +++ b/vendor/github.com/sapcc/go-bits/audittools/rabbitmq.go @@ -112,8 +112,9 @@ func (c *RabbitConnection) PublishEvent(event *cadf.Event) error { false, // mandatory: don't publish if no queue is bound that matches the routing key false, // immediate: don't publish if no consumer on the matched queue is ready to accept the delivery amqp.Publishing{ - ContentType: "text/plain", - Body: b, + ContentType: "text/plain", + Body: b, + DeliveryMode: amqp.Persistent, // Ensure message persistence }, ) } diff --git a/vendor/github.com/sapcc/go-bits/httpext/server.go b/vendor/github.com/sapcc/go-bits/httpext/server.go index 699127523..379582aa3 100644 --- a/vendor/github.com/sapcc/go-bits/httpext/server.go +++ b/vendor/github.com/sapcc/go-bits/httpext/server.go @@ -33,6 +33,15 @@ import ( var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM} +// ShutdownTimeout is the timeout that ListenAndServeContext() will impose on +// server.Shutdown() before forcefully terminating request handlers that are +// still in progress. +// +// The default timeout is quite lenient to accommodate long-running requests, +// but it can be lowered for servers running in an interactive terminal session +// where a quick response to Ctrl-C is more important. +var ShutdownTimeout = 30 * time.Second + // ContextWithSIGINT creates a new context.Context using the provided Context, and // launches a goroutine that cancels the Context when an interrupt signal is caught. // @@ -80,7 +89,7 @@ func ListenAndServeContext(ctx context.Context, addr string, handler http.Handle logg.Info("Shutting down HTTP server...") - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), ShutdownTimeout) err := server.Shutdown(ctx) cancel() waitForServerShutdown <- err diff --git a/vendor/github.com/sapcc/go-bits/respondwith/pkg.go b/vendor/github.com/sapcc/go-bits/respondwith/pkg.go index 754e53785..0369ae5a3 100644 --- a/vendor/github.com/sapcc/go-bits/respondwith/pkg.go +++ b/vendor/github.com/sapcc/go-bits/respondwith/pkg.go @@ -25,18 +25,26 @@ package respondwith import ( "encoding/json" "net/http" + + "github.com/sapcc/go-bits/logg" ) // JSON serializes the given data into an HTTP response body // The `code` argument specifies the HTTP response code, usually 200. func JSON(w http.ResponseWriter, code int, data any) { - bytes, err := json.Marshal(&data) - if err == nil { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(code) - w.Write(bytes) //nolint:errcheck - } else { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + + //NOTE 1: We used to do json.Marshal() here, but switched to json.Encoder.Encode() to avoid + //allocating an additional []byte buffer with the entire JSON response before writing it out. + // + //NOTE 2: Intuition would suggest to wrap `w` in a bufio.Writer, but json.Encoder already writes + //into an internal buffer first and then sends that entire buffer into w.Write() all at once, so + //we do not need to add buffering ourselves. + + err := json.NewEncoder(w).Encode(data) + if err != nil { + logg.Error("could not respondwith.JSON(): " + err.Error()) } } diff --git a/vendor/modules.txt b/vendor/modules.txt index b06ffe90b..8d4f44268 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -147,7 +147,7 @@ github.com/sapcc/go-api-declarations/internal/marshal github.com/sapcc/go-api-declarations/limes github.com/sapcc/go-api-declarations/limes/rates github.com/sapcc/go-api-declarations/limes/resources -# github.com/sapcc/go-bits v0.0.0-20240222221204-90b493ffdee9 +# github.com/sapcc/go-bits v0.0.0-20240301103239-440d8f9152e0 ## explicit; go 1.21 github.com/sapcc/go-bits/assert github.com/sapcc/go-bits/audittools