Skip to content

Timeouts

Gerasimos (Makis) Maropoulos edited this page Jun 30, 2019 · 3 revisions

Neffos provides a wrapper of neffos.Namespaces and neffos.Events structures to deal with custom Write and Read deadtimes/timeouts, it's the neffos.WithTimeout.

Its outline looks like this:

// WithTimeout completes the `ConnHandler` interface.
// Can be used to register namespaces and events or just events on an empty namespace
// with Read and Write timeouts.
//
// See `New` and `Dial`.
type WithTimeout struct {
    ReadTimeout  time.Duration
    WriteTimeout time.Duration

    Namespaces Namespaces
    Events     Events
}

It's easy to set timeouts, instead of var events = neffos.Namespaces{"namespace": neffos.Events: ...} you just wrap it with neffos.WithTimeout like this:

var events = neffos.WithTimeout {
    ReadTimeout:  60 * time.Second,
    WriteTimeout: 60 * time.Second,

    Namespaces: neffos.Namespaces {
        "namespace": neffos.Events {
            "onMyEvent": func(c *neffos.NSConn, msg neffos.Message) error {
                // [...]
            },
        },
    },
}

or for empty namespace:

var events = neffos.WithTimeout {
    ReadTimeout:  60 * time.Second,
    WriteTimeout: 60 * time.Second,

    Events: neffos.Events {
        "onMyEvent": func(c *neffos.NSConn, msg neffos.Message) error {
            // [...]
        },
    },
}
websocketServer := neffos.New(gobwas.DefaultUpgrader, events)
websocketClient, err := neffos.Dial(
    context.Background(),
    gobwas.DefaultDialer,
    "ws://localhost:8080/echo",
    events,
)

Of cource you could provide those timeouts based on what Upgrader and Dialer implementations are being used on Server and Client respectfully, i.e the gorilla's one has ReadTimeout and WriteTimeout optional fields that you can set on a custom gorilla/websocket.Upgrader structure but if you want a universal way to set your own timeouts in the connection handler level(namespaces, events) use the neffos.WithTimeout instead.