Skip to content

Commit

Permalink
fix: http client requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 22, 2024
1 parent e982308 commit 0be3889
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"syscall"
"time"

"github.com/joho/godotenv"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/sync/errgroup"
)
Expand All @@ -36,7 +36,17 @@ var metricsFile string = "/app/db/metrics.sqlite"
var collectorEnabled bool = false
var collectorRetentionPeriodDays int = 7

// HTTP client with connection pooling
// HTTP client (Docker) with connection pooling
var dockerHttpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
Timeout: 10 * time.Second,
}

// HTTP client (other) with connection pooling
var httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
Expand Down Expand Up @@ -266,6 +276,11 @@ func main() {
log.Fatal(err)
}

// Use Unix socket transport
dockerHttpClient.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", "/var/run/docker.sock")
}

r := gin.Default()
r.GET("/api/health", func(c *gin.Context) {
c.String(200, "ok")
Expand Down Expand Up @@ -371,10 +386,5 @@ func makeDockerRequest(url string) (*http.Response, error) {
}
req.Header.Set("Content-Type", "application/json")

// Use Unix socket transport
httpClient.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", "/var/run/docker.sock")
}

return httpClient.Do(req)
return dockerHttpClient.Do(req)
}

0 comments on commit 0be3889

Please sign in to comment.