Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overrriding OnSubscribe and OnUnsubscribe functions per Stream. #138

Open
dylangleason opened this issue Sep 2, 2022 · 0 comments

Comments

@dylangleason
Copy link

Goal

While the Server supports initializing the server with global OnSubscribe and OnUnsubscribe callbacks, this applies to all streams. Allowing customized behavior for these events on a per-stream basis provides greater flexibility.

Problem

The Server contains only an unexported getStream method, so the consumer of this library cannot set the OnSubscribe callback on the Stream directly. This seems like a sensible choice, especially since access to resources on the Server requires synchronization via a sync.Mutex.

However the programmer can still kind of get around this by doing doing the following:

stream := server.CreateStream(stream)    // only creates the stream if it doesn't already exist
stream.OnSubscribe = customSubscribeFunc // not atomic, since CreateStream uses a mutex

While this kind of works, it seems like this was not the intent of the API and seems unsafe to rely on.

Proposed Solution

Export a method on the Server instance that allows customizing the callback per stream, for example:

func (s *Server) OverrideOnSubscribeCallbackForStream(streamID string, callback func(string, *Subscriber)) {
    s.mu.Lock()
    defer s.mu.Unlock()
    s.Streams[streamID].OnSubscribe = callback
}

Example use case:

s.OverrideOnSubscribeCallback(stream, myCustomStreamFunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant