- Refractor code

- Add a better logging for 429 when streaming
- Fix minor issues
This commit is contained in:
Mukhtar Akere
2025-04-01 06:37:10 +01:00
parent 8bf164451c
commit 7d954052ae
28 changed files with 214 additions and 179 deletions
+1 -1
View File
@@ -217,7 +217,7 @@ func SetConfigPath(path string) error {
return nil
}
func GetConfig() *Config {
func Get() *Config {
once.Do(func() {
instance = &Config{} // Initialize instance first
if err := instance.loadConfig(); err != nil {
+4 -4
View File
@@ -17,7 +17,7 @@ var (
)
func GetLogPath() string {
cfg := config.GetConfig()
cfg := config.Get()
logsDir := filepath.Join(cfg.Path, "logs")
if _, err := os.Stat(logsDir); os.IsNotExist(err) {
@@ -29,9 +29,9 @@ func GetLogPath() string {
return filepath.Join(logsDir, "decypharr.log")
}
func NewLogger(prefix string) zerolog.Logger {
func New(prefix string) zerolog.Logger {
level := config.GetConfig().LogLevel
level := config.Get().LogLevel
rotatingLogFile := &lumberjack.Logger{
Filename: GetLogPath(),
@@ -91,7 +91,7 @@ func NewLogger(prefix string) zerolog.Logger {
func GetDefaultLogger() zerolog.Logger {
once.Do(func() {
logger = NewLogger("decypharr")
logger = New("decypharr")
})
return logger
}
+1 -1
View File
@@ -56,7 +56,7 @@ func getDiscordHeader(event string) string {
}
func SendDiscordMessage(event string, status string, message string) error {
cfg := config.GetConfig()
cfg := config.Get()
webhookURL := cfg.DiscordWebhook
if webhookURL == "" {
return nil
+22 -17
View File
@@ -60,9 +60,10 @@ type Client struct {
}
// WithMaxRetries sets the maximum number of retry attempts
func (c *Client) WithMaxRetries(retries int) *Client {
c.maxRetries = retries
return c
func WithMaxRetries(maxRetries int) ClientOption {
return func(c *Client) {
c.maxRetries = maxRetries
}
}
// WithTimeout sets the request timeout
@@ -79,24 +80,27 @@ func WithRedirectPolicy(policy func(req *http.Request, via []*http.Request) erro
}
// WithRateLimiter sets a rate limiter
func (c *Client) WithRateLimiter(rl *rate.Limiter) *Client {
c.rateLimiter = rl
return c
func WithRateLimiter(rl *rate.Limiter) ClientOption {
return func(c *Client) {
c.rateLimiter = rl
}
}
// WithHeaders sets default headers
func (c *Client) WithHeaders(headers map[string]string) *Client {
c.headers = headers
return c
func WithHeaders(headers map[string]string) ClientOption {
return func(c *Client) {
c.headers = headers
}
}
func (c *Client) SetHeader(key, value string) {
c.headers[key] = value
}
func (c *Client) WithLogger(logger zerolog.Logger) *Client {
c.logger = logger
return c
func WithLogger(logger zerolog.Logger) ClientOption {
return func(c *Client) {
c.logger = logger
}
}
func WithTransport(transport *http.Transport) ClientOption {
@@ -106,11 +110,12 @@ func WithTransport(transport *http.Transport) ClientOption {
}
// WithRetryableStatus adds status codes that should trigger a retry
func (c *Client) WithRetryableStatus(statusCodes ...int) *Client {
for _, code := range statusCodes {
c.retryableStatus[code] = true
func WithRetryableStatus(statusCodes ...int) ClientOption {
return func(c *Client) {
for _, code := range statusCodes {
c.retryableStatus[code] = true
}
}
return c
}
// doRequest performs a single HTTP request with rate limiting
@@ -249,7 +254,7 @@ func New(options ...ClientOption) *Client {
http.StatusServiceUnavailable: true,
http.StatusGatewayTimeout: true,
},
logger: logger.NewLogger("request"),
logger: logger.New("request"),
timeout: 60 * time.Second,
}