koding / websocketproxy

WebSocket reverse proxy handler for Go
http://godoc.org/github.com/koding/websocketproxy
MIT License
427 stars 120 forks source link

Proposal - Let WebSocketProxy use an "event/state observer" callback #22

Open janmiderback opened 6 years ago

janmiderback commented 6 years ago

Motivation

Some of this is possible now, but it requires to wrap ResponseWriters and it is not granular enough.

Implementation

It could be something like:

type Event int

const (
    InternalError Event = iota
    BackendDialFailed
    BackendDialSucceeded
    UpgradeFailed
    UpgradeSucceeded
    Closed
    ClosedAbnormalClosure
)

type WebsocketProxy struct {
    ...

    // Observer, if non-nil is called at certain actions and states through the
    // WebSocket upgrade process carried out in ServeHTTP. It provides a way for
    // clients/middleware to hook in actions in these notifications.
    // The Event parameter carries the event and the string parameter carries
    // an informational string suitable for logging.
    Observer func(Event, string)

    ...
}

The provided event and the Observer signature are just a suggestion.

Also, events could be notified using a channel. However, actions on the receiver side should be short, and channel messaging could be implemented in the observer, if needed.