-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
40 lines (35 loc) · 1.17 KB
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package captchasolve
import (
"errors"
"fmt"
"github.com/Matthew17-21/CaptchaSolve/internal/queue"
)
// TokenQueue defines the interface for token queue operations
type tokenQueue interface {
Enqueue(*CaptchaAnswer) error
Dequeue() (*CaptchaAnswer, error)
Clear()
Len() int
}
// ClearTokens removes any/all pre-harvested tokens
func (c *captchasolve) ClearTokens() { c.queue.Clear() }
// getValidTokenFromQueue attempts to get a non-expired token from the queue
func (c *captchasolve) getValidTokenFromQueue() (*CaptchaAnswer, error) {
// Check if pre-harvested tokens are already saved.
// No need to check the length since the Dequeue method does it under the hood.
c.logger.Info("Attempting to get a valid token from queue...")
tkn, err := c.queue.Dequeue()
if err != nil {
if errors.Is(err, queue.ErrQueueEmpty) {
c.logger.Info("Can't get token - queue is empty.")
return nil, queue.ErrQueueEmpty
}
c.logger.Error("Unknown error getting token from queue:", err)
return nil, fmt.Errorf("error dequeueing: %w", err)
}
if tkn.IsExpired() {
c.logger.Info("Token is expired. Getting new one from queue...")
return c.getValidTokenFromQueue()
}
return tkn, nil
}