mirror of
https://git.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-25 21:31:27 +00:00
Fix http client
- Remove automatic retries on 429 - Tweak http client config for better connection re-using
This commit is contained in:
parent
237182c171
commit
140dfe2f63
18
main.go
18
main.go
|
@ -4,10 +4,12 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"bloat/config"
|
"bloat/config"
|
||||||
"bloat/kv"
|
"bloat/kv"
|
||||||
|
@ -26,6 +28,20 @@ func errExit(err error) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupHttp() {
|
||||||
|
tr := http.DefaultTransport.(*http.Transport)
|
||||||
|
tr.MaxIdleConnsPerHost = 30
|
||||||
|
tr.MaxIdleConns = 300
|
||||||
|
tr.ForceAttemptHTTP2 = false
|
||||||
|
tr.DialContext = (&net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
KeepAlive: 3 * time.Minute,
|
||||||
|
DualStack: true,
|
||||||
|
}).DialContext
|
||||||
|
client := http.DefaultClient
|
||||||
|
client.Transport = tr
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
opts, _, err := util.Getopts(os.Args, "f:")
|
opts, _, err := util.Getopts(os.Args, "f:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -93,6 +109,8 @@ func main() {
|
||||||
logger = log.New(lf, "", log.LstdFlags)
|
logger = log.New(lf, "", log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupHttp()
|
||||||
|
|
||||||
s := service.NewService(config.ClientName, config.ClientScope,
|
s := service.NewService(config.ClientName, config.ClientScope,
|
||||||
config.ClientWebsite, customCSS, config.PostFormats, renderer,
|
config.ClientWebsite, customCSS, config.PostFormats, renderer,
|
||||||
sessionRepo, appRepo, config.SingleInstance)
|
sessionRepo, appRepo, config.SingleInstance)
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/tomnomnom/linkheader"
|
"github.com/tomnomnom/linkheader"
|
||||||
)
|
)
|
||||||
|
@ -30,7 +29,7 @@ type Config struct {
|
||||||
|
|
||||||
// Client is a API client for mastodon.
|
// Client is a API client for mastodon.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
http.Client
|
*http.Client
|
||||||
config *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,33 +143,12 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
req.Header.Set("Content-Type", ct)
|
req.Header.Set("Content-Type", ct)
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp *http.Response
|
resp, err := c.Do(req)
|
||||||
backoff := 1000 * time.Millisecond
|
|
||||||
for {
|
|
||||||
resp, err = c.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// handle status code 429, which indicates the server is throttling
|
|
||||||
// our requests. Do an exponential backoff and retry the request.
|
|
||||||
if resp.StatusCode == 429 {
|
|
||||||
if backoff > time.Hour {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
backoff *= 2
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-time.After(backoff):
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return parseAPIError("bad request", resp)
|
return parseAPIError("bad request", resp)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
@ -190,7 +168,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
// NewClient return new mastodon API client.
|
// NewClient return new mastodon API client.
|
||||||
func NewClient(config *Config) *Client {
|
func NewClient(config *Config) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
Client: *http.DefaultClient,
|
Client: http.DefaultClient,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue