Fix unbounded memory growth in notification storage issue

This commit is contained in:
2025-10-26 00:17:13 -07:00
parent a3365c303a
commit 6291cfe218
21 changed files with 4686 additions and 53 deletions
+304
View File
@@ -0,0 +1,304 @@
package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// RESTClient is a client for the Notifier REST API
type RESTClient struct {
baseURL string
apiKey string
timeout time.Duration
maxRetries int
retryBackoff time.Duration
client *http.Client
}
// NewRESTClient creates a new REST client with the given config
func NewRESTClient(cfg ClientConfig) *RESTClient {
if cfg.Timeout == 0 {
cfg.Timeout = 30 * time.Second
}
if cfg.MaxRetries == 0 {
cfg.MaxRetries = 3
}
if cfg.RetryBackoff == 0 {
cfg.RetryBackoff = 100 * time.Millisecond
}
tlsConfig := &tls.Config{
InsecureSkipVerify: cfg.TLSInsecure,
}
httpClient := &http.Client{
Timeout: cfg.Timeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
return &RESTClient{
baseURL: cfg.BaseURL,
apiKey: cfg.APIKey,
timeout: cfg.Timeout,
maxRetries: cfg.MaxRetries,
retryBackoff: cfg.RetryBackoff,
client: httpClient,
}
}
// Send sends a single notification
func (c *RESTClient) Send(ctx context.Context, req NotificationRequest) (*NotificationResponse, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
respBody, statusCode, err := c.doRequest(ctx, "POST", "/api/v1/notifications", body)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK && statusCode != http.StatusCreated && statusCode != http.StatusAccepted {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
// The API wraps the response in a "result" field
var wrapper struct {
Result NotificationResponse `json:"result"`
}
if err := json.Unmarshal(respBody, &wrapper); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &wrapper.Result, nil
}
// SendBatch sends multiple notifications
func (c *RESTClient) SendBatch(ctx context.Context, reqs []NotificationRequest) ([]*NotificationResponse, error) {
// Wrap requests in the proper format expected by the API
payload := struct {
Notifications []NotificationRequest `json:"notifications"`
}{
Notifications: reqs,
}
body, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
respBody, statusCode, err := c.doRequest(ctx, "POST", "/api/v1/notifications/batch", body)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK && statusCode != http.StatusCreated && statusCode != http.StatusAccepted {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var wrapper struct {
Results []*NotificationResponse `json:"results"`
}
if err := json.Unmarshal(respBody, &wrapper); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return wrapper.Results, nil
}
// GetNotification retrieves a notification by ID
func (c *RESTClient) GetNotification(ctx context.Context, id string) (*Notification, error) {
url := fmt.Sprintf("/api/v1/notifications/%s", id)
respBody, statusCode, err := c.doRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var notif Notification
if err := json.Unmarshal(respBody, &notif); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &notif, nil
}
// ListNotifications lists notifications with filters
func (c *RESTClient) ListNotifications(ctx context.Context, filter ListNotificationsRequest) (*ListNotificationsResponse, error) {
respBody, statusCode, err := c.doRequest(ctx, "GET", "/api/v1/notifications", nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var resp ListNotificationsResponse
if err := json.Unmarshal(respBody, &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &resp, nil
}
// CancelNotification cancels a pending notification
func (c *RESTClient) CancelNotification(ctx context.Context, id string) error {
url := fmt.Sprintf("/api/v1/notifications/%s", id)
respBody, statusCode, err := c.doRequest(ctx, "DELETE", url, nil)
if err != nil {
return err
}
if statusCode != http.StatusOK && statusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
return nil
}
// RetryNotification retries a failed notification
func (c *RESTClient) RetryNotification(ctx context.Context, id string) (*NotificationResponse, error) {
url := fmt.Sprintf("/api/v1/notifications/%s/retry", id)
respBody, statusCode, err := c.doRequest(ctx, "POST", url, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var resp NotificationResponse
if err := json.Unmarshal(respBody, &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &resp, nil
}
// GetStats retrieves notification statistics
func (c *RESTClient) GetStats(ctx context.Context) (*NotificationStats, error) {
respBody, statusCode, err := c.doRequest(ctx, "GET", "/api/v1/stats", nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var stats NotificationStats
if err := json.Unmarshal(respBody, &stats); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &stats, nil
}
// GetNotifiers retrieves available notifiers
func (c *RESTClient) GetNotifiers(ctx context.Context) (*NotifiersResponse, error) {
respBody, statusCode, err := c.doRequest(ctx, "GET", "/api/v1/notifiers", nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", statusCode, string(respBody))
}
var resp NotifiersResponse
if err := json.Unmarshal(respBody, &resp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &resp, nil
}
// HealthCheck checks service health
func (c *RESTClient) HealthCheck(ctx context.Context) (bool, error) {
url := c.baseURL + "/health"
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return false, fmt.Errorf("failed to create request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return false, fmt.Errorf("health check failed: %w", err)
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK, nil
}
// doRequest performs an HTTP request with retry logic
func (c *RESTClient) doRequest(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
var lastErr error
var lastStatusCode int
for attempt := 0; attempt <= c.maxRetries; attempt++ {
if attempt > 0 {
select {
case <-time.After(c.retryBackoff):
case <-ctx.Done():
return nil, 0, ctx.Err()
}
}
url := c.baseURL + path
var bodyReader io.Reader
if body != nil {
bodyReader = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, url, bodyReader)
if err != nil {
lastErr = fmt.Errorf("failed to create request: %w", err)
continue
}
req.Header.Set("Content-Type", "application/json")
if c.apiKey != "" {
req.Header.Set("X-API-Key", c.apiKey)
}
resp, err := c.client.Do(req)
if err != nil {
lastErr = fmt.Errorf("request failed: %w", err)
continue
}
respBody, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
lastErr = fmt.Errorf("failed to read response: %w", err)
continue
}
lastStatusCode = resp.StatusCode
// Only retry on specific status codes
if resp.StatusCode >= 500 {
lastErr = fmt.Errorf("server error: %d", resp.StatusCode)
continue
}
// Success or client error (don't retry)
return respBody, resp.StatusCode, nil
}
return nil, lastStatusCode, fmt.Errorf("request failed after %d attempts: %w", c.maxRetries+1, lastErr)
}
+100
View File
@@ -0,0 +1,100 @@
package client
import "time"
// NotificationRequest represents a notification to send
type NotificationRequest struct {
Type string `json:"type"` // stdout, email, slack, ntfy
Account string `json:"account"` // Optional: account name (uses default if empty)
Subject string `json:"subject"` // Email subject or title
Body string `json:"body"` // Notification message body
Recipients []string `json:"recipients"` // Email addresses, Slack channels, etc.
Metadata map[string]string `json:"metadata,omitempty"` // Optional metadata
}
// NotificationResponse represents the response from sending a notification
type NotificationResponse struct {
NotificationID string `json:"notification_id"`
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
SentAt time.Time `json:"sent_at"`
}
// NotificationStatus represents the status of a notification
type NotificationStatus string
const (
StatusPending NotificationStatus = "pending"
StatusQueued NotificationStatus = "queued"
StatusRetrying NotificationStatus = "retrying"
StatusSent NotificationStatus = "sent"
StatusFailed NotificationStatus = "failed"
)
// Notification represents a notification with full details
type Notification struct {
ID string `json:"id"`
Type string `json:"type"`
Account string `json:"account"`
Subject string `json:"subject"`
Body string `json:"body"`
Recipients []string `json:"recipients"`
Status NotificationStatus `json:"status"`
RetryCount int `json:"retry_count"`
MaxRetries int `json:"max_retries"`
LastError string `json:"last_error,omitempty"`
CreatedAt time.Time `json:"created_at"`
SentAt *time.Time `json:"sent_at,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
// NotificationStats represents statistics about notifications
type NotificationStats struct {
TotalSent int64 `json:"total_sent"`
TotalFailed int64 `json:"total_failed"`
TotalPending int64 `json:"total_pending"`
TotalQueued int64 `json:"total_queued"`
ByType map[string]int64 `json:"by_type"`
ByStatus map[string]int64 `json:"by_status"`
}
// ListNotificationsRequest represents filters for listing notifications
type ListNotificationsRequest struct {
IDs []string `json:"ids,omitempty"`
Types []string `json:"types,omitempty"`
Statuses []NotificationStatus `json:"statuses,omitempty"`
Recipients []string `json:"recipients,omitempty"`
CreatedAfter *time.Time `json:"created_after,omitempty"`
CreatedBefore *time.Time `json:"created_before,omitempty"`
Offset int `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
}
// ListNotificationsResponse represents the response from listing notifications
type ListNotificationsResponse struct {
Notifications []*Notification `json:"notifications"`
Total int `json:"total"`
}
// NotifierInfo represents information about an available notifier
type NotifierInfo struct {
Type string `json:"type"`
Accounts []string `json:"accounts"`
DefaultAccount string `json:"default_account"`
}
// NotifiersResponse represents available notifiers
type NotifiersResponse struct {
Notifiers []NotifierInfo `json:"notifiers"`
}
// ClientConfig contains configuration for the client
type ClientConfig struct {
BaseURL string // Base URL for REST API (e.g., "http://localhost:8080")
APIKey string // Optional API key for authentication
Timeout time.Duration // Request timeout (default: 30s)
MaxRetries int // Max retries on failure (default: 3)
RetryBackoff time.Duration // Backoff between retries (default: 100ms)
TLSInsecure bool // Disable TLS verification (for testing only)
}