-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve resiliency for qBittorrent disconnects
- Loading branch information
Showing
7 changed files
with
47 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,64 @@ | ||
package qbittorrent | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type ( | ||
API struct { | ||
host string | ||
client *http.Client | ||
host string | ||
username, password string | ||
client *http.Client | ||
} | ||
) | ||
|
||
func New(host, username, password string) *API { | ||
func New(ctx context.Context, host, username, password string) *API { | ||
jar, err := cookiejar.New(nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
api := &API{ | ||
host: fmt.Sprintf("%s/api/v2", host), | ||
host: fmt.Sprintf("%s/api/v2", host), | ||
username: username, | ||
password: password, | ||
client: &http.Client{ | ||
Timeout: 3 * time.Second, | ||
Jar: jar, | ||
}, | ||
} | ||
var version string | ||
api.Wait() | ||
api.Wait(ctx) | ||
if version, err = api.Version(); err != nil { | ||
if !errors.Is(err, ErrUnauthorized) { | ||
log.Fatal().Msgf("failed to initialize: %s", err) | ||
} | ||
if err := api.Login(username, password); err != nil { | ||
log.Fatal().Msgf("could not initialize qBittorrent: %s", err) | ||
} | ||
if version, err = api.Version(); err == ErrUnauthorized { | ||
log.Fatal().Msgf("could not check version: %s", err) | ||
} | ||
log.Fatal().Msgf("failed to connect to qBittorrent: %s", err) | ||
} | ||
log.Info().Msgf("connected to qBitTorrent:%s", version) | ||
log.Info().Msgf("connected to qBittorrent:%s", version) | ||
return api | ||
} | ||
|
||
func (api *API) Do(req *http.Request) (*http.Response, error) { | ||
resp, err := api.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode >= 400 { | ||
return nil, ErrUnauthorized | ||
ctx := context.WithoutCancel(req.Context()) | ||
localReq := req.Clone(ctx) | ||
resp, err := api.client.Do(localReq) | ||
switch { | ||
case errors.Is(err, syscall.ECONNREFUSED) || | ||
errors.Is(err, syscall.ECONNABORTED) || | ||
errors.Is(err, syscall.ECONNRESET): | ||
log.Warn().Msgf("qBittorrent disconnected") | ||
api.Wait(ctx) | ||
return api.Do(req) | ||
case err == nil && resp.StatusCode >= 400: | ||
if loginErr := api.Login(api.username, api.password); loginErr != nil { | ||
return resp, loginErr | ||
} | ||
return api.Do(req) | ||
} | ||
return resp, nil | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
package qbittorrent | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func (api *API) Wait() { | ||
for retries := 5; retries >= 0; retries-- { | ||
_, err := api.client.Get(api.host + "/app/version") | ||
if err == nil { | ||
func (api *API) Wait(ctx context.Context) { | ||
log.Info().Msgf("probing for qBittorrent") | ||
for { | ||
if ctx.Err() != nil { | ||
return | ||
} | ||
log.Info().Msgf("waiting for qBitTorrent") | ||
time.Sleep(time.Duration(6-retries) * 3 * time.Second) | ||
if _, err := api.client.Get(api.host + "/app/version"); err == nil { | ||
log.Info().Msgf("qBittorrent is ready") | ||
return | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters