Documentation
¶
Overview ¶
Package idempotency provides middleware for idempotent POST request handling. Clients can send an Idempotency-Key header to ensure requests are processed exactly once, with subsequent requests returning the cached response.
Index ¶
Constants ¶
const ( // HeaderIdempotencyKey is the header name for idempotency keys HeaderIdempotencyKey = "Idempotency-Key" // HeaderIdempotencyReplayed indicates the response was replayed from cache HeaderIdempotencyReplayed = "Idempotency-Replayed" // DefaultTTL is the default time-to-live for cached responses DefaultTTL = 24 * time.Hour // MaxKeyLength is the maximum allowed length for an idempotency key MaxKeyLength = 256 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedResponse ¶
type CachedResponse struct {
StatusCode int `json:"status_code"`
Headers map[string]string `json:"headers"`
Body []byte `json:"body"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
CachedResponse stores a cached API response
type Info ¶
type Info struct {
Key string `json:"key"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
Replayed bool `json:"replayed"`
}
Info represents information about an idempotency key
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements Store using in-memory storage
func GetDefaultStore ¶
func GetDefaultStore() *MemoryStore
GetDefaultStore returns the default idempotency store
func NewMemoryStore ¶
func NewMemoryStore(ttl time.Duration) *MemoryStore
NewMemoryStore creates a new in-memory idempotency store
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(key string)
Delete removes a cached response
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(key string) (*CachedResponse, bool)
Get retrieves a cached response
func (*MemoryStore) Lock ¶
func (s *MemoryStore) Lock(key string) bool
Lock acquires a processing lock
func (*MemoryStore) Set ¶
func (s *MemoryStore) Set(key string, response *CachedResponse)
Set stores a response
func (*MemoryStore) Unlock ¶
func (s *MemoryStore) Unlock(key string)
Unlock releases a processing lock
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides idempotency handling
func NewMiddleware ¶
func NewMiddleware(store Store) *Middleware
NewMiddleware creates a new idempotency middleware
type ResponseRecorder ¶
type ResponseRecorder struct {
http.ResponseWriter
// contains filtered or unexported fields
}
ResponseRecorder captures the response for caching
func NewResponseRecorder ¶
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder
NewResponseRecorder creates a new response recorder
func (*ResponseRecorder) Header ¶
func (r *ResponseRecorder) Header() http.Header
Header returns the header map
func (*ResponseRecorder) ToCachedResponse ¶
func (r *ResponseRecorder) ToCachedResponse() *CachedResponse
ToCachedResponse converts the recorded response to a cached response
func (*ResponseRecorder) Write ¶
func (r *ResponseRecorder) Write(b []byte) (int, error)
Write captures the response body
func (*ResponseRecorder) WriteHeader ¶
func (r *ResponseRecorder) WriteHeader(code int)
WriteHeader captures the status code
type Store ¶
type Store interface {
// Get retrieves a cached response by key
Get(key string) (*CachedResponse, bool)
// Set stores a response with the given key
Set(key string, response *CachedResponse)
// Delete removes a cached response
Delete(key string)
// Lock acquires a lock for processing a key (returns true if lock acquired)
Lock(key string) bool
// Unlock releases the lock for a key
Unlock(key string)
}
Store defines the interface for idempotency key storage
Source Files
¶
- idempotency.go