(add-subscription! config selection & {:keys [from]})Create a new subscription for the given topic and group. Throws if a subscription already exists for this topic and group.
Returns true if the subscription was created.
selection is a topic name (string) or map with:
:topic (required) topic name:group consumer group id (default: "default")Options:
:from starting position:
- :earliest start from the beginning of the topic (default)
- :latest start from the current end of the topic (skips existing records)
- nat-int? start from a specific eidCreate a new subscription for the given topic and group. Throws if a
subscription already exists for this topic and group.
Returns true if the subscription was created.
`selection` is a topic name (string) or map with:
- `:topic` (required) topic name
- `:group` consumer group id (default: "default")
Options:
- `:from` starting position:
- `:earliest` start from the beginning of the topic (default)
- `:latest` start from the current end of the topic (skips existing records)
- nat-int? start from a specific eid(add-topic! config topic & {:as opts})Create a new topic
config a connected config maptopic the name of the topic that will be published to and subscribed toopts optional map containing any of the following keys:
:value-type column type for record values:key-type column type for record keys:index-key? when true, creates a btree index on the key column (default: false)Both key-type and value-type can be any of :bytea, :jsonb, or :text
The default is :bytea (binary data) and can handle any serializer.
:jsonb works great with the :json serializer and may also work with, e.g., a transit serializer (supplied by you)
:text works with textual serializers that return strings. The built-in serializers :edn, :json, and :string all return strings.
Note: opts apply only at creation time and cannot be used to change an existing topic.
Create a new topic
- `config` a connected config map
- `topic` the name of the topic that will be published to and subscribed to
- `opts` optional map containing any of the following keys:
- `:value-type` column type for record values
- `:key-type` column type for record keys
- `:index-key?` when true, creates a btree index on the key column (default: false)
Both key-type and value-type can be any of :bytea, :jsonb, or :text
The default is :bytea (binary data) and can handle any serializer.
:jsonb works great with the :json serializer and may also work with,
e.g., a transit serializer (supplied by you)
:text works with textual serializers that return strings. The
built-in serializers :edn, :json, and :string all return strings.
Note: opts apply only at creation time and cannot be used to change an existing topic.(append! config topic records & {:as opts})Add records to a topic stream.
config a connected config maptopic the name of the topic (must already exist), or a topic map
as returned by add-topic!, ensure-topic, or list-topics.
Passing a topic map skips looking up the topic's column
types on every call.records a seq of records to insert (see below)opts option map:
:serialize-key Key serializer. Must be a keyword or function.:serialize-value Value serializer. Must be a keyword or function.The default serializers are no-op and take the data as-is, but there
are other built-in serializers available including :json, :edn,
and :string. Alternatively, a function that takes a single arg can
be passed. It will be invoked with the key or value.
Records are maps with the following:
:key (required, even if nil):value (required):meta (optional) map of metadataReturns the largest eid inserted by the batch.
Add records to a topic stream.
- `config` a connected config map
- `topic` the name of the topic (must already exist), or a topic map
as returned by `add-topic!`, `ensure-topic`, or `list-topics`.
Passing a topic map skips looking up the topic's column
types on every call.
- `records` a seq of records to insert (see below)
- `opts` option map:
- `:serialize-key` Key serializer. Must be a keyword or function.
- `:serialize-value` Value serializer. Must be a keyword or function.
The default serializers are no-op and take the data as-is, but there
are other built-in serializers available including `:json`, `:edn`,
and `:string`. Alternatively, a function that takes a single arg can
be passed. It will be invoked with the key or value.
Records are maps with the following:
- `:key` (required, even if `nil`)
- `:value` (required)
- `:meta` (optional) map of metadata
Returns the largest eid inserted by the batch.(append-one! config topic record & {:as opts})Like append! but for a single record. topic may be a topic name or a
topic map, as for append!. Returns the inserted eid.
Like append! but for a single record. `topic` may be a topic name or a topic map, as for `append!`. Returns the inserted eid.
(commit-offset! config selection record)Only use with consumer tx-mode :manual.
selection a topic (string) or a map:
:topic (string) name of topic to listen to:group (string) name of listening group (default: "default")message the record that has been processedThe selection must match the what was passed to start-consumer.
Note: when using commit-mode :auto and :tx-wrap, this is handled
automatically.
Only use with consumer tx-mode `:manual`.
- `selection` a topic (string) or a map:
- `:topic` (string) name of topic to listen to
- `:group` (string) name of listening group (default: "default")
- `message` the record that has been processed
The selection must match the what was passed to `start-consumer`.
Note: when using commit-mode `:auto` and `:tx-wrap`, this is handled
automatically.(ensure-subscription config selection & {:keys [from]})Ensure a subscription exists for the given topic and group. If no subscription exists, one is created. If one already exists, it is left unchanged.
Returns true if a new subscription was created, false if one already existed.
selection is a topic name (string) or map with:
:topic (required) topic name:group consumer group id (default: "default")Options:
:from starting position when a new subscription is created:
- :earliest start from the beginning of the topic (default)
- :latest start from the current end of the topic (skips existing records)
- nat-int? start from a specific eidEnsure a subscription exists for the given topic and group. If no subscription
exists, one is created. If one already exists, it is left unchanged.
Returns true if a new subscription was created, false if one already existed.
`selection` is a topic name (string) or map with:
- `:topic` (required) topic name
- `:group` consumer group id (default: "default")
Options:
- `:from` starting position when a new subscription is created:
- `:earliest` start from the beginning of the topic (default)
- `:latest` start from the current end of the topic (skips existing records)
- nat-int? start from a specific eid(ensure-topic config topic & {:as opts})Find or create a topic. This takes the same arguments as add-topic!.
If the topic already exists with the same key-type and value-type, returns
it unchanged. Throws if the topic exists but was created with different
column types. Note: opts apply only at creation time and cannot be used
to change an existing topic.
Find or create a topic. This takes the same arguments as `add-topic!`. If the topic already exists with the same key-type and value-type, returns it unchanged. Throws if the topic exists but was created with different column types. Note: opts apply only at creation time and cannot be used to change an existing topic.
(init! config)Create all necessary tables and functions in a connected postgres database.
config should be a connected config map
Create all necessary tables and functions in a connected postgres database. `config` should be a connected config map
(list-subscriptions config & {:as opts})Returns a vector of all subscriptions, ordered by topic and group. Each subscription is a map with:
:topic the topic name:group consumer group id:offset last committed eid for this group:topic-eid highest eid available in the topic:lag topic-eid - offset: an upper bound on unread records, since eids can have gaps:updated-at java.time.Instant of the last offset commit (nil if never committed):timestamp java.time.Instant of the last consumed record (nil if offset is 0):topic-timestamp java.time.Instant of the latest record in the topic (nil if empty):timestamp-lag java.time.Duration between subscription and topic timestamps (nil if either is nil):processing-delay java.time.Duration from publish time to consumer commit for the most
recently consumed record (nil if either :updated-at or :timestamp is nil)
Options::selections a collection of filters; each item is either a topic name string
(all groups for that topic) or a map with:
- :topic (required) topic name
- :group consumer group id; when omitted, all groups for the topic are returned
When not provided, all subscriptions are returned.Returns a vector of all subscriptions, ordered by topic and group.
Each subscription is a map with:
- `:topic` the topic name
- `:group` consumer group id
- `:offset` last committed eid for this group
- `:topic-eid` highest eid available in the topic
- `:lag` topic-eid - offset: an upper bound on unread records, since eids can have gaps
- `:updated-at` java.time.Instant of the last offset commit (nil if never committed)
- `:timestamp` java.time.Instant of the last consumed record (nil if offset is 0)
- `:topic-timestamp` java.time.Instant of the latest record in the topic (nil if empty)
- `:timestamp-lag` java.time.Duration between subscription and topic timestamps (nil if either is nil)
- `:processing-delay` java.time.Duration from publish time to consumer commit for the most
recently consumed record (nil if either :updated-at or :timestamp is nil)
Options:
- `:selections` a collection of filters; each item is either a topic name string
(all groups for that topic) or a map with:
- `:topic` (required) topic name
- `:group` consumer group id; when omitted, all groups for the topic are returned
When not provided, all subscriptions are returned.(list-topics config)Returns a vector of all topics in the ottla schema, ordered by name. Each topic is a map with :topic, :key-type, and :value-type.
Returns a vector of all topics in the ottla schema, ordered by name. Each topic is a map with :topic, :key-type, and :value-type.
(make-config conn-map & {:as opts})Build a config map for use with ottla functions.
conn-map a pg2 connection map (host, port, database, user, password, etc.)opts optional keyword args:
:schema the PostgreSQL schema name to use (default: "ottla")Build a config map for use with ottla functions. - `conn-map` a pg2 connection map (host, port, database, user, password, etc.) - `opts` optional keyword args: - `:schema` the PostgreSQL schema name to use (default: "ottla")
(remove-subscription! config selection)Delete the subscription for the given topic and group. Returns true if the subscription existed and was removed, false if no subscription was found.
selection is a topic name (string) or map with:
:topic (required) topic name:group consumer group id (default: "default")Delete the subscription for the given topic and group. Returns true if the subscription existed and was removed, false if no subscription was found. `selection` is a topic name (string) or map with: - `:topic` (required) topic name - `:group` consumer group id (default: "default")
(remove-topic! config topic)Remove a topic. Warning: permanently and immediately removes all records for the topic.
config a connected config maptopic the name of the topicRemove a topic. Warning: permanently and immediately removes all records for the topic. - `config` a connected config map - `topic` the name of the topic
(reset-consumer-offset! config selection new-offset)Change a consumer's offset to the provided number exactly.
Use 0 as the new-offset to replay all records from the earliest
available.
Change a consumer's offset to the provided number exactly. Use `0` as the new-offset to replay all records from the earliest available.
(start-consumer config selection handler & {:as opts})Alias for start-standalone-consumer.
Alias for `start-standalone-consumer`.
(start-listener config & {:as opts})Start a LISTEN/NOTIFY listener that many consumers can share, using a
single database connection. Pass it to start-listening-consumer.
The listener is Closeable; close it after the consumers that use it.
Like a consumer, a listener implements ottla.protocols/IShutdown and
ottla.protocols/IStatus. Its status reports :state, :connected?,
:listening?, :topics, and :reconnects.
All topics NOTIFY on a single channel, ottla, so a listener LISTENs
once and routes notifications to its consumers by topic.
The listener runs as one long-lived task on an executor, which it occupies for as long as the listener is open.
config a config map (:conn-map is used to connect)opts option map:
:conn-map a pg2 connection map for the listener, in place of
the config's (e.g. to LISTEN on a direct connection
while fetches go through PgBouncer):executor an ExecutorService to run the listener's task on. It
must have a thread to spare for as long as the
listener is open. When omitted, the listener
creates a single-threaded one, and shuts it down on
close. On close, the listener cancels its task on an
executor you pass in, but never shuts it down.:listen-min-ms (int) min delay between checks, used right after activity (default 10):listen-max-ms (int) max delay between checks; the delay doubles while idle up to this (default 250):reconnect-ms (int) how long to wait before reconnecting after a disconnect (default 2000):await-close-ms (int) when closing, how long to wait for the listener's task to stop (default 1000):exception-handler (fn [e]) handle exceptions raised while waking consumersStart a LISTEN/NOTIFY listener that many consumers can share, using a
single database connection. Pass it to `start-listening-consumer`.
The listener is Closeable; close it after the consumers that use it.
Like a consumer, a listener implements `ottla.protocols/IShutdown` and
`ottla.protocols/IStatus`. Its status reports `:state`, `:connected?`,
`:listening?`, `:topics`, and `:reconnects`.
All topics NOTIFY on a single channel, `ottla`, so a listener LISTENs
once and routes notifications to its consumers by topic.
The listener runs as one long-lived task on an executor, which it
occupies for as long as the listener is open.
- `config` a config map (`:conn-map` is used to connect)
- `opts` option map:
- `:conn-map` a pg2 connection map for the listener, in place of
the config's (e.g. to LISTEN on a direct connection
while fetches go through PgBouncer)
- `:executor` an ExecutorService to run the listener's task on. It
must have a thread to spare for as long as the
listener is open. When omitted, the listener
creates a single-threaded one, and shuts it down on
close. On close, the listener cancels its task on an
executor you pass in, but never shuts it down.
- `:listen-min-ms` (int) min delay between checks, used right after activity (default 10)
- `:listen-max-ms` (int) max delay between checks; the delay doubles while idle up to this (default 250)
- `:reconnect-ms` (int) how long to wait before reconnecting after a disconnect (default 2000)
- `:await-close-ms` (int) when closing, how long to wait for the listener's task to stop (default 1000)
- `:exception-handler` (fn [e]) handle exceptions raised while waking consumers(start-listening-consumer config listener selection handler & {:as opts})Start a consumer that learns about new records through listener, a
listener from start-listener that many consumers can share. The
consumer opens no LISTEN connection of its own. Close the listener after
the consumers that use it.
As with start-standalone-consumer, the consumer runs its handler on a worker
thread of its own, keeps a database connection of its own for fetching
and committing, and creates its subscription if it doesn't exist yet.
Only one consumer per group is active at a time. A consumer fetches only
while its connection's session holds an advisory lock on the group's
subscription; other consumers in the group, in this process or others,
wait as hot standbys, trying the lock on each notification or poll.
(status consumer) reports :active?. Because session advisory locks
need a real session, don't connect consumers through PgBouncer in
transaction pooling mode.
config a config map. The consumer connects from its :conn-map
(or the :conn-map option), and closes that connection on close. It
ignores the config's :conn, so a config with a pool in :conn for
producers can be passed to consumers as-is. If the connection
breaks, the consumer reconnects on a later notification or poll, no
sooner than :reconnect-ms after, and comes back as a standby.listener a listener from start-listenerselection a topic (string) or a map:
:topic (string) name of topic to listen to:group (string) name of listening group (default: "default"):commit-mode #{:manual :auto :tx-wrap} (default: :auto)handler a function that will receive a sequence of deserialized recordsopts option map:
:poll-ms (int) how often to poll for new records (default 15000):await-close-ms (int) how long to wait when closing consumer:scheduler a ScheduledExecutorService for polling. When
omitted, the consumer creates a single-threaded one,
and shuts it down on close. A scheduler you pass in
is never shut down by the consumer.:exception-handler (fn [e]) handle raised exceptions:xform (fn) transducer for post-deserialization processing:deserialize-key Key deserializer. Keyword or function:deserialize-value Value deserializer. Keyword or function:max-records (int) max records fetched per batch (default 100):conn-map a pg2 connection map for the consumer's
connection, in place of the config's:reconnect-ms (int) how long to wait before reconnecting after
the connection breaks (default 2000)Deserializers do the opposite of serializers, turning a serialized value into a deserialized one. The same built-ins are available as for the serializers: :string, :json, and :edn
Start a consumer that learns about new records through `listener`, a
listener from `start-listener` that many consumers can share. The
consumer opens no LISTEN connection of its own. Close the listener after
the consumers that use it.
As with `start-standalone-consumer`, the consumer runs its handler on a worker
thread of its own, keeps a database connection of its own for fetching
and committing, and creates its subscription if it doesn't exist yet.
Only one consumer per group is active at a time. A consumer fetches only
while its connection's session holds an advisory lock on the group's
subscription; other consumers in the group, in this process or others,
wait as hot standbys, trying the lock on each notification or poll.
`(status consumer)` reports `:active?`. Because session advisory locks
need a real session, don't connect consumers through PgBouncer in
transaction pooling mode.
- `config` a config map. The consumer connects from its `:conn-map`
(or the `:conn-map` option), and closes that connection on close. It
ignores the config's `:conn`, so a config with a pool in `:conn` for
producers can be passed to consumers as-is. If the connection
breaks, the consumer reconnects on a later notification or poll, no
sooner than `:reconnect-ms` after, and comes back as a standby.
- `listener` a listener from `start-listener`
- `selection` a topic (string) or a map:
- `:topic` (string) name of topic to listen to
- `:group` (string) name of listening group (default: "default")
- `:commit-mode` #{:manual :auto :tx-wrap} (default: :auto)
- `handler` a function that will receive a sequence of deserialized records
- `opts` option map:
- `:poll-ms` (int) how often to poll for new records (default 15000)
- `:await-close-ms` (int) how long to wait when closing consumer
- `:scheduler` a ScheduledExecutorService for polling. When
omitted, the consumer creates a single-threaded one,
and shuts it down on close. A scheduler you pass in
is never shut down by the consumer.
- `:exception-handler` (fn [e]) handle raised exceptions
- `:xform` (fn) transducer for post-deserialization processing
- `:deserialize-key` Key deserializer. Keyword or function
- `:deserialize-value` Value deserializer. Keyword or function
- `:max-records` (int) max records fetched per batch (default 100)
- `:conn-map` a pg2 connection map for the consumer's
connection, in place of the config's
- `:reconnect-ms` (int) how long to wait before reconnecting after
the connection breaks (default 2000)
Deserializers do the opposite of serializers, turning a serialized
value into a deserialized one. The same built-ins are available as
for the serializers: :string, :json, and :edn
(start-poll-only-consumer config selection handler & {:as opts})Start a poll-only consumer. It uses no listener and no LISTEN
connection, and picks up new records every :poll-ms, so lower
:poll-ms to suit the latency you need.
Takes the same arguments and options as start-listening-consumer,
minus the listener.
Start a poll-only consumer. It uses no listener and no LISTEN connection, and picks up new records every `:poll-ms`, so lower `:poll-ms` to suit the latency you need. Takes the same arguments and options as `start-listening-consumer`, minus the listener.
(start-standalone-consumer config selection handler & {:as opts})Start a standalone consumer. The consumer runs its handler on a worker thread of its own, keeps a database connection of its own for fetching and committing, and starts a listener of its own, with its own LISTEN connection, to learn about new records as they're appended.
Only one consumer per group is active at a time; any others wait as
hot standbys. See start-listening-consumer.
To share one listener across many consumers, use
start-listening-consumer instead. To poll only, with no listener,
use start-poll-only-consumer.
If no subscription exists for the given topic and group, one is created automatically at startup with the cursor set to 0 (reads from the beginning of the topic).
config a config map. See start-listening-consumer for how the
consumer connects; its listener connects the same way.selection a topic (string) or a map:
:topic (string) name of topic to listen to:group (string) name of listening group (default: "default"):commit-mode #{:manual :auto :tx-wrap} (default: :auto)handler a function that will receive a sequence of deserialized recordsopts option map. All the options of start-listening-consumer, plus
these, which configure the consumer's listener:
:listen-min-ms (int) min delay between LISTEN checks, used right after activity (default 10):listen-max-ms (int) max delay between LISTEN checks; the delay doubles while idle up to this (default 250):listen-ms (int) alias for :listen-max-ms
:conn-map and :reconnect-ms apply to the listener as well as to
the consumer's own connection.The :exception-handler also handles exceptions raised in the listener.
Start a standalone consumer. The consumer runs its handler on a worker
thread of its own, keeps a database connection of its own for fetching
and committing, and starts a listener of its own, with its own LISTEN
connection, to learn about new records as they're appended.
Only one consumer per group is active at a time; any others wait as
hot standbys. See `start-listening-consumer`.
To share one listener across many consumers, use
`start-listening-consumer` instead. To poll only, with no listener,
use `start-poll-only-consumer`.
If no subscription exists for the given topic and group, one is created
automatically at startup with the cursor set to 0 (reads from the
beginning of the topic).
- `config` a config map. See `start-listening-consumer` for how the
consumer connects; its listener connects the same way.
- `selection` a topic (string) or a map:
- `:topic` (string) name of topic to listen to
- `:group` (string) name of listening group (default: "default")
- `:commit-mode` #{:manual :auto :tx-wrap} (default: :auto)
- `handler` a function that will receive a sequence of deserialized records
- `opts` option map. All the options of `start-listening-consumer`, plus
these, which configure the consumer's listener:
- `:listen-min-ms` (int) min delay between LISTEN checks, used right after activity (default 10)
- `:listen-max-ms` (int) max delay between LISTEN checks; the delay doubles while idle up to this (default 250)
- `:listen-ms` (int) alias for `:listen-max-ms`
`:conn-map` and `:reconnect-ms` apply to the listener as well as to
the consumer's own connection.
The `:exception-handler` also handles exceptions raised in the listener.(topic-subscriptions config & {:as opts})Returns all topics with their consumer group subscriptions nested under each
topic. Unlike list-subscriptions, every topic appears in the result even if
it has no subscribers.
Each entry is a map with:
:topic the topic name:subscriptions a vector of subscription maps (empty when no subscribers)Each subscription map contains the same keys as list-subscriptions (except
:topic, which is on the outer map)
Options:
:selections a collection of filters; each item is either a topic name string
(all groups for that topic) or a map with:
- :topic (required) topic name
- :group consumer group id; when omitted, all groups for the topic are returned
When not provided, all topics and their subscriptions are returned.Returns all topics with their consumer group subscriptions nested under each
topic. Unlike `list-subscriptions`, every topic appears in the result even if
it has no subscribers.
Each entry is a map with:
- `:topic` the topic name
- `:subscriptions` a vector of subscription maps (empty when no subscribers)
Each subscription map contains the same keys as `list-subscriptions` (except
`:topic`, which is on the outer map)
Options:
- `:selections` a collection of filters; each item is either a topic name string
(all groups for that topic) or a map with:
- `:topic` (required) topic name
- `:group` consumer group id; when omitted, all groups for the topic are returned
When not provided, all topics and their subscriptions are returned.(trim-topic! config topic & {:as opts})Delete records from a topic. Exactly one of the following options must be provided:
:before-eid delete all records with eid less than this value:before-timestamp delete all records with timestamp before this value:all? delete all records before the current maximum eid;
the most recent record is always retainedBy default, the deletion is clamped to the minimum subscription cursor across
all consumer groups, preserving records not yet consumed by any subscriber.
Pass :ignore-subscriptions? true to delete unconditionally.
Returns the number of records deleted.
Delete records from a topic. Exactly one of the following options must be provided:
- `:before-eid` delete all records with eid less than this value
- `:before-timestamp` delete all records with timestamp before this value
- `:all?` delete all records before the current maximum eid;
the most recent record is always retained
By default, the deletion is clamped to the minimum subscription cursor across
all consumer groups, preserving records not yet consumed by any subscriber.
Pass `:ignore-subscriptions? true` to delete unconditionally.
Returns the number of records deleted.(with-connected-config [sym config] & body)Ensures config has an open database connection for the duration of body,
binding the connected config to sym. If the config is already connected,
the existing connection is used as-is. Otherwise, a new connection is opened
and automatically closed when body completes (even on exception).
Ensures `config` has an open database connection for the duration of `body`, binding the connected config to `sym`. If the config is already connected, the existing connection is used as-is. Otherwise, a new connection is opened and automatically closed when `body` completes (even on exception).
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |