Packages
eventstore
1.0.0-rc.0
1.4.8
1.4.7
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.11.0-rc.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Event store using PostgreSQL for persistence.
Current section
Files
Jump to
Current section
Files
lib/event_store/subscriptions/subscriber.ex
defmodule EventStore.Subscriptions.Subscriber do
@moduledoc false
defstruct [
:pid,
:ref,
:partition_key,
last_sent: 0,
buffer_size: 1,
in_flight: []
]
alias EventStore.RecordedEvent
alias EventStore.Subscriptions.Subscriber
@doc """
Subscriber is available to receive events when the number of in-flight events
is less than its configured buffer size. By default this is set to one event.
"""
def available?(%Subscriber{in_flight: []}), do: true
def available?(%Subscriber{in_flight: in_flight, buffer_size: buffer_size}),
do: length(in_flight) < buffer_size
@doc """
Is the given event in the same partition as any in-flight events?
"""
def in_partition?(%Subscriber{partition_key: nil}, _partition_key), do: false
def in_partition?(%Subscriber{partition_key: partition_key}, partition_key), do: true
def in_partition?(%Subscriber{}, _partition_key), do: false
def track_in_flight(%Subscriber{} = subscriber, %RecordedEvent{} = event, partition_key) do
%Subscriber{in_flight: in_flight} = subscriber
%RecordedEvent{event_number: event_number} = event
%Subscriber{
subscriber
| in_flight: [event | in_flight],
last_sent: event_number,
partition_key: partition_key
}
end
def reset_in_flight(%Subscriber{} = subscriber) do
%Subscriber{subscriber | in_flight: [], partition_key: nil}
end
@doc """
Acknowledge the in-flight event by number and all events sent to the
subscriber before the ack'd event.
"""
def acknowledge(%Subscriber{} = subscriber, ack) do
%Subscriber{in_flight: in_flight} = subscriber
case ack_event_index(in_flight, ack) do
nil ->
{:error, :unexpected_ack}
index ->
# All in-flight events up to the ack'd event number are also ack'd
{in_flight, acknowledged_events} = Enum.split(in_flight, index)
subscriber =
case in_flight do
[] ->
%Subscriber{subscriber | in_flight: [], partition_key: nil}
in_flight ->
%Subscriber{subscriber | in_flight: in_flight}
end
{:ok, subscriber, acknowledged_events}
end
end
defp ack_event_index(in_flight, ack) do
Enum.find_index(in_flight, fn
%RecordedEvent{event_number: ^ack} -> true
%RecordedEvent{} -> false
end)
end
end