From 02e580c1d20c9a6798d0f9a913cf150f091b87a4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 27 May 2022 15:59:08 -0700 Subject: [PATCH] logtail: use http.NewRequestWithContext Saves some allocs. Not hot, but because we can now. And a const instead of a var. Change-Id: Ieb2b64534ed38051c36b2c0aa2e82739d9d0e015 Signed-off-by: Brad Fitzpatrick --- logtail/logtail.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/logtail/logtail.go b/logtail/logtail.go index 19dd6145a..8d159b358 100644 --- a/logtail/logtail.go +++ b/logtail/logtail.go @@ -395,7 +395,11 @@ func (l *Logger) awaitInternetUp(ctx context.Context) { // origlen indicates the pre-compression body length. // origlen of -1 indicates that the body is not compressed. func (l *Logger) upload(ctx context.Context, body []byte, origlen int) (uploaded bool, err error) { - req, err := http.NewRequest("POST", l.url, bytes.NewReader(body)) + const maxUploadTime = 45 * time.Second + ctx, cancel := context.WithTimeout(ctx, maxUploadTime) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "POST", l.url, bytes.NewReader(body)) if err != nil { // I know of no conditions under which this could fail. // Report it very loudly. @@ -408,11 +412,6 @@ func (l *Logger) upload(ctx context.Context, body []byte, origlen int) (uploaded } req.Header["User-Agent"] = nil // not worth writing one; save some bytes - maxUploadTime := 45 * time.Second - ctx, cancel := context.WithTimeout(ctx, maxUploadTime) - defer cancel() - req = req.WithContext(ctx) - compressedNote := "not-compressed" if origlen != -1 { compressedNote = "compressed"