configure http client timeout (#169)

Reviewed-on: https://git.ptzo.gdn/feditools/relay/pulls/169
Co-authored-by: Tyr Mactire <tyr@pettingzoo.co>
Co-committed-by: Tyr Mactire <tyr@pettingzoo.co>
This commit is contained in:
Tyr Mactire 2022-11-28 04:20:20 +00:00 committed by PettingZoo Gitea
parent c62733e41b
commit e806385400
No known key found for this signature in database
GPG Key ID: 39788A4390A1372F
14 changed files with 66 additions and 27 deletions

View File

@ -80,7 +80,15 @@ var Start action.Action = func(topCtx context.Context) error {
}()
// create http client
httpClient, err := http.NewClient(ctx)
httpClient, err := http.NewClient(
ctx,
fmt.Sprintf("Go-http-client/2.0 (%s/%s; +https://%s/)",
viper.GetString(config.Keys.ApplicationName),
viper.GetString(config.Keys.SoftwareVersion),
viper.GetString(config.Keys.ServerExternalHostname),
),
viper.GetInt(config.Keys.HTTPClientTimeout),
)
if err != nil {
l.Errorf("http client: %s", err.Error())
cancel()

View File

@ -76,7 +76,15 @@ var Start action.Action = func(ctx context.Context) error {
}()
// create http client
httpClient, err := http.NewClient(ctx)
httpClient, err := http.NewClient(
ctx,
fmt.Sprintf("Go-http-client/2.0 (%s/%s; +https://%s/)",
viper.GetString(config.Keys.ApplicationName),
viper.GetString(config.Keys.SoftwareVersion),
viper.GetString(config.Keys.ServerExternalHostname),
),
viper.GetInt(config.Keys.HTTPClientTimeout),
)
if err != nil {
l.Errorf("http client: %s", err.Error())
cancel()

View File

@ -0,0 +1,11 @@
package flag
import (
"git.ptzo.gdn/feditools/relay/internal/config"
"github.com/spf13/cobra"
)
// HTTPClient adds all flags for configuring the http client.
func HTTPClient(cmd *cobra.Command, values config.Values) {
cmd.PersistentFlags().Int(config.Keys.HTTPClientTimeout, values.HTTPClientTimeout, usage.HTTPClientTimeout)
}

View File

@ -7,6 +7,7 @@ import (
// Server adds all flags for running the server.
func Server(cmd *cobra.Command, values config.Values) {
HTTPClient(cmd, values)
Redis(cmd, values)
Runner(cmd, values)

View File

@ -7,6 +7,7 @@ import (
// Worker adds all flags for running the server.
func Worker(cmd *cobra.Command, values config.Values) {
HTTPClient(cmd, values)
Redis(cmd, values)
Runner(cmd, values)

2
go.mod
View File

@ -3,7 +3,7 @@ module git.ptzo.gdn/feditools/relay
go 1.19
require (
git.ptzo.gdn/feditools/go-lib v0.19.0
git.ptzo.gdn/feditools/go-lib v0.19.1
github.com/allegro/bigcache/v3 v3.1.0
github.com/contribsys/faktory v1.6.2
github.com/contribsys/faktory_worker_go v1.6.0

4
go.sum
View File

@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.ptzo.gdn/feditools/go-lib v0.19.0 h1:VDY92FYLTg64IMvpheF2UJU50hvTGPLxiCzxfKL3OYY=
git.ptzo.gdn/feditools/go-lib v0.19.0/go.mod h1:AjT/r7wnZzZqU//GEQ1YGwepx1Pt7BIILTG4edoY+Uw=
git.ptzo.gdn/feditools/go-lib v0.19.1 h1:tX4jo4EG9SIciniUkBuLYcTrWckqjYfPToNMFvmSnlg=
git.ptzo.gdn/feditools/go-lib v0.19.1/go.mod h1:AjT/r7wnZzZqU//GEQ1YGwepx1Pt7BIILTG4edoY+Uw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=

View File

@ -26,6 +26,9 @@ type KeyNames struct {
DbTLSMode string
DbTLSCACert string
// HTTP Client
HTTPClientTimeout string
// redis
RedisAddress string
RedisDB string
@ -87,6 +90,9 @@ var Keys = KeyNames{
DbTLSMode: "db-tls-mode",
DbTLSCACert: "db-tls-ca-cert",
// HTTP Client
HTTPClientTimeout: "http-client-timeout",
// redis
RedisAddress: "redis-address",
RedisDB: "redis-db",

View File

@ -26,6 +26,9 @@ type Values struct {
DbTLSMode string
DbTLSCACert string
// HTTP Client
HTTPClientTimeout int
// redis
RedisAddress string
RedisDB int
@ -84,6 +87,9 @@ var Defaults = Values{
DbTLSMode: "disable",
DbTLSCACert: "",
// HTTP Client
HTTPClientTimeout: 5,
// redis
RedisAddress: "localhost:6379",
RedisDB: 0,

View File

@ -40,7 +40,7 @@ func New(d db.DB, t *fedihelper.Transport, k kv.KV, tok *token.Tokenizer) (*Modu
tokz: tok,
}
fedi, err := fedihelper.New(k, t, appName, fediHelpers)
fedi, err := fedihelper.New(k, t, appName, fediHelpers, viper.GetInt(config.Keys.HTTPClientTimeout))
if err != nil {
return nil, err
}

View File

@ -2,32 +2,27 @@ package http
import (
"context"
"fmt"
"git.ptzo.gdn/feditools/relay/internal/config"
"github.com/spf13/viper"
"io"
"net/http"
"time"
)
func NewClient(_ context.Context) (*Client, error) {
userAgent := fmt.Sprintf("Go-http-client/2.0 (%s/%s; +https://%s/)",
viper.GetString(config.Keys.ApplicationName),
viper.GetString(config.Keys.SoftwareVersion),
viper.GetString(config.Keys.ServerExternalHostname),
)
func NewClient(_ context.Context, userAgent string, clientTimeout int) (*Client, error) {
return &Client{
userAgent: userAgent,
clientTimeout: time.Duration(clientTimeout),
userAgent: userAgent,
}, nil
}
type Client struct {
userAgent string
clientTimeout time.Duration
userAgent string
}
// Do runs a request with the http client.
func (c *Client) Do(req *http.Request) (resp *http.Response, err error) {
client := &http.Client{
Timeout: c.clientTimeout * time.Second,
Transport: c.Transport(),
}

View File

@ -13,7 +13,7 @@ const actorCacheExp = 60 * time.Minute
const nodeInfoCacheExp = 60 * time.Minute
// New creates a new fedi module.
func New(k KV, t *Transport, clientName string, helpers []Helper) (*FediHelper, error) {
func New(k KV, t *Transport, clientName string, helpers []Helper, httpClientTimeout int) (*FediHelper, error) {
newFedi := &FediHelper{
http: t,
kv: k,
@ -24,9 +24,10 @@ func New(k KV, t *Transport, clientName string, helpers []Helper) (*FediHelper,
helpers: map[SoftwareName]Helper{},
appClientName: clientName,
actorCacheExp: actorCacheExp,
nodeinfoCacheExp: nodeInfoCacheExp,
appClientName: clientName,
actorCacheExp: actorCacheExp,
httpClientTimeout: time.Duration(httpClientTimeout) * time.Second,
nodeinfoCacheExp: nodeInfoCacheExp,
}
// add helpers
@ -51,10 +52,11 @@ type FediHelper struct {
helpers map[SoftwareName]Helper
appClientName string
actorCacheExp time.Duration
nodeinfoCacheExp time.Duration
requestGroup singleflight.Group
appClientName string
actorCacheExp time.Duration
httpClientTimeout time.Duration
nodeinfoCacheExp time.Duration
requestGroup singleflight.Group
}
func (f *FediHelper) SetCreateAccountHandler(handler CreateAccountHandler) {

View File

@ -14,6 +14,7 @@ type HttpClient interface {
func (f *FediHelper) httpClient() *http.Client {
// i'm sorry
return &http.Client{
Timeout: f.httpClientTimeout,
Transport: f.http.Client().Transport(),
}
}

2
vendor/modules.txt vendored
View File

@ -1,4 +1,4 @@
# git.ptzo.gdn/feditools/go-lib v0.19.0
# git.ptzo.gdn/feditools/go-lib v0.19.1
## explicit; go 1.17
git.ptzo.gdn/feditools/go-lib
git.ptzo.gdn/feditools/go-lib/database