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 filter
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
@ -22,6 +21,7 @@ import (
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/utils"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/avast/retry-go/v4"
"github.com/mattn/go-shellwords"
@ -52,6 +52,8 @@ type service struct {
releaseRepo domain.ReleaseRepo
indexerSvc indexer.Service
apiService indexer.APIService
httpClient *http.Client
}
func NewService(log logger.Logger, repo domain.FilterRepo, actionRepo domain.ActionRepo, releaseRepo domain.ReleaseRepo, apiService indexer.APIService, indexerSvc indexer.Service) Service {
@ -62,6 +64,10 @@ func NewService(log logger.Logger, repo domain.FilterRepo, actionRepo domain.Act
releaseRepo: releaseRepo,
apiService: apiService,
indexerSvc: indexerSvc,
httpClient: &http.Client{
Timeout: time.Second * 120,
Transport: sharedhttp.TransportTLSInsecure,
},
}
}
@ -659,14 +665,6 @@ func (s *service) webhook(ctx context.Context, external domain.FilterExternal, r
s.log.Trace().Msgf("sending %s to external webhook filter: (%s) payload: (%s)", external.WebhookMethod, external.WebhookHost, external.WebhookData)
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 120 * time.Second}
method := http.MethodPost
if external.WebhookMethod != "" {
method = external.WebhookMethod
@ -720,7 +718,7 @@ func (s *service) webhook(ctx context.Context, external domain.FilterExternal, r
if external.WebhookData != "" && dataArgs != "" {
clonereq.Body = io.NopCloser(bytes.NewBufferString(dataArgs))
}
res, err := client.Do(clonereq)
res, err := s.httpClient.Do(clonereq)
if err != nil {
return 0, errors.Wrap(err, "could not make request for webhook")
}