refactor(http): implement shared transport and clients (#1288)

* fix(http): flip to a shared transport and clients

* nice threads

* that is terrible

* fake uri for magnet

* lazy locking

* why bother with r's

* flip magic params to struct

* refactor(http-clients): use separate clients with shared transport

* refactor(http-clients): add missing license header

* refactor(http-clients): defer and fix errors

---------

Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
Kyle Sanderson 2023-12-29 14:49:22 -08:00 committed by GitHub
parent 2a4fb7750b
commit 3234f0d919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 537 additions and 391 deletions

View file

@ -6,7 +6,6 @@ package action
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
@ -197,14 +196,6 @@ func (s *service) webhook(ctx context.Context, action *domain.Action, release do
s.log.Trace().Msgf("webhook action '%s' - host: %s data: %s", action.Name, action.WebhookHost, action.WebhookData)
}
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 120 * time.Second}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, action.WebhookHost, bytes.NewBufferString(action.WebhookData))
if err != nil {
return errors.Wrap(err, "could not build request for webhook")
@ -214,8 +205,7 @@ func (s *service) webhook(ctx context.Context, action *domain.Action, release do
req.Header.Set("User-Agent", "autobrr")
start := time.Now()
res, err := client.Do(req)
res, err := s.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "could not make request for webhook")
}

View file

@ -6,10 +6,13 @@ package action
import (
"context"
"log"
"net/http"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/download_client"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/asaskevich/EventBus"
"github.com/dcarbone/zadapters/zstdlog"
@ -34,6 +37,8 @@ type service struct {
repo domain.ActionRepo
clientSvc download_client.Service
bus EventBus.Bus
httpClient *http.Client
}
func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
@ -42,6 +47,11 @@ func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_cl
repo: repo,
clientSvc: clientSvc,
bus: bus,
httpClient: &http.Client{
Timeout: time.Second * 120,
Transport: sharedhttp.TransportTLSInsecure,
},
}
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)