StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
stripekit::WebhookDedupStore Class Referenceabstract

Remembers which webhook events have been handled. More...

#include <WebhookDedupStore.h>

Inheritance diagram for stripekit::WebhookDedupStore:
[legend]

Public Member Functions

virtual ~WebhookDedupStore ()=default
 Destroys the store instance.
virtual ClaimResult claim (const ClaimRequest &request)=0
 Tries to reserve an event id for processing.
virtual bool mark_processed (std::string_view event_id, std::chrono::system_clock::time_point processed_at)=0
 Records that an event has been fully handled.
virtual bool release_claim (std::string_view event_id, std::chrono::system_clock::time_point claimed_at)=0
 Releases an unfinished claim after its handler has stopped.
virtual void erase_expired (std::chrono::system_clock::time_point now, std::chrono::hours retention)=0
 Deletes records older than the retention period.

Detailed Description

Remembers which webhook events have been handled.

Stripe delivers the same event more than once whenever it cannot confirm receipt. This interface skips completed events and reserves unfinished events for one worker at a time. Handlers must still be safe to retry after failure or a crash.

Which implementation to use depends on the deployment. Use InMemoryWebhookDedupStore for tests, SqliteWebhookDedupStore for a single host, and an implementation backed by the application's shared database when several instances handle the same endpoint.

Implementations must be thread-safe, and claim() must be atomic: two instances racing on the same event id must produce exactly one ClaimOutcome::Claimed. A read followed by a write is not sufficient; use the database's conditional insert.

Definition at line 102 of file WebhookDedupStore.h.

Constructor & Destructor Documentation

◆ ~WebhookDedupStore()

virtual stripekit::WebhookDedupStore::~WebhookDedupStore ( )
virtualdefault

Destroys the store instance.

Member Function Documentation

◆ claim()

virtual ClaimResult stripekit::WebhookDedupStore::claim ( const ClaimRequest & request)
pure virtual

Tries to reserve an event id for processing.

Parameters
requestThe event id to reserve, and the timing rules to apply.
Returns
Whether this caller won the reservation, and why not if it did not. Store failures are reported here rather than thrown.

Implemented in stripekit::InMemoryWebhookDedupStore, and stripekit::SqliteWebhookDedupStore.

◆ mark_processed()

virtual bool stripekit::WebhookDedupStore::mark_processed ( std::string_view event_id,
std::chrono::system_clock::time_point processed_at )
pure virtual

Records that an event has been fully handled.

Later deliveries of the same event then report ClaimOutcome::DuplicateProcessed.

Parameters
event_idThe event id that was handled.
processed_atWhen it finished.
Returns
True if a claim record was updated; false if there was nothing to update.

Implemented in stripekit::InMemoryWebhookDedupStore, and stripekit::SqliteWebhookDedupStore.

◆ release_claim()

virtual bool stripekit::WebhookDedupStore::release_claim ( std::string_view event_id,
std::chrono::system_clock::time_point claimed_at )
pure virtual

Releases an unfinished claim after its handler has stopped.

Delete only the unprocessed record whose claim time matches claimed_at, so an older attempt cannot release a newer worker's reservation.

Parameters
event_idThe event to allow another delivery to retry.
claimed_atThe successful ClaimRequest::now for this attempt.
Returns
True if the matching claim was removed; false if it no longer exists.
Exceptions
StripeKitExceptionThe store could not release the claim.

Implemented in stripekit::InMemoryWebhookDedupStore, and stripekit::SqliteWebhookDedupStore.

◆ erase_expired()

virtual void stripekit::WebhookDedupStore::erase_expired ( std::chrono::system_clock::time_point now,
std::chrono::hours retention )
pure virtual

Deletes records older than the retention period.

Call this periodically to stop the store growing without limit. Keep the retention comfortably longer than Stripe's retry window, which runs for several days, or an old event could be processed a second time.

Parameters
nowCurrent time.
retentionHow long a record must be kept.

Implemented in stripekit::InMemoryWebhookDedupStore, and stripekit::SqliteWebhookDedupStore.


The documentation for this class was generated from the following file: