Skip to content

Commit

Permalink
update handle request timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ochom committed Dec 8, 2024
1 parent b15253e commit b0c1242
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions gttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gttp
import (
"bytes"
"context"
"crypto/tls"
"io"
"net/http"
"time"
Expand All @@ -19,6 +20,23 @@ type Response struct {
Body []byte
}

// getClient ...
func getClient(timeout ...time.Duration) *http.Client {
client := &http.Client{
Timeout: getTimeout(timeout...),
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: time.Second * 90,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}

return client
}

func getTimeout(timeout ...time.Duration) time.Duration {
if len(timeout) == 0 {
return 10 * time.Second
Expand All @@ -34,28 +52,26 @@ func Post(url string, headers M, body any, timeout ...time.Duration) (res *Respo
return
}

req.Close = true
for k, v := range headers {
req.Header.Set(k, v)
}

ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...))
defer cancel()

client := &http.Client{}
response, err := client.Do(req.WithContext(ctx))
reqDo, err := getClient(timeout...).Do(req.WithContext(ctx))
if err != nil {
return
}

defer response.Body.Close()
defer reqDo.Body.Close()

bodyBytes, err := io.ReadAll(response.Body)
bodyBytes, err := io.ReadAll(reqDo.Body)
if err != nil {
return
}

return &Response{response.StatusCode, bodyBytes}, nil
return &Response{reqDo.StatusCode, bodyBytes}, nil
}

// Get sends a GET request to the specified URL.
Expand All @@ -65,28 +81,26 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er
return
}

req.Close = true
for k, v := range headers {
req.Header.Set(k, v)
}

ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...))
defer cancel()

client := &http.Client{}
response, err := client.Do(req.WithContext(ctx))
reqDo, err := getClient(timeout...).Do(req.WithContext(ctx))
if err != nil {
return
}

defer response.Body.Close()
defer reqDo.Body.Close()

bodyBytes, err := io.ReadAll(response.Body)
bodyBytes, err := io.ReadAll(reqDo.Body)
if err != nil {
return
}

return &Response{response.StatusCode, bodyBytes}, nil
return &Response{reqDo.StatusCode, bodyBytes}, nil
}

// Custom sends a custom request to the specified URL.
Expand All @@ -96,26 +110,24 @@ func Custom(url, method string, headers M, body any, timeout ...time.Duration) (
return
}

req.Close = true
for k, v := range headers {
req.Header.Set(k, v)
}

ctx, cancel := context.WithTimeout(context.Background(), getTimeout(timeout...))
defer cancel()

client := &http.Client{}
response, err := client.Do(req.WithContext(ctx))
reqDo, err := getClient(timeout...).Do(req.WithContext(ctx))
if err != nil {
return
}

defer response.Body.Close()
defer reqDo.Body.Close()

bodyBytes, err := io.ReadAll(response.Body)
bodyBytes, err := io.ReadAll(reqDo.Body)
if err != nil {
return
}

return &Response{response.StatusCode, bodyBytes}, nil
return &Response{reqDo.StatusCode, bodyBytes}, nil
}

0 comments on commit b0c1242

Please sign in to comment.