r3labs / sse

Server Sent Events server and client for Golang
Mozilla Public License 2.0
857 stars 179 forks source link

feature: implement max log entries and replay per stream #162

Open zze0s opened 1 year ago

zze0s commented 1 year ago

Hi!

Been a happy user of this library for a couple of years and it works great! :smiley:

Something I've wanted and needed for a while is the ability to set a limit on the amount of events the event log can store, and being able to set if it should replay per stream and not globally.

This PR implements a new log store using container/list of the std library.

Along with that it adds the new method CreateStreamWithOpts(id string, opts StreamOpts) which takes a StreamOpts config:

type StreamOpts struct {
    // Max amount of log entries per stream
    MaxEntries int
    // Enables creation of a stream when a client connects
    AutoStream bool
    // Enables automatic replay for each new subscriber that connects
    AutoReplay bool
}

The benefits of a new method is the backwards compatibility is kept.

When MaxEntries is set to a value greater than 0 it will check the length on Add, then remove the log at the back, and insert the new log event.

Happy to take any feedback! And please let me know if this is something of interest at all. If not then I'll maintain a fork. :smile:

If you think container/list is a bad idea, then we could possibly expose a LogStorer interface and have that configurable by the user and keep the old implementation as default.

snowbows commented 2 weeks ago

@purehyperbole - any chance to get this feature released?