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 <bradfitz@tailscale.com>
pull/4752/head
Brad Fitzpatrick 2 years ago committed by Brad Fitzpatrick
parent 2903d42921
commit 02e580c1d2

@ -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"

Loading…
Cancel
Save