StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
SqliteWebhookDedupStore.h
1/** SqliteWebhookDedupStore.h
2 *
3 * Optional SQLite-backed implementation of webhook event-id tracking.
4 * It stores claim and processed state in a SQLite database.
5 *
6 * @author Hans de Ruiter
7 *
8 * @license See LICENSE.md for details.
9 */
10
11#pragma once
12
13#include "StripeKit/WebhookDedupStore.h"
14
15#include <mutex>
16#include <string>
17
18struct sqlite3;
19
20namespace stripekit {
21
22/**
23 * @ingroup webhooks
24 *
25 * A de-duplication store that uses a SQLite database.
26 *
27 * The sensible default for an application running on a single host: unlike
28 * InMemoryWebhookDedupStore it survives restarts, and it needs no database
29 * server. Applications running several instances should implement
30 * WebhookDedupStore against their shared database instead, since separate
31 * SQLite files cannot agree on which events are done.
32 *
33 * Only available when StripeKit was built with SQLite support; check
34 * sqlite_dedup_store_is_available() if that is in doubt.
35 *
36 * @code
37 * static stripekit::SqliteWebhookDedupStore dedup_store("stripe-webhooks.sqlite3");
38 * @endcode
39 *
40 * The store must outlive every handler that uses it. Safe to use from several
41 * threads.
42 */
44public:
45 /**
46 * Opens or creates the database.
47 *
48 * Missing tables are created, so a fresh path just works.
49 *
50 * @param database_path Path to the SQLite file. Its directory must exist
51 * and be writable, and the file must be on local storage; network
52 * filesystems do not give SQLite the locking it needs.
53 * @throws ConfigurationException The database could not be opened or
54 * prepared, or this build has no SQLite support.
55 */
56 explicit SqliteWebhookDedupStore(std::string database_path);
57
58 /**
59 * Closes the database and destroys the store.
60 */
62
63 /**
64 * Tries to reserve an event id for processing.
65 *
66 * @param request The event id to reserve, and the timing rules to apply.
67 * @return Whether this caller won the reservation.
68 */
69 ClaimResult claim(const ClaimRequest& request) override;
70
71 /**
72 * Records that an event has been fully handled.
73 *
74 * @param event_id The event id that was handled.
75 * @param processed_at When it finished.
76 * @return True if a claim record was updated.
77 */
78 bool mark_processed(std::string_view event_id,
79 std::chrono::system_clock::time_point processed_at) override;
80
81 /** @copydoc WebhookDedupStore::release_claim */
82 bool release_claim(std::string_view event_id,
83 std::chrono::system_clock::time_point claimed_at) override;
84
85 /**
86 * Deletes records older than the retention period.
87 *
88 * @param now Current time.
89 * @param retention How long a record must be kept. Keep this longer than
90 * Stripe's retry window, which runs for several days.
91 */
92 void erase_expired(std::chrono::system_clock::time_point now,
93 std::chrono::hours retention) override;
94
95private:
96 sqlite3* db = nullptr;
97 std::mutex mutex;
98};
99
100/**
101 * @ingroup webhooks
102 *
103 * Reports whether this build can use SqliteWebhookDedupStore.
104 *
105 * SQLite support is a build option, so a StripeKit built without it will fail
106 * to construct the store.
107 *
108 * @return True when SQLite support was compiled in.
109 */
111
112} // namespace stripekit
void erase_expired(std::chrono::system_clock::time_point now, std::chrono::hours retention) override
Deletes records older than the retention period.
~SqliteWebhookDedupStore() override
Closes the database and destroys the store.
SqliteWebhookDedupStore(std::string database_path)
Opens or creates the database.
bool release_claim(std::string_view event_id, std::chrono::system_clock::time_point claimed_at) override
Releases an unfinished claim after its handler has stopped.
ClaimResult claim(const ClaimRequest &request) override
Tries to reserve an event id for processing.
bool mark_processed(std::string_view event_id, std::chrono::system_clock::time_point processed_at) override
Records that an event has been fully handled.
Remembers which webhook events have been handled.
bool sqlite_dedup_store_is_available()
Reports whether this build can use SqliteWebhookDedupStore.
Async.h.
Definition Async.h:20
Asks the store to reserve an event id.
The store's answer to a ClaimRequest.