Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LongpollManager ¶
type LongpollManager struct { SubscriptionHandler func(w http.ResponseWriter, r *http.Request) // contains filtered or unexported fields }
LongpollManager provides an interface to interact with the internal longpolling pup-sub goroutine.
This allows you to publish events via Publish() If for some reason you want to stop the pub-sub goroutine at any time you can call Shutdown() and all longpolling will be disabled. Note that the pub-sub goroutine will exit on program exit, so for most simple programs, calling Shutdown() is not necessary.
A LongpollManager is created with each subscriptionManager that gets created by CreateLongpollManager() This interface also exposes the HTTP handler that client code can attach to a URL like so:
mux := http.NewServeMux() mux.HandleFunc("/custom/path/to/events", manager.SubscriptionHandler)
Note, this http handler can be wrapped by another function (try capturing the manager in a closure) to add additional validation, access control, or other functionality on top of the subscription handler.
You can have another http handler publish events by capturing the manager in a closure and calling manager.Publish() from inside a http handler. See the advanced example (examples/advanced/advanced.go)
If for some reason you want multiple goroutines handling different pub-sub channels, you can simply create multiple LongpollManagers.
func CreateCustomManager ¶
func CreateCustomManager(maxTimeoutSeconds, eventBufferSize int, loggingEnabled bool) (*LongpollManager, error)
Creates a custom LongpollManager and pub-sub goroutine connected via channels that are exposed via LongpollManager's Publish() function and SubscriptionHandler field which can get used as an http handler function.
The options are as follows. maxTimeoutSeconds, the max number of seconds a longpoll web request can wait before returning a timeout response in the event of no events on that subscription category.
eventBufferSize, the number of events that get buffered per subscription category before we start throwing the oldest events out. These buffers are used to support longpolling for events in the past, and giving a better guarantee that any events that occurred between client's longpoll requests can still be seen by their next request.
loggingEnabled, whether or not log statements are printed out.
func CreateManager ¶
func CreateManager() (*LongpollManager, error)
Creates a basic LongpollManager and pub-sub goroutine connected via channels that are exposed via LongpollManager's Publish() function and SubscriptionHandler field which can get used as an http handler function. This basic LongpollManager's default options should cover most client's longpolling needs without having to worry about details. This uses the following options: maxTimeoutSeconds of 180. eventBufferSize size 250 (buffers this many most recent events per subscription category) and loggingEnabled as true.
Creates a basic LongpollManager with the default settings. This manager is an interface on top of an internal goroutine that uses channels to support event pub-sub.
The default settings should handle most use cases, unless you expect to have a large number of events on a given subscription category within a short amount of time. Perhaps having more than 50 events a second on a single subscription category would be the time to start considering tweaking the settings via CreateCustomManager.
The default settings are: a max longpoll timeout window of 180 seconds, the per-subscription-category event buffer size of 250 events, and logging enabled.
func (*LongpollManager) Publish ¶
func (m *LongpollManager) Publish(category string, data interface{}) error
Publish an event for a given subscription category. This event can have any arbitrary data that is convert-able to JSON via the standard's json.Marshal() the category param must be a non-empty string no longer than 1024, otherwise you get an error.
func (*LongpollManager) Shutdown ¶
func (m *LongpollManager) Shutdown()
Shutdown allows the internal goroutine that handles the longpull pup-sub to be stopped. This may be useful if you want to turn off longpolling without terminating your program. After a shutdown, you can't call Publish() or get any new results from the SubscriptionHandler. Multiple calls to this function on the same manager will result in a panic.
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
advanced
This is a more advanced example that shows a few more possibilities when using golongpoll.
|
This is a more advanced example that shows a few more possibilities when using golongpoll. |
basic
This is a basic example of how to use golongpoll.
|
This is a basic example of how to use golongpoll. |