andreykaipov / goobs

Go client library for OBS Studio
Apache License 2.0
137 stars 22 forks source link

Add obs connection timeout #136

Closed Olex1313 closed 9 months ago

Olex1313 commented 10 months ago

Hello, I've been trying to limit time for connecting to obs with not only response timeout, but with context, maybe allow user to pass context.Context in goobs.New() via options func or add a NewWithCtx() function to pass context to gorilla dialer

andreykaipov commented 9 months ago

Hi @Olex1313, the response timeout option is for responses from an already established connection, so you're right that you can't use it to limit the connection timeout.

However you should be able to use the goobs.WithDialer option to specify the handshake timeout:

    client, err := goobs.New(
        "localhost:4455",
        goobs.WithPassword("goodpassword"),
        goobs.WithDialer(&websocket.Dialer{
            HandshakeTimeout: 3 * time.Second,
        }),
    )

The default is 45 seconds, which feels a little high to me. Maybe it's worth changing the default goobs uses? In any case, based on this bit of code, the handshake timeout is passed to context.WithTimeout, which sounds like what you're trying to do.

We could also even pass the context directly by setting the NetDial function for our custom dialer:

    dialer := websocket.DefaultDialer
    dialer.NetDial = func(network string, addr string) (net.Conn, error) {
        ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
        defer cancel()
        d := &net.Dialer{}
        return d.DialContext(ctx, network, addr)
    }
    client, err := goobs.New(
        ...,
        goobs.WithDialer(dialer),
    )
Olex1313 commented 9 months ago

@andreykaipov, Oh didn't notice Dialer option, so I think my issue is solved :), about the standard timeout, maybe it should be equal to default goobs response timeout (10s)?