Documentation
¶
Overview ¶
Package longpoll provides an implementation of the long polling mechanism of the PubSub pattern. Although the primary purpose of the package is to aid the development of web applications, it provides no specific web handlers and can be used in other distributed applications.
The package provides the Channel type to manage publishing and retrieval of information for each individual subscription, and the LongPoll type to manage subscription channels allowing for adding, removing and publishing to all.
Index ¶
- Constants
- type Channel
- func (ch *Channel) Drop()
- func (ch *Channel) Get(polltime time.Duration) (chan []interface{}, error)
- func (ch *Channel) ID() string
- func (ch *Channel) IsAlive() bool
- func (ch *Channel) IsGetWaiting() bool
- func (ch *Channel) Publish(data interface{}, topic string) error
- func (ch *Channel) QueueSize() int
- func (ch *Channel) Topics() []string
- type LongPoll
- func (lp *LongPoll) Channel(id string) (*Channel, bool)
- func (lp *LongPoll) Channels() []*Channel
- func (lp *LongPoll) Drop(id string)
- func (lp *LongPoll) Get(id string, polltime time.Duration) (chan []interface{}, error)
- func (lp *LongPoll) Ids() []string
- func (lp *LongPoll) IsAlive() bool
- func (lp *LongPoll) MustSubscribe(timeout time.Duration, topics ...string) string
- func (lp *LongPoll) Publish(data interface{}, topics ...string) error
- func (lp *LongPoll) Shutdown()
- func (lp *LongPoll) Subscribe(timeout time.Duration, topics ...string) (string, error)
- func (lp *LongPoll) Topics() []string
- type Timeout
Constants ¶
const Version = 1.2
Version of the library.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel represents a single channel for publishing and receiving data over a long-polling subscription. Data published to any of the topics subscribed to will be received by the client asking for new data. The receiving is not split by topic.
The subscription is setup to timeout if no Get request is made before the end of the timeout period provided at construction. Every Get request extends the lifetime of the subscription for the duration of the timeout.
func MustNewChannel ¶
MustNewChannel acts just like NewChannel, however, it does not return errors and panics instead.
func NewChannel ¶
NewChannel constructs a new long-polling pubsub channel with the given timeout, optional exit handler, and subscribing to given topics. Every new channel gets a unique channel/subscription Id assigned based on UUID.v4.
Constructing a channel with NewChannel starts a timeout timer. The first Get request must follow within the timeout window.
func (*Channel) Drop ¶
func (ch *Channel) Drop()
Drop terminates any publishing and receiving on the channel, signals the currently waiting Get request to return empty, terminates the timeout timer and runs the exit handler if supplied.
func (*Channel) Get ¶
Get requests data published on all of the channel topics. The function returns a channel to receive the data set on.
The request is held until data becomes available (published to a matching topic). Upon new data, or if data has been waiting at the time of the call, the request returns immediately. Otherwise it waits over the `polltime` duration and return empty if no new data arrives. It is expected that a new Get request is made immediately afterwards to receive further data and prevent channel timeout.
Multiple Get requests to the channel can be made concurrently, however, every data sample will be delivered to only one request issuer. It is not guaranteed to which one, although every new incoming request will trigger a return of any earlier one.
func (*Channel) IsGetWaiting ¶
IsGetWaiting reports if there is a Get request waiting for data.
func (*Channel) Publish ¶
Publish publishes data on the channel in a non-blocking manner if the topic corresponds to one of those provided at construction. Data published to other topics will be silently ignored. No topic information is persisted and retrieved with the data.
type LongPoll ¶
type LongPoll struct {
// contains filtered or unexported fields
}
The LongPoll type represents a subscription manager. It provides functionality to manage multiple long-polling subscriptions allowing for adding and removing subscriptions, publishing to all subscriptions, receiving data by subscription Id.
func (*LongPoll) Channel ¶
Channel returns a pointer to the subscription channel behind the given id.
func (*LongPoll) Channels ¶
Channels returns the list of all currently up and running subscription channels. For performance reasons when dealing with a large number of subscription channels all operations across all of them use this method to retrieve the list first and unlock the thread ASAP. If a subscription channel is removed after the list was retrieved, the operation will still run on that channel. If a channel is added, the operation will not apply to it.
func (*LongPoll) Drop ¶
Drop terminates a subscription channel for the given Id and removes it from the list of subscription channels.
func (*LongPoll) Get ¶
Get requests data published on all of the topics for the given subscription channel. See further info in (*Channel).Get.
func (*LongPoll) Ids ¶
Ids returns the list of Ids of all currently up and running subscription channels.
func (*LongPoll) MustSubscribe ¶
MustSubscribe acts in the same manner as Subscribe, however, it does not return errors and panics instead.
func (*LongPoll) Publish ¶
Publish publishes data on all subscription channels with minimal blocking. Data is published separately for each topic. Closed subscription channels and mismatching topics are ignored silently.
func (*LongPoll) Shutdown ¶
func (lp *LongPoll) Shutdown()
Shutdown terminates the pubsub service and drops all subscription channels.
type Timeout ¶
type Timeout struct {
// contains filtered or unexported fields
}
Timeout implements a callback mechanism on timeout (along with reporting on a buffered channel), which is extendable in time via pinging the object. An alive timeout can be dropped at any time, in which case the callback will not be executed, but the exit will still be reported on the channel.
This extendable Timeout is used for monitoring long polling subscriptions here, which would expire if no client asks for data within a defined timeout (or timeout extended otherwise).
func MustNewTimeout ¶
MustNewTimeout acts just like NewTimeout, however, it does not return errors and panics instead.
func NewTimeout ¶
NewTimeout creates and starts a new timeout timer accepting an optional exit handler.
func (*Timeout) Drop ¶
func (tor *Timeout) Drop()
Drop drops the timeout handler and reports the exit on the reporting channel. The drop will take place at most after 1/100th of the timeout and the onTimeout handler will not get called.
func (*Timeout) Ping ¶
func (tor *Timeout) Ping()
Ping pings the timeout handler extending it for another timeout duration.
func (*Timeout) ReportChan ¶
ReportChan retrieves the timeout reporting channel, which will get a true reported on exit (in case of timeout or drop).