2 * We use a very basic strategy to use TCP_CORK semantics optimally
3 * in most TCP servers: On corked sockets, we will uncork on recv()
4 * if there was a previous send(). Otherwise we do not fiddle
5 * with TCP_CORK at all.
7 * Under Linux, we can rely on TCP_CORK being inherited in an
8 * accept()-ed client socket so we can avoid syscalls for each
9 * accept()-ed client if we know the accept() socket corks.
11 * This module does NOTHING for client TCP sockets, we only deal
12 * with accept()-ed sockets right now.
16 #include "my_fileno.h"
17 #include <netinet/tcp.h>
20 * As of FreeBSD 4.5, TCP_NOPUSH == TCP_CORK
21 * ref: http://dotat.at/writing/nopush.html
22 * We won't care for older FreeBSD since nobody runs Ruby on them...
25 # define KGIO_NOPUSH TCP_CORK
26 #elif defined(TCP_NOPUSH)
27 # define KGIO_NOPUSH TCP_NOPUSH
31 static ID id_autopush_state
;
32 static int enabled
= 1;
35 AUTOPUSH_STATE_ACCEPTOR_IGNORE
= -1,
36 AUTOPUSH_STATE_IGNORE
= 0,
37 AUTOPUSH_STATE_WRITER
= 1,
38 AUTOPUSH_STATE_WRITTEN
= 2,
39 AUTOPUSH_STATE_ACCEPTOR
= 3
42 static enum autopush_state
state_get(VALUE io
)
46 if (rb_ivar_defined(io
, id_autopush_state
) == Qfalse
)
47 return AUTOPUSH_STATE_IGNORE
;
48 val
= rb_ivar_get(io
, id_autopush_state
);
50 return (enum autopush_state
)NUM2INT(val
);
53 static void state_set(VALUE io
, enum autopush_state state
)
55 rb_ivar_set(io
, id_autopush_state
, INT2NUM(state
));
58 static enum autopush_state
detect_acceptor_state(VALUE io
);
59 static void push_pending_data(VALUE io
);
63 * Kgio.autopush? -> true or false
65 * Returns whether or not autopush is enabled.
67 * Only available on systems with TCP_CORK (Linux) or
68 * TCP_NOPUSH (FreeBSD, and maybe other *BSDs).
70 static VALUE
s_get_autopush(VALUE self
)
72 return enabled
? Qtrue
: Qfalse
;
77 * Kgio.autopush = true
78 * Kgio.autopush = false
80 * Enables or disables autopush for sockets created with kgio_accept
81 * and kgio_tryaccept methods. Autopush relies on TCP_CORK/TCP_NOPUSH
82 * being enabled on the listen socket.
84 * Only available on systems with TCP_CORK (Linux) or
85 * TCP_NOPUSH (FreeBSD, and maybe other *BSDs).
87 * Please do not use this (or kgio at all) in new code. Under Linux,
88 * use MSG_MORE, instead, as it requires fewer syscalls. Users of
89 * other systems are encouraged to add MSG_MORE support to their
92 static VALUE
s_set_autopush(VALUE self
, VALUE val
)
102 * io.kgio_autopush? -> true or false
104 * Returns the current autopush state of the Kgio::SocketMethods-enabled
107 * Only available on systems with TCP_CORK (Linux) or
108 * TCP_NOPUSH (FreeBSD, and maybe other *BSDs).
110 static VALUE
autopush_get(VALUE io
)
112 return state_get(io
) <= 0 ? Qfalse
: Qtrue
;
118 * io.kgio_autopush = true
119 * io.kgio_autopush = false
121 * Enables or disables autopush on any given Kgio::SocketMethods-capable
122 * IO object. This does NOT enable or disable TCP_NOPUSH/TCP_CORK right
123 * away, that must be done with IO.setsockopt
125 * Only available on systems with TCP_CORK (Linux) or
126 * TCP_NOPUSH (FreeBSD, and maybe other *BSDs).
128 static VALUE
autopush_set(VALUE io
, VALUE vbool
)
131 state_set(io
, AUTOPUSH_STATE_WRITER
);
133 state_set(io
, AUTOPUSH_STATE_IGNORE
);
137 void init_kgio_autopush(void)
139 VALUE mKgio
= rb_define_module("Kgio");
142 rb_define_singleton_method(mKgio
, "autopush?", s_get_autopush
, 0);
143 rb_define_singleton_method(mKgio
, "autopush=", s_set_autopush
, 1);
145 tmp
= rb_define_module_under(mKgio
, "SocketMethods");
146 rb_define_method(tmp
, "kgio_autopush=", autopush_set
, 1);
147 rb_define_method(tmp
, "kgio_autopush?", autopush_get
, 0);
149 id_autopush_state
= rb_intern("@kgio_autopush_state");
153 * called after a successful write, just mark that we've put something
154 * in the skb and will need to uncork on the next write.
156 void kgio_autopush_send(VALUE io
)
158 if (state_get(io
) == AUTOPUSH_STATE_WRITER
)
159 state_set(io
, AUTOPUSH_STATE_WRITTEN
);
162 /* called on successful accept() */
163 void kgio_autopush_accept(VALUE accept_io
, VALUE client_io
)
165 enum autopush_state acceptor_state
;
169 acceptor_state
= state_get(accept_io
);
170 if (acceptor_state
== AUTOPUSH_STATE_IGNORE
)
171 acceptor_state
= detect_acceptor_state(accept_io
);
172 if (acceptor_state
== AUTOPUSH_STATE_ACCEPTOR
)
173 state_set(client_io
, AUTOPUSH_STATE_WRITER
);
175 state_set(client_io
, AUTOPUSH_STATE_IGNORE
);
178 void kgio_autopush_recv(VALUE io
)
180 if (enabled
&& (state_get(io
) == AUTOPUSH_STATE_WRITTEN
)) {
181 push_pending_data(io
);
182 state_set(io
, AUTOPUSH_STATE_WRITER
);
186 static enum autopush_state
detect_acceptor_state(VALUE io
)
189 int fd
= my_fileno(io
);
190 socklen_t optlen
= sizeof(int);
191 enum autopush_state state
;
193 if (getsockopt(fd
, IPPROTO_TCP
, KGIO_NOPUSH
, &corked
, &optlen
) != 0) {
194 if (errno
!= EOPNOTSUPP
)
195 rb_sys_fail("getsockopt(TCP_CORK/TCP_NOPUSH)");
197 state
= AUTOPUSH_STATE_ACCEPTOR_IGNORE
;
199 state
= AUTOPUSH_STATE_ACCEPTOR
;
201 state
= AUTOPUSH_STATE_ACCEPTOR_IGNORE
;
203 state_set(io
, state
);
209 * checks to see if we've written anything since the last recv()
210 * If we have, uncork the socket and immediately recork it.
212 static void push_pending_data(VALUE io
)
215 const socklen_t optlen
= sizeof(int);
216 const int fd
= my_fileno(io
);
218 if (setsockopt(fd
, IPPROTO_TCP
, KGIO_NOPUSH
, &optval
, optlen
) != 0)
219 rb_sys_fail("setsockopt(TCP_CORK/TCP_NOPUSH, 0)");
220 /* immediately recork */
222 if (setsockopt(fd
, IPPROTO_TCP
, KGIO_NOPUSH
, &optval
, optlen
) != 0)
223 rb_sys_fail("setsockopt(TCP_CORK/TCP_NOPUSH, 1)");
225 #else /* !KGIO_NOPUSH */
226 void kgio_autopush_recv(VALUE io
){}
227 void kgio_autopush_send(VALUE io
){}
228 void init_kgio_autopush(void)
231 #endif /* ! KGIO_NOPUSH */