Files
flatrender/services/render/internal/identityclient/client.go
T
soroush.asadi 1f52f53cf7
Build backend images / build content-svc (push) Failing after 51s
Build backend images / build file-svc (push) Failing after 53s
Build backend images / build gateway (push) Failing after 1m1s
Build backend images / build identity-svc (push) Failing after 48s
Build backend images / build notification-svc (push) Failing after 42s
Build backend images / build render-svc (push) Failing after 47s
Build backend images / build studio-svc (push) Failing after 1m13s
feat(render+identity): daily render-limit — consume on submit, refund on admin-stop
Business rule: each user has a daily render limit. Admin-stop refunds the used
charge (not the user's fault); a user's own cancel does not.

- identity: ConsumeRenderChargeAsync / RefundRenderChargeAsync on DailyRemainRenderCount
  with lazy daily reset (mig 24: daily_renders_reset_at). Convention: max=0 ⇒ UNLIMITED,
  so existing 0/0 users keep rendering until an admin sets a real limit.
- identity InternalController (service-token): POST /v1/internal/render-charge/{consume,refund}
- render-svc: identityclient + on Create consume (block 429 when limit reached, fail-open
  on identity outage); on admin Stop refund the job owner; user /cancel unchanged
- compose: IDENTITY_URL for render-svc, ServiceToken for identity-svc

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 02:18:00 +03:30

78 lines
2.1 KiB
Go

// Package identityclient calls identity-svc's internal endpoints for render-charge
// consume/refund (service-to-service, shared service token).
package identityclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
)
type Client struct {
baseURL string
token string
http *http.Client
}
func New(baseURL, token string) *Client {
return &Client{baseURL: baseURL, token: token, http: &http.Client{Timeout: 8 * time.Second}}
}
func (c *Client) configured() bool { return c.baseURL != "" }
type consumeResp struct {
Allowed bool `json:"allowed"`
Remaining int `json:"remaining"`
}
func (c *Client) post(ctx context.Context, path string, userID uuid.UUID) (*http.Response, error) {
body, _ := json.Marshal(map[string]string{"user_id": userID.String()})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Service-Token", c.token)
return c.http.Do(req)
}
// Consume decrements the user's daily render count. Returns whether the render is
// allowed. Fails OPEN (allowed=true) if identity is unreachable, to avoid blocking
// renders on a transient outage.
func (c *Client) Consume(ctx context.Context, userID uuid.UUID) (bool, error) {
if !c.configured() {
return true, nil
}
resp, err := c.post(ctx, "/v1/internal/render-charge/consume", userID)
if err != nil {
return true, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return true, fmt.Errorf("consume status %d", resp.StatusCode)
}
var r consumeResp
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return true, err
}
return r.Allowed, nil
}
// Refund returns one render to the user's daily count (admin-stop only).
func (c *Client) Refund(ctx context.Context, userID uuid.UUID) error {
if !c.configured() {
return nil
}
resp, err := c.post(ctx, "/v1/internal/render-charge/refund", userID)
if err != nil {
return err
}
resp.Body.Close()
return nil
}