raindrops RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Jean Boussier <jean.boussier@gmail.com>
To: raindrops-public@yhbt.net
Subject: [PATCH] tcp_listener_stats: always eagerly close sockets
Date: Sat, 23 Sep 2023 11:56:31 +0200	[thread overview]
Message-ID: <6E0E349D-A7CE-4B88-8F89-66438BB775A1@gmail.com> (raw)


Once again apologies for not submitting the patch in an usable format. It can be downloaded with curl/wget from https://github.com/casperisfine/raindrops/commit/1c92b440ad7b11a1708a1d5ed75b0767f213b40a.patch

I just debugged an issue with our system, I was witnessing the
number of file descriptor in our process grow at an alarming rate
which I mapped to our use of raindrops to report utilisation.

For various reasons we don’t call raindrops from a Rack middleware
but have one process that monitor the socket continuously, and
share that data with the workers.

Since we call tcp_listener_stats every seconds in a process
that doesn't do much else, GC very rarely triggers if at all
 which cause `InetDiagSocket` instances to accumulate very

Each of those instances holds a file descriptor.

Looking at the raindrops implementation it seems to assume
the GC will take care of regularly closing these sockets, but
I think it’s a bit too bold of an assumption.


 ext/raindrops/linux_inet_diag.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c
index 2a2360c..b3d9a51 100644
--- a/ext/raindrops/linux_inet_diag.c
+++ b/ext/raindrops/linux_inet_diag.c
@@ -634,7 +634,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 	switch (TYPE(addrs)) {
 	case T_STRING:
 		rb_hash_aset(rv, addrs, tcp_stats(&args, addrs));
-		return rv;
+		goto out;
 	case T_ARRAY: {
 		long i;
 		long len = RARRAY_LEN(addrs);
@@ -643,7 +643,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 			VALUE cur = rb_ary_entry(addrs, 0);
 			rb_hash_aset(rv, cur, tcp_stats(&args, cur));
-			return rv;
+			goto out;
 		for (i = 0; i < len; i++) {
 			union any_addr check;
@@ -659,6 +659,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
+        rb_io_close(sock);
 		         "addr must be an array of strings, a string, or nil");
@@ -671,6 +672,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self)
 	if (RHASH_SIZE(rv) > 1)
 		rb_hash_foreach(rv, drop_placeholders, Qfalse);
 	/* let GC deal with corner cases */
 	if (argc < 2) rb_io_close(sock);
 	return rv;

             reply	other threads:[~2023-09-23  9:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23  9:56 Jean Boussier [this message]
2023-09-26 21:40 ` [PATCH] tcp_listener_stats: always eagerly close sockets Eric Wong
2023-09-30 23:13   ` [PATCH] middleware: reuse inet_diag netlink socket Eric Wong
2023-09-30 23:35     ` [squash] make reusing inet_diag sock fork+preload safe Eric Wong
2023-12-29 17:44       ` [PATCH v3] middleware: reuse inet_diag netlink socket Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: http://yhbt.net/raindrops/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6E0E349D-A7CE-4B88-8F89-66438BB775A1@gmail.com \
    --to=jean.boussier@gmail.com \
    --cc=raindrops-public@yhbt.net \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).