refactor: rename CaptchaTimeoutError to CaptchaPollTimeoutError and separate HTTP client timeout from poll timeout

This commit is contained in:
2026-04-12 21:09:24 +04:00
parent cea692cf1b
commit c839b71c9e
2 changed files with 21 additions and 21 deletions

View File

@@ -14,10 +14,10 @@ import (
const ( const (
captchaPollInterval = 1 * time.Second captchaPollInterval = 1 * time.Second
captchaTimeout = 120 * time.Second captchaPollTimeout = 120 * time.Second
) )
var captchaHTTPClient = &http.Client{Timeout: captchaTimeout} var captchaHTTPClient = &http.Client{Timeout: 5 * time.Second}
// solveCaptcha creates a task on the given captcha service and polls until it is solved, // solveCaptcha creates a task on the given captcha service and polls until it is solved,
// returning the extracted token from the solution object. // returning the extracted token from the solution object.
@@ -32,7 +32,7 @@ var captchaHTTPClient = &http.Client{Timeout: captchaTimeout}
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func solveCaptcha(baseURL, apiKey string, task map[string]any, solutionKey string, taskIDIsString bool) (string, error) { func solveCaptcha(baseURL, apiKey string, task map[string]any, solutionKey string, taskIDIsString bool) (string, error) {
if apiKey == "" { if apiKey == "" {
@@ -98,15 +98,15 @@ func captchaCreateTask(baseURL, apiKey string, task map[string]any) (string, err
} }
// captchaPollResult polls the getTaskResult endpoint at captchaPollInterval until the task // captchaPollResult polls the getTaskResult endpoint at captchaPollInterval until the task
// is solved, an error is returned by the service, or the overall captchaTimeout is hit. // is solved, an error is returned by the service, or the overall captchaPollTimeout is hit.
// //
// It can return the following errors: // It can return the following errors:
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func captchaPollResult(baseURL, apiKey, taskID, solutionKey string, taskIDIsString bool) (string, error) { func captchaPollResult(baseURL, apiKey, taskID, solutionKey string, taskIDIsString bool) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), captchaTimeout) ctx, cancel := context.WithTimeout(context.Background(), captchaPollTimeout)
defer cancel() defer cancel()
ticker := time.NewTicker(captchaPollInterval) ticker := time.NewTicker(captchaPollInterval)
@@ -115,7 +115,7 @@ func captchaPollResult(baseURL, apiKey, taskID, solutionKey string, taskIDIsStri
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return "", types.NewCaptchaTimeoutError(taskID) return "", types.NewCaptchaPollTimeoutError(taskID)
case <-ticker.C: case <-ticker.C:
token, err := captchaGetTaskResult(baseURL, apiKey, taskID, solutionKey, taskIDIsString) token, err := captchaGetTaskResult(baseURL, apiKey, taskID, solutionKey, taskIDIsString)
if errors.Is(err, types.ErrCaptchaProcessing) { if errors.Is(err, types.ErrCaptchaProcessing) {
@@ -200,7 +200,7 @@ const twoCaptchaBaseURL = "https://api.2captcha.com"
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func twoCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) { func twoCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) {
return solveCaptcha(twoCaptchaBaseURL, apiKey, map[string]any{ return solveCaptcha(twoCaptchaBaseURL, apiKey, map[string]any{
@@ -217,7 +217,7 @@ func twoCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string,
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func twoCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) { func twoCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) {
task := map[string]any{ task := map[string]any{
@@ -238,7 +238,7 @@ func twoCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction strin
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func twoCaptchaSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) { func twoCaptchaSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) {
task := map[string]any{ task := map[string]any{
@@ -262,7 +262,7 @@ const antiCaptchaBaseURL = "https://api.anti-captcha.com"
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func antiCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) { func antiCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) {
return solveCaptcha(antiCaptchaBaseURL, apiKey, map[string]any{ return solveCaptcha(antiCaptchaBaseURL, apiKey, map[string]any{
@@ -280,7 +280,7 @@ func antiCaptchaSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string,
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func antiCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) { func antiCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) {
task := map[string]any{ task := map[string]any{
@@ -302,7 +302,7 @@ func antiCaptchaSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction stri
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func antiCaptchaSolveHCaptcha(apiKey, websiteURL, websiteKey string) (string, error) { func antiCaptchaSolveHCaptcha(apiKey, websiteURL, websiteKey string) (string, error) {
return solveCaptcha(antiCaptchaBaseURL, apiKey, map[string]any{ return solveCaptcha(antiCaptchaBaseURL, apiKey, map[string]any{
@@ -319,7 +319,7 @@ func antiCaptchaSolveHCaptcha(apiKey, websiteURL, websiteKey string) (string, er
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func antiCaptchaSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) { func antiCaptchaSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) {
task := map[string]any{ task := map[string]any{
@@ -343,7 +343,7 @@ const capSolverBaseURL = "https://api.capsolver.com"
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func capSolverSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) { func capSolverSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, error) {
return solveCaptcha(capSolverBaseURL, apiKey, map[string]any{ return solveCaptcha(capSolverBaseURL, apiKey, map[string]any{
@@ -360,7 +360,7 @@ func capSolverSolveRecaptchaV2(apiKey, websiteURL, websiteKey string) (string, e
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func capSolverSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) { func capSolverSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string) (string, error) {
task := map[string]any{ task := map[string]any{
@@ -381,7 +381,7 @@ func capSolverSolveRecaptchaV3(apiKey, websiteURL, websiteKey, pageAction string
// - types.ErrCaptchaKeyEmpty // - types.ErrCaptchaKeyEmpty
// - types.CaptchaRequestError // - types.CaptchaRequestError
// - types.CaptchaAPIError // - types.CaptchaAPIError
// - types.CaptchaTimeoutError // - types.CaptchaPollTimeoutError
// - types.CaptchaSolutionKeyError // - types.CaptchaSolutionKeyError
func capSolverSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) { func capSolverSolveTurnstile(apiKey, websiteURL, websiteKey, cData string) (string, error) {
task := map[string]any{ task := map[string]any{

View File

@@ -520,15 +520,15 @@ func (e CaptchaRequestError) Unwrap() error {
return e.Err return e.Err
} }
type CaptchaTimeoutError struct { type CaptchaPollTimeoutError struct {
TaskID string TaskID string
} }
func NewCaptchaTimeoutError(taskID string) CaptchaTimeoutError { func NewCaptchaPollTimeoutError(taskID string) CaptchaPollTimeoutError {
return CaptchaTimeoutError{TaskID: taskID} return CaptchaPollTimeoutError{TaskID: taskID}
} }
func (e CaptchaTimeoutError) Error() string { func (e CaptchaPollTimeoutError) Error() string {
return fmt.Sprintf("captcha solving timed out (taskId: %s)", e.TaskID) return fmt.Sprintf("captcha solving timed out (taskId: %s)", e.TaskID)
} }