Skip to content

Commit

Permalink
XHTTP client: Simplify OpenDownload()
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Dec 20, 2024
1 parent 53b04d5 commit 3c82d4e
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions transport/internet/splithttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3c82d4e

Please sign in to comment.