fluent / fluent-logger-node

A structured logger for Fluentd (Node.js)
Apache License 2.0
259 stars 83 forks source link

Timeout doesn't close socket connection #139

Open KrysKruk opened 5 years ago

KrysKruk commented 5 years ago

Hi guys!

I was struggling some time on automatically closing the fluent socket when idling. I wanted to emit some logs and then let the process to exit. That never happened, because the internal fluent socket was still open. I could just call the end() function, but that was not an option for me.

After studying Node.js socket documentation I found out that calling socket.setTimeout doesn't really close the connection, only emits a timeout event which then needs to be handled by the developer.

That was of course my lack of knowledge, but I'm thinking how fluent-logger-node could be improved here.

What do you guys think of emitting the timeout event on the FluentSender and let developer call end() method later?

The other option would be adding e.g. autoClose or closeOnTimeout boolean flags to the FluentSender constructor.

Also I figured that in tls mode, the given timeout is not set on the socket (even { tlsOptions: { timeout: 3 } } doesn't work).

For now I use a workaround:

fluent.on('connect', () => {
  fluent._socket.setTimeout(1000)
  fluent._socket.on('timeout', () => {
    fluent.end()
  })
})

Thanks for feedback. Sorry if I misunderstood the problem and there is some other way to handle that.

okkez commented 5 years ago

Could you show me your code?

The behavior that I expected:

// This will close the logger and its internal socket as soon as possible.
logger.end();

// This will close the logger and its internal socket after send the last message.
logger.end("end", "The last message");