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

@ -5,7 +5,6 @@ package notification
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
@ -15,6 +14,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog"
@ -50,12 +50,18 @@ const (
type discordSender struct {
log zerolog.Logger
Settings domain.Notification
httpClient *http.Client
}
func NewDiscordSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
return &discordSender{
log: log.With().Str("sender", "discord").Logger(),
Settings: settings,
httpClient: &http.Client{
Timeout: time.Second * 30,
Transport: sharedhttp.Transport,
},
}
}
@ -80,27 +86,20 @@ func (a *discordSender) Send(event domain.NotificationEvent, payload domain.Noti
req.Header.Set("Content-Type", "application/json")
//req.Header.Set("User-Agent", "autobrr")
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 30 * time.Second}
res, err := client.Do(req)
res, err := a.httpClient.Do(req)
if err != nil {
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
a.log.Trace().Msgf("discord status: %v response: %v", res.StatusCode, string(body))
// discord responds with 204, Notifiarr with 204 so lets take all 200 as ok