mirror of
https://github.com/bestnite/bilinovel-downloader.git
synced 2025-10-26 09:11:01 +00:00
refactor
This commit is contained in:
@@ -9,11 +9,19 @@ import (
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
var client *resty.Client
|
||||
type RestyClient struct {
|
||||
client *resty.Client
|
||||
concurrency int
|
||||
sem chan struct{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
client = resty.New()
|
||||
client.SetTransport(&http.Transport{
|
||||
func NewRestyClient(concurrency int) *RestyClient {
|
||||
client := &RestyClient{
|
||||
client: resty.New(),
|
||||
concurrency: concurrency,
|
||||
sem: make(chan struct{}, concurrency),
|
||||
}
|
||||
client.client.SetTransport(&http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
if addr == "www.bilinovel.com:443" {
|
||||
addr = "64.140.161.52:443"
|
||||
@@ -24,7 +32,16 @@ func init() {
|
||||
},
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
})
|
||||
client.SetRetryCount(10).
|
||||
client.client.
|
||||
OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
|
||||
client.sem <- struct{}{}
|
||||
return nil
|
||||
}).
|
||||
OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
|
||||
<-client.sem
|
||||
return nil
|
||||
})
|
||||
client.client.SetRetryCount(10).
|
||||
SetRetryWaitTime(3 * time.Second).
|
||||
SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
||||
if resp.StatusCode() == http.StatusTooManyRequests {
|
||||
@@ -43,10 +60,13 @@ func init() {
|
||||
AddRetryCondition(func(r *resty.Response, err error) bool {
|
||||
return err != nil || r.StatusCode() == http.StatusTooManyRequests
|
||||
})
|
||||
|
||||
client.client.SetLogger(disableLogger{}).SetHeader("Accept-Charset", "utf-8").SetHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0")
|
||||
return client
|
||||
}
|
||||
|
||||
func Request() *resty.Request {
|
||||
return client.R().SetLogger(disableLogger{}).SetHeader("Accept-Charset", "utf-8").SetHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0")
|
||||
func (c *RestyClient) R() *resty.Request {
|
||||
return c.client.R()
|
||||
}
|
||||
|
||||
type disableLogger struct{}
|
||||
|
||||
Reference in New Issue
Block a user