NFIBrokerage / spear

A sharp EventStoreDB v20+ client backed by Mint :yum:
https://hex.pm/packages/spear
Apache License 2.0
85 stars 14 forks source link

getPersistentSubcriptionInfo/1 ? #74

Closed kristofka closed 1 year ago

kristofka commented 1 year ago

Hi, some of the other clients implement a getPersistentSubscriptionInfo function, granted, it's easy enough to implement with Spear.request/5 eg:

iex(1)>  {:ok, conn} = Spear.Connection.start_link(connection_string: "esdb://localhost:2113")
{:ok, #PID<0.234.0>}
iex(2)> require Spear.Records.Persistent
Spear.Records.Persistent
iex(3)> require Spear.Records.Shared
Spear.Records.Shared
iex(4)> stream_id = Spear.Records.Shared.stream_identifier(stream_name: "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8")
{:"event_store.client.StreamIdentifier",
 "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8"}
iex(5)> stream_options = Spear.Records.Persistent.get_info_req_options(stream_option:  {:stream_identifier, stream_id}, group_name: "subscriber")
{:"event_store.client.persistent_subscriptions.GetInfoReq.Options",
 {:stream_identifier,
  {:"event_store.client.StreamIdentifier",
   "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8"}}, "subscriber"}
iex(6)> get_info_message= Spear.Records.Persistent.get_info_req(options: stream_options)
{:"event_store.client.persistent_subscriptions.GetInfoReq",
 {:"event_store.client.persistent_subscriptions.GetInfoReq.Options",
  {:stream_identifier,
   {:"event_store.client.StreamIdentifier",
    "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8"}}, "subscriber"}}
iex(7)> {:ok, resp} = Spear.request(conn, Spear.Records.Persistent,:GetInfo, [get_info_message])
{:ok,
 {:"event_store.client.persistent_subscriptions.GetInfoResp",
  {:"event_store.client.persistent_subscriptions.SubscriptionInfo",
   "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8", "subscriber",
   "Live", [], 0, 2, 0, "", "2", true, "0", 10000, false, 10, 100, 300, 100,
   3000, 1, 100, 2, 3, 1, 0, 0, "RoundRobin", 1, 0}}}
iex(8)> {_, infos} = resp
{:"event_store.client.persistent_subscriptions.GetInfoResp",
 {:"event_store.client.persistent_subscriptions.SubscriptionInfo",
  "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8", "subscriber", "Live",
  [], 0, 2, 0, "", "2", true, "0", 10000, false, 10, 100, 300, 100, 3000, 1,
  100, 2, 3, 1, 0, 0, "RoundRobin", 1, 0}}
iex(9)>  Spear.Records.Persistent.subscription_info(infos) |> Enum.into(%{})
%{
  average_per_second: 0,
  buffer_size: 300,
  check_point_after_milliseconds: 3000,
  connections: [],
  count_since_last_measurement: 0,
  event_source: "$ce-commandedtestdadc98d0_31d8_4518_b4a2_d4a148595ec8",
  extra_statistics: false,
  group_name: "subscriber",
  last_checkpointed_event_position: "",
  last_known_event_position: "2",
  live_buffer_count: 3,
  live_buffer_size: 100,
  max_check_point_count: 100,
  max_retry_count: 10,
  max_subscriber_count: 1,
  message_timeout_milliseconds: 10000,
  min_check_point_count: 1,
  named_consumer_strategy: "RoundRobin",
  outstanding_messages_count: 0,
  parked_message_count: 0,
  read_batch_size: 100,
  read_buffer_count: 2,
  resolve_link_tos: true,
  retry_buffer_count: 1,
  start_from: "0",
  status: "Live",
  total_in_flight_messages: 0,
  total_items: 2
}

If you think such a function could live under the Spear#utility set of functions, I'd gladly attempt a pull request.

There would be two issues to keep in mind :

If you think you'd rather have client applications handle that through Spear.request/5, feel free to close the issue.

Thanks.

the-mikedavis commented 1 year ago

Ah looks like these are new RPCs: https://github.com/EventStore/EventStore/commit/31454a061fbfb27a790aa7c2e3f698bf02a79fde#diff-96a3dceee570d38ca341085c4887d112c8fc3ad5bbcf3963e3a73937c7b21b87

Adding new functions for them sounds good 👍. I think they could belong in @doc api: :persistent since they're defined in the persistent.proto. And I agree, a new struct Spear.PersistentSubscription.Info is needed - that's a lot of fields in the SubscriptionInfo message. A PR would be great!

It looks like those RPCs are only implemented in the v22.10.0 tag. For now I've been adding a note to the docs for RPCs that are implemented only on some server versions (see Spear.append_batch/5). I think other clients check the server capabilities RPC on start-up (if available) and give a more helpful error message if it isn't implemented. I left that out of the Spear.Connection module for now since it adds a lot of complexity but maybe that should be added too eventually.

kristofka commented 1 year ago

A quick update to let you know I'm working on the PR. I'll add a note in the doc stating that the function is available only for v22.10.0 and above.

The PR should come within a couple of days.

the-mikedavis commented 1 year ago

Added in #76