From 3c82d4eb559ef2d3e0a8ce47afe7624b757eb7b9 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Fri, 20 Dec 2024 07:53:45 +0000 Subject: [PATCH] XHTTP client: Simplify OpenDownload() --- transport/internet/splithttp/client.go | 49 ++++++++------------------ 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/transport/internet/splithttp/client.go b/transport/internet/splithttp/client.go index 3baea6f070d7..40beae445c61 100644 --- a/transport/internet/splithttp/client.go +++ b/transport/internet/splithttp/client.go @@ -104,57 +104,36 @@ func (c *DefaultDialerClient) OpenDownload(ctx context.Context, baseURL string) // logs gotConn := done.New() - var downResponse io.ReadCloser - gotDownResponse := done.New() - ctx, ctxCancel := context.WithCancel(ctx) + ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ + GotConn: func(connInfo httptrace.GotConnInfo) { + remoteAddr = connInfo.Conn.RemoteAddr() + localAddr = connInfo.Conn.LocalAddr() + gotConn.Close() + }, + }) - go func() { - trace := &httptrace.ClientTrace{ - GotConn: func(connInfo httptrace.GotConnInfo) { - remoteAddr = connInfo.Conn.RemoteAddr() - localAddr = connInfo.Conn.LocalAddr() - gotConn.Close() - }, - } - - // in case we hit an error, we want to unblock this part - defer gotConn.Close() - - ctx = httptrace.WithClientTrace(ctx, trace) - - req, err := http.NewRequestWithContext( - ctx, - "GET", - baseURL, - nil, - ) - if err != nil { - errors.LogInfoInner(ctx, err, "failed to construct download http request") - gotDownResponse.Close() - return - } + req, _ := http.NewRequestWithContext(ctx, "GET", baseURL, nil) + req.Header = c.transportConfig.GetRequestHeader() - req.Header = c.transportConfig.GetRequestHeader() + var downResponse io.ReadCloser + gotDownResponse := done.New() + go func() { + defer gotDownResponse.Close() response, err := c.client.Do(req) - gotConn.Close() if err != nil { + gotConn.Close() errors.LogInfoInner(ctx, err, "failed to send download http request") - gotDownResponse.Close() return } - if response.StatusCode != 200 { // c.closed = true response.Body.Close() errors.LogInfo(ctx, "invalid status code on download:", response.Status) - gotDownResponse.Close() return } - downResponse = response.Body - gotDownResponse.Close() }() <-gotConn.Wait()