Skip to content

Connecting to non AMQP server should fail#78

Merged
dentarg merged 4 commits intomainfrom
connect-to-non-amqp-server-should-fail
Aug 8, 2023
Merged

Connecting to non AMQP server should fail#78
dentarg merged 4 commits intomainfrom
connect-to-non-amqp-server-should-fail

Conversation

@carlhoerberg
Copy link
Copy Markdown
Member

Fixes #60

@carlhoerberg
Copy link
Copy Markdown
Member Author

Probably should set a socketTimeout when connecting, and then resetting it once connected.

@carlhoerberg carlhoerberg marked this pull request as ready for review July 26, 2023 03:14
@carlhoerberg carlhoerberg requested a review from dentarg July 26, 2023 12:19
@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 26, 2023

It doesn't fail for me, trying the example from the README

$ cat examples/readme.js
import { AMQPClient } from '../lib/mjs/amqp-client.js'

async function run() {
  try {
    const amqp = new AMQPClient("amqp://localhost:15672?hearbeat=1")
    const conn = await amqp.connect()
    const ch = await conn.channel()
    const q = await ch.queue()
    const consumer = await q.subscribe({noAck: true}, async (msg) => {
      console.log(msg.bodyToString())
      await consumer.cancel()
    })
    await q.publish("Hello World", {deliveryMode: 2})
    await consumer.wait() // will block until consumer is canceled or throw an error if server closed channel/connection
    await conn.close()
  } catch (e) {
    console.error("ERROR", e)
    e.connection.close()
    setTimeout(run, 1000) // will try to reconnect in 1s
  }
}

run()
$ node --enable-source-maps examples/readme.js

$ echo $?
0

$ time node --enable-source-maps examples/readme.js
node --enable-source-maps examples/readme.js  0.15s user 0.17s system 6% cpu 5.303 total

(I checked out this branch and ran npm run build and the ran the example code)

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 26, 2023

(if I stop RabbitMQ it fails with ECONNREFUSED but that is the case with the main branch too)

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 26, 2023

AMQPClient("amqp://localhost:15672?hearbeat=1")

Oops typo in the URI, if I fix it (?heartbeat=1), it does fail

$ node --enable-source-maps examples/readme.js
ERROR /Users/dentarg/eightyfourcodes/amqp-client.js/src/amqp-socket-client.ts:61
      socket.on('timeout', () => reject(new AMQPError("timeout", this)))
                                        ^

AMQPError: timeout
    at Socket.<anonymous> (/Users/dentarg/eightyfourcodes/amqp-client.js/src/amqp-socket-client.ts:61:41)
    at Socket.emit (node:events:513:28)
    at Socket._onTimeout (node:net:550:8)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 26, 2023

Is it only when using ?heartbeat=<something> that it should fail? Maybe only when we can make it fail?

@carlhoerberg
Copy link
Copy Markdown
Member Author

Without heartbeat it will wait 60s for successful connection, with heartbeat=1, only 1 second. Could lower it, but sometimes an overloaded RabbitMQ server might take a long time to authenticate to.

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 27, 2023

The example program exit without error after ~5 seconds when not using the heartbeat parameter, is that expected or should it error too? when trying to connect to the mgmt HTTP port

@carlhoerberg
Copy link
Copy Markdown
Member Author

carlhoerberg commented Jul 27, 2023 via email

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 28, 2023

The example program do have try/catch, and if I run it with const amqp = new AMQPClient("amqp://localhost:9000") and using this branch, against the built-in web server in Python, which will respond with 400 Bad Request

$ python3 -m http.server 9000
Serving HTTP on :: port 9000 (http://[::]:9000/) ...
::ffff:127.0.0.1 - - [28/Jul/2023 10:24:18] code 400, message Bad HTTP/0.9 request type ('AMQP\\x00\\x00')
::ffff:127.0.0.1 - - [28/Jul/2023 10:24:18] "AMQP\x00\x00\x09\x01" 400 -
^C
Keyboard interrupt received, exiting.

it does error after the timeout (which I lowered to 10 seconds when testing) (full log here)

$ time node --enable-source-maps examples/readme.js
ERROR /Users/dentarg/eightyfourcodes/amqp-client.js/src/amqp-socket-client.ts:61
      socket.on('timeout', () => reject(new AMQPError("timeout", this)))
                                        ^

AMQPError: timeout

...

but if I run the example program against Puma (Ruby web server)

$ echo 'app { [200, {}, ["OK"]] }' | puma --config /dev/stdin --port 9000
Puma starting in single mode...
* Puma version: 6.3.0 (ruby 3.2.2-p53) ("Mugi No Toki Itaru")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 64142
* Listening on http://0.0.0.0:9000
Use Ctrl-C to stop
2023-07-28 10:26:12 +0200 HTTP parse error, malformed request: #<Puma::HttpParserError: Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?>

it returns directly without any error

$ time node --enable-source-maps examples/readme.js
node --enable-source-maps examples/readme.js  0.14s user 0.05s system 54% cpu 0.350 total

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Jul 28, 2023

Using main branch against the Python web server, it seems to hang forever (no bad request response logged), so things have improved with this change (against Puma, main and this branch behaves the same).

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Aug 8, 2023

Let's merge this, it is an improvement for sure.

@dentarg dentarg merged commit 7a59414 into main Aug 8, 2023
@dentarg dentarg deleted the connect-to-non-amqp-server-should-fail branch August 8, 2023 07:45
@dentarg dentarg mentioned this pull request Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connecting to non AMQP server doesn't fail

2 participants