social: add Client.Unsubscribe for per-handler removal - #20
Conversation
Client.CloseContext unsubscribes the shared RTA subscription and clears every registered handler, so a caller that only wants to remove its own handler (for example on shutdown) also disables any other Subscribe users on the same client. Add Client.Unsubscribe(ctx, handler), which removes a single handler by identity and tears the RTA subscription down only once the last handler is gone. Handlers must be comparable to be removed; a non-comparable handler returns an error rather than panicking, preserving support for non-comparable handlers in Subscribe/dispatch.
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_088cb957-ff83-48a1-a3ee-5b4105ec807d) |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Closing one broadcaster called social CloseContext, which unsubscribes the shared RTA subscription and clears every handler on the client. That could disable other Social().Subscribe users on a shared xsapi.Client. Use the new go-xsapi social Client.Unsubscribe(ctx, handler) to remove only the broadcaster's own handler; the RTA subscription is torn down only when its last handler is gone. Bumps the go-xsapi replace to the commit that adds Unsubscribe. Depends on HashimTheArab/go-xsapi#20; the replace pins that branch commit and should be re-pinned to lunar once it merges.
Summary
social.Client.CloseContextunsubscribes the shared RTA subscription and clears every registered handler (c.subscriptionHandlers = nil). A caller that only wants to remove its own handler — e.g. on shutdown — therefore also disables any otherSubscribeusers sharing the same*xsapi.Client.Add
Client.Unsubscribe(ctx, handler)for precise, per-handler removal.Behaviour
Subscribe.rta.ErrUnavailableon teardown, which is treated as success (nothing to unsubscribe).Non-comparable handlers
Subscribe/dispatch already support non-comparable handlers, so removal can't rely on==blindly.Unsubscribechecksreflect.TypeOf(h).Comparable()up front and returns an error (rather than panicking) for a non-comparable handler. With a comparableh, theexisting == hscan is panic-free: a differing dynamic type compares unequal, and an identical type is comparable.Tests
TestUnsubscribeRemovesOnlyTheGivenHandler— removing one of two handlers leaves the other receiving events, keeps the subscription until the last is gone, then tears it down; a repeat removal is a no-op.TestUnsubscribeRejectsNonComparableHandler— a non-comparable handler returns an error instead of panicking.Race-clean; full
socialpackage passes.Note
Medium Risk
Changes shared RTA subscription lifecycle and handler list under the same mutex as Subscribe; incorrect teardown could affect other subscribers, though behavior is covered by new tests and mirrors existing CloseContext patterns.
Overview
Adds
Client.Unsubscribe(ctx, h)so a caller can drop only its own RTA social subscription handler without affecting otherSubscribeusers on the same*social.Client—unlikeCloseContext, which unsubscribes and clears every handler.Removal matches handlers by identity (
==on the value passed toSubscribe). Non-comparable handlers get an upfrontreflect.TypeOfcheck and an error instead of a panic during lookup. Unregistering an unknown handler is a no-op; when the last handler is removed, the shared RTA subscription is torn down viaunsubscriber, treatingrta.ErrUnavailableon a never-active subscription as success.Tests cover selective removal, event delivery to the surviving handler, teardown only after the last handler, repeat unsubscribe as no-op, and rejection of non-comparable handlers.
Reviewed by Cursor Bugbot for commit b780abd. Bugbot is set up for automated code reviews on this repo. Configure here.