Auto-indexing service and GraphQL API for AT Protocol Records
quickslice.slices.network/
atproto
gleam
graphql
1-module(subscription_counter_ffi).
2-export([get_global_counter/0, increment_global/0, decrement_global/0, get_global_count/0]).
3
4-define(COUNTER_KEY, graphql_subscription_counter).
5
6%% Get or create the global subscription counter
7%% Uses persistent_term for efficient, shared access across all processes
8get_global_counter() ->
9 case persistent_term:get(?COUNTER_KEY, undefined) of
10 undefined ->
11 Counter = counters:new(1, [atomics]),
12 persistent_term:put(?COUNTER_KEY, Counter),
13 Counter;
14 Counter ->
15 Counter
16 end.
17
18%% Atomically increment the global counter and return the new value
19increment_global() ->
20 Counter = get_global_counter(),
21 counters:add(Counter, 1, 1),
22 counters:get(Counter, 1).
23
24%% Atomically decrement the global counter and return the new value
25decrement_global() ->
26 Counter = get_global_counter(),
27 counters:sub(Counter, 1, 1),
28 counters:get(Counter, 1).
29
30%% Get the current global counter value
31get_global_count() ->
32 Counter = get_global_counter(),
33 counters:get(Counter, 1).