nquic_session_cache (nquic v1.0.0)
View SourcePer-instance session ticket cache for QUIC 0-RTT resumption.
Each cache is a gen_server that owns one named ETS table and runs a
periodic eviction sweep. Multiple caches can coexist on the same node
(one per logical client of the library), identified by the Name atom
passed to start_link/1,2.
Usage
Start the cache under your own supervisor before using nquic:
-spec children() -> [supervisor:child_spec()].
children() ->
[nquic_session_cache:child_spec(my_quic_tickets, #{sweep_ms => 60_000})].Then reference it in connect options:
{ok, Conn} = nquic:connect(Host, Port, #{session_cache => my_quic_tickets}).All cache functions fail loudly (badarg from the underlying ETS call)
if Name does not refer to a live cache. There is no auto-start: the
caller is responsible for initialising the cache synchronously before
any nquic connect is issued.
Custom backends
The session_cache connect option also accepts {module, Mod} where
Mod exports the triple:
store(Host, Port, Ticket) -> ok.
lookup(Host, Port) -> {ok, Ticket} | {error, not_found}.
delete(Host, Port) -> ok.Use this to plug in an application-specific storage (Redis, Mnesia, etc).
Summary
Functions
Standard child spec with the default sweep interval.
Standard child spec with custom options.
Drop every entry in cache Name. Raises badarg if not started.
Delete a ticket from cache Name. Raises badarg if not started.
Look up a session ticket for {Host, Port} in cache Name.
Returns {ok, Ticket} for a live ticket, {error, not_found} for a
miss or an expired entry. Raises badarg if Name is not a live
cache.
If Opts does not already carry a session_ticket, resolve the
configured session_cache selector and inject any cached ticket
into Opts. Returns Opts unchanged on miss or when no cache is
configured.
Accepts the same selector shape as the connect/3,4 option:
false (no cache), an atom naming a started cache, or
{module, Mod} for a custom backend exporting lookup/2.
Return the number of entries in cache Name.
Start a cache registered under Name with default options.
Start a cache registered under Name.
Accepted options
Stop the cache and tear down its ETS table.
Store a session ticket under {Host, Port} for the cache Name.
Raises badarg if Name is not a live cache; start it via
start_link/1,2 or child_spec/1,2 first.
Types
-type cache_name() :: atom().
-type host() :: inet:hostname() | inet:ip_address().
Functions
-spec child_spec(cache_name()) -> supervisor:child_spec().
Standard child spec with the default sweep interval.
-spec child_spec(cache_name(), map()) -> supervisor:child_spec().
Standard child spec with custom options.
-spec clear(cache_name()) -> ok.
Drop every entry in cache Name. Raises badarg if not started.
-spec delete(cache_name(), host(), inet:port_number()) -> ok.
Delete a ticket from cache Name. Raises badarg if not started.
-spec handle_call(term(), gen_server:from(), #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}) -> {reply, {error, unknown_request}, #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}}.
-spec handle_cast(term(), #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}) -> {noreply, #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}}.
-spec handle_info(sweep | term(), #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}) -> {noreply, #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}}.
-spec init({cache_name(), map()}) -> {ok, #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}}.
-spec lookup(cache_name(), host(), inet:port_number()) -> {ok, map()} | {error, not_found}.
Look up a session ticket for {Host, Port} in cache Name.
Returns {ok, Ticket} for a live ticket, {error, not_found} for a
miss or an expired entry. Raises badarg if Name is not a live
cache.
-spec maybe_load_ticket(host(), inet:port_number(), map()) -> map().
If Opts does not already carry a session_ticket, resolve the
configured session_cache selector and inject any cached ticket
into Opts. Returns Opts unchanged on miss or when no cache is
configured.
Accepts the same selector shape as the connect/3,4 option:
false (no cache), an atom naming a started cache, or
{module, Mod} for a custom backend exporting lookup/2.
-spec size(cache_name()) -> non_neg_integer().
Return the number of entries in cache Name.
-spec start_link(cache_name()) -> {ok, pid()} | ignore | {error, term()}.
Start a cache registered under Name with default options.
-spec start_link(cache_name(), map()) -> {ok, pid()} | ignore | {error, term()}.
Start a cache registered under Name.
Accepted options:
sweep_ms- eviction interval in milliseconds (default 60000). Returns{error, {already_started, Pid}}if a cache with this name is already registered. This call is synchronous: when it returns `{ok, }the ETS table is live and ready forstore/4/lookup/3`.
-spec stop(cache_name()) -> ok.
Stop the cache and tear down its ETS table.
-spec store(cache_name(), host(), inet:port_number(), map()) -> ok.
Store a session ticket under {Host, Port} for the cache Name.
Raises badarg if Name is not a live cache; start it via
start_link/1,2 or child_spec/1,2 first.
-spec terminate(term(), #state{name :: cache_name(), sweep_ms :: pos_integer(), sweep_ref :: reference() | undefined}) -> ok.