Fork me on GitHub
#aleph
<
2019-02-28
>
Empperi09:02:10

Hi! Using websocket-client to connect into websocket endpoint. This works just fine but I have a problem when target endpoint is not there when trying to make the connection

Empperi09:02:27

The connection creation just hangs forever, no errors or anything

Empperi09:02:31

I must be missing something

Empperi09:02:16

I think it is because websocket-client returns a deferred which I must first dereference to use it

Empperi09:02:47

And that dereferencing will hang until the connection is established, which it never manages to do since the target endpoint isn't available

Empperi09:02:53

So, is there a way to tell the websocket-client a connection timeout?

kachayev09:02:19

First, you can use (d/timeout _) to fail the deferred after timeout you need

Empperi09:02:35

OK, that was what I was more or less looking for I think

kachayev09:02:37

Second, there’s a ConnectionTimeout that fires when you cannot establish a TCP connection. The problem with Websocket protocol is that it requires one another round-trip to setup the stream: handshake request/response (see https://github.com/ztellman/aleph/pull/422 and https://github.com/netty/netty/issues/8841). If your server is “not there”, it fails. If your server unable to finish the handshake - it’s not managed by the Aleph for now (it would be after PR is merged tho’).

Empperi09:02:50

Well in this case the target server is starting sometimes when I'm trying to establish connection

Empperi09:02:04

And it will get up eventually

Empperi09:02:14

(or if it doesn't then there's a bigger problem)

kachayev09:02:02

(-> (websocket-client ...)
    (d/timeout! 5e3 ::timeout)
    (d/chain' (fn [conn] (if-not (identical? ::timeout conn) conn (time/in 5e3 (websocket-client ...))))))

kachayev09:02:32

Out of my head, didn’t test it 🙂

kachayev09:02:00

TCP doesn’t have a solution for “server is starting” as a protocol, unfortunately 😉

Empperi09:02:09

Yeah I know

Empperi09:02:30

It is pretty obvious that I need to retry a bit later if it isn't just there

Empperi09:02:43

And do that for x times or give up

igrishaev10:02:51

Hi! There is a lot of middleware for the HTTP client. Yet I don’t understand how to use them when sending HTTP calls. For example, when GET-ting a JSON file, how to finish with the body being parsed automatically? What option should I specify?

igrishaev10:02:21

What I’ve tried:

@(http/get "")
returns just a byte stream as body

igrishaev10:02:41

@(http/get "" 
           {:middleware middleware/wrap-request})
gives the same result

kachayev10:02:23

Wrap-request is applied automatically

kachayev10:02:40

:as :json should work

igrishaev11:02:50

what works! Does it mean, I can just pass the standard clj-http args?

kachayev11:02:14

Almost always :)

kachayev11:02:32

There’s a list of those things that are different

igrishaev11:02:58

when sending JSON body, with that work?

:form-params {:foo 42}
:content-type :json

kachayev11:02:39

Or :application/json

kachayev11:02:47

I don’t remember exactly

igrishaev11:02:52

yes, it seems to be the full version (defmethod coerce-form-params :application/json

kachayev11:02:10

What about clj-http? Do they accept short version?

igrishaev11:02:29

yes, the take just :content-type :json

kachayev11:02:53

Feel free to open an issue with “clj-http parity” tag :)

kachayev11:02:00

Or I’ll do that in the evening

igrishaev11:02:32

but it’s not a problem, really. Instead, it would be nice to add more examples in readme because auto encode/decode JSON happens constantly

igrishaev11:02:54

ok probably I’ll contribute

kachayev11:02:44

Let me know if I can help with anything

igrishaev11:02:23

what is good about clj-http, you never remember all the options, but they’ve got rich base of examples, so you just copy and paste

kachayev11:02:28

The idea was that you can just copy example for clj-http and it works 😂

kachayev11:02:45

But, yeah... documentation is a weak point for now

kachayev11:02:59

And it requires constant attention

igrishaev11:02:11

maybe I was confused with a docstring to aleph.http/request function which highlights only the basic args

igrishaev11:02:40

but it’s clear now, thanks a lot!

kachayev11:02:26

My pleasure!