Module: Iodine
- Defined in:
- lib/iodine.rb,
lib/iodine/json.rb,
lib/iodine/pubsub.rb,
lib/iodine/version.rb,
lib/iodine/mustache.rb,
lib/iodine/connection.rb,
lib/iodine/rack_utils.rb,
lib/rack/handler/iodine.rb,
ext/iodine/iodine.c
Overview
Iodine is an HTTP / WebSocket server as well as an Evented Network Tool Library. In essense, Iodine is a Ruby port for the [facil.io](facil.io) C library.
Here is a simple telnet based echo server using Iodine (see full list at Connection):
require 'iodine'
# define the protocol for our service
module EchoProtocol
def on_open(client)
# Set a connection timeout
client.timeout = 10
# Write a welcome message
client.write "Echo server running on Iodine #{Iodine::VERSION}.\r\n"
end
# this is called for incoming data - note data might be fragmented.
def (client, data)
# write the data we received
client.write "echo: #{data}"
# close the connection when the time comes
client.close if data =~ /^bye[\n\r]/
end
# called if the connection is still open and the server is shutting down.
def on_shutdown(client)
# write the data we received
client.write "Server going away\r\n"
end
extend self
end
# create the service instance, the block returns a connection handler.
Iodine.listen(port: "3000") { EchoProtocol }
# start the service
Iodine.threads = 1
Iodine.start
Methods for setting up and starting Iodine include Iodine.start, Iodine.threads, Iodine.threads=, Iodine.workers and Iodine.workers=.
Methods for setting startup / operational callbacks include Iodine.on_idle, Iodine.on_state.
Methods for asynchronous execution include Iodine.run (same as Iodine.defer), Iodine.run_after and Iodine.run_every.
Methods for application wide pub/sub include Iodine.subscribe, Iodine.unsubscribe and Iodine.publish. Connection specific pub/sub methods are documented in the Connection class).
Methods for TCP/IP and Unix Sockets connections include Iodine.listen and Iodine.connect.
Methods for HTTP connections include Iodine.listen2http.
Note that the HTTP server supports both TCP/IP and Unix Sockets as well as SSE / WebSockets extensions.
Iodine doesn’t call Iodine.patch_rack automatically, but doing so will improve Rack’s performace.
Please read the README file for an introduction to Iodine.
Defined Under Namespace
Modules: Base, JSON, PubSub, Rack Classes: Connection, Mustache
Constant Summary collapse
- VERSION =
'0.7.16'.freeze
Class Method Summary collapse
-
.after_fork(&block) ⇒ Object
deprecated
Deprecated.
use Iodine.on_state.
-
.after_fork_in_master(&block) ⇒ Object
deprecated
Deprecated.
use Iodine.on_state.
-
.after_fork_in_worker(&block) ⇒ Object
deprecated
Deprecated.
use Iodine.on_state.
-
.attach_fd(fd, handler) ⇒ Object
The Iodine.attach_fd method instructs iodine to attach a socket to the server using it’s numerical file descriptor.
-
.before_fork(&block) ⇒ Object
deprecated
Deprecated.
use Iodine.on_state.
-
.connect(args) ⇒ Object
The Iodine.connect method instructs iodine to connect to a server using either TCP/IP or Unix sockets.
-
.defer ⇒ Object
Runs a block of code asyncronously (adds the code to the event queue).
-
.listen(args) ⇒ Object
The Iodine.listen method instructs iodine to listen to incoming connections using either TCP/IP or Unix sockets.
-
.listen2http(opt) ⇒ Object
Listens to incoming HTTP connections and handles incoming requests using the Rack specification.
-
.master? ⇒ Boolean
Returns ‘true` if this process is the master / root process, `false` otherwise.
-
.on_idle ⇒ Object
Schedules a single occuring event for the next idle cycle.
-
.on_shutdown(&block) ⇒ Object
deprecated
Deprecated.
use Iodine.on_state.
-
.on_state(event) ⇒ Object
Sets a block of code to run when Iodine’s core state is updated.
-
.patch_rack ⇒ Object
Will monkey patch some Rack methods to increase their performance.
-
.publish(*args) ⇒ Object
Publishes a message to a channel.
-
.run ⇒ Object
Runs a block of code asyncronously (adds the code to the event queue).
-
.run_after(milliseconds) ⇒ Object
Runs the required block after the specified number of milliseconds have passed.
-
.run_every(*args) ⇒ Object
Runs the required block after the specified number of milliseconds have passed.
-
.start ⇒ Object
This will block the calling (main) thread and start the Iodine reactor.
-
.stop ⇒ Object
This will stop the iodine server, shutting it down.
-
.subscribe(*args) ⇒ Object
Subscribes to a Pub/Sub stream / channel or replaces an existing subscription.
-
.threads ⇒ Object
Returns the number of worker threads that will be used when Iodine.start is called.
-
.threads=(val) ⇒ Object
Sets the number of worker threads that will be used when Iodine.start is called.
-
.unsubscribe(name) ⇒ Object
Unsubscribes from a Pub/Sub stream / channel.
-
.verbosity ⇒ Object
Gets the logging level used for Iodine messages.
-
.verbosity=(val) ⇒ Object
Gets the logging level used for Iodine messages.
-
.worker? ⇒ Boolean
Returns ‘true` if this process is a worker process or if iodine is running in a single process mode (the master is also a worker), `false` otherwise.
-
.workers ⇒ Object
Returns the number of worker processes that will be used when Iodine.start is called.
-
.workers=(val) ⇒ Object
Sets the number of worker processes that will be used when Iodine.start is called.
Class Method Details
.after_fork(&block) ⇒ Object
use on_state.
Sets a block of code to run after a new worker process is forked (cluster mode only).
Code runs in both the parent and the child.
91 92 93 94 |
# File 'lib/iodine.rb', line 91 def self.after_fork(&block) warn "Iodine.after_fork is deprecated, use Iodine.on_state(:after_fork)." Iodine.on_state(:after_fork, &block) end |
.after_fork_in_master(&block) ⇒ Object
use on_state.
Sets a block of code to run in the master / root process, after a new worker process is forked (cluster mode only).
105 106 107 108 |
# File 'lib/iodine.rb', line 105 def self.after_fork_in_master(&block) warn "Iodine.after_fork_in_master is deprecated, use Iodine.on_state(:enter_master)." Iodine.on_state(:enter_master, &block) end |
.after_fork_in_worker(&block) ⇒ Object
use on_state.
Sets a block of code to run in the worker process, after a new worker process is forked (cluster mode only).
98 99 100 101 |
# File 'lib/iodine.rb', line 98 def self.after_fork_in_worker(&block) warn "Iodine.after_fork_in_worker is deprecated, use Iodine.on_state(:enter_child)." Iodine.on_state(:enter_child, &block) end |
.attach_fd(fd, handler) ⇒ Object
The attach_fd method instructs iodine to attach a socket to the server using it’s numerical file descriptor.
This is faster than attaching a Ruby IO object since it allows iodine to directly call the system’s read/write methods. However, this doesn’t support TLS/SSL connections.
This method requires two objects, a file descriptor (‘fd`) and a callback object.
See listen for details about the callback object.
Returns the callback object (handler) used.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'ext/iodine/iodine_tcp.c', line 329
static VALUE iodine_tcp_attach_fd(VALUE self, VALUE fd, VALUE handler) {
// clang-format on
Check_Type(fd, T_FIXNUM);
if (handler == Qnil || handler == Qfalse || handler == Qtrue) {
rb_raise(rb_eArgError, "A callback object must be provided.");
}
IodineStore.add(handler);
int other = dup(NUM2INT(fd));
if (other == -1) {
rb_raise(rb_eIOError, "invalid fd.");
}
intptr_t uuid = fio_fd2uuid(other);
iodine_tcp_attch_uuid(uuid, handler);
IodineStore.remove(handler);
return handler;
(void)self;
}
|
.before_fork(&block) ⇒ Object
use on_state.
Sets a block of code to run before a new worker process is forked (cluster mode only).
82 83 84 85 |
# File 'lib/iodine.rb', line 82 def self.before_fork(&block) warn "Iodine.before_fork is deprecated, use Iodine.on_state(:before_fork)." Iodine.on_state(:before_fork, &block) end |
.connect(args) ⇒ Object
The connect method instructs iodine to connect to a server using either TCP/IP or Unix sockets.
The method accepts a single Hash argument with the following optional keys:
- :port
-
The port to listen to, deafults to 0 (using a Unix socket)
- :address
-
The address to listen to, which could be a Unix Socket path as well as an IPv4 / IPv6 address. Deafults to 0.0.0.0 (or the IPv6 equivelant).
- :handler
-
A connection callback object that supports the following same callbacks listen in the listen method’s documentation.
- :timeout
-
An integer timeout for connection establishment (doen’t effect the new connection’s timeout. Should be in the rand of 0..255.
The method also accepts an optional block.
Either a block or the :handler key MUST be present.
If the connection fails, only the ‘on_close` callback will be called (with a `nil` client).
Returns the handler object used.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'ext/iodine/iodine_tcp.c', line 276
static VALUE iodine_tcp_connect(VALUE self, VALUE args) {
// clang-format on
Check_Type(args, T_HASH);
VALUE rb_port = rb_hash_aref(args, port_id);
VALUE rb_address = rb_hash_aref(args, address_id);
VALUE rb_handler = rb_hash_aref(args, handler_id);
VALUE rb_timeout = rb_hash_aref(args, timeout_id);
uint8_t timeout = 0;
fio_str_s port = FIO_STR_INIT;
if (rb_handler == Qnil || rb_handler == Qfalse || rb_handler == Qtrue) {
rb_raise(rb_eArgError, "A callback object (:handler) must be provided.");
}
IodineStore.add(rb_handler);
if (rb_address != Qnil) {
Check_Type(rb_address, T_STRING);
}
if (rb_port != Qnil) {
if (rb_port == Qfalse) {
fio_str_write_i(&port, 0);
} else if (RB_TYPE_P(rb_port, T_STRING))
fio_str_write(&port, RSTRING_PTR(rb_port), RSTRING_LEN(rb_port));
else if (RB_TYPE_P(rb_port, T_FIXNUM))
fio_str_write_i(&port, FIX2LONG(rb_port));
else
rb_raise(rb_eTypeError,
"The `port` property MUST be either a String or a Number");
}
if (rb_timeout != Qnil) {
Check_Type(rb_timeout, T_FIXNUM);
timeout = NUM2USHORT(rb_timeout);
}
fio_connect(.port = fio_str_info(&port).data,
.address =
(rb_address == Qnil ? NULL : StringValueCStr(rb_address)),
.on_connect = iodine_tcp_on_connect,
.on_fail = iodine_tcp_on_fail, .timeout = timeout,
.udata = (void *)rb_handler);
return rb_handler;
(void)self;
}
|
.defer ⇒ Object
Runs a block of code asyncronously (adds the code to the event queue).
Always returns the block of code to executed (Proc object).
Code will be executed only while Iodine is running (after start).
Code blocks that where scheduled to run before Iodine enters cluster mode will run on all child processes.
201 202 203 204 205 206 207 |
# File 'ext/iodine/iodine_defer.c', line 201
static VALUE iodine_defer_run(VALUE self) {
rb_need_block();
VALUE block = IodineStore.add(rb_block_proc());
fio_defer(iodine_defer_performe_once, (void *)block, NULL);
return block;
(void)self;
}
|
.listen(args) ⇒ Object
The listen method instructs iodine to listen to incoming connections using either TCP/IP or Unix sockets.
The method accepts a single Hash argument with the following optional keys:
- :port
-
The port to listen to, deafults to nil (using a Unix socket)
- :address
-
The address to listen to, which could be a Unix Socket path as well as an IPv4 / IPv6 address. Deafults to 0.0.0.0 (or the IPv6 equivelant).
- :handler
-
An object that answers the ‘call` method (i.e., a Proc).
The method also accepts an optional block.
Either a block or the :handler key MUST be present.
The handler Proc (or object) should return a connection callback object that supports the following callbacks (see also Connection):
- on_open(client)
-
called after a connection was established
- on_message(client, data)
-
called when incoming data is available. Data may be fragmented.
- on_drained(client)
-
called when all the pending ‘client.write` events have been processed (see Iodine::Connection#pending).
- ping(client)
-
called whenever a timeout has occured (see Iodine::Connection#timeout=).
- on_shutdown(client)
-
called if the server is shutting down. This is called before the connection is closed.
- on_close(client)
-
called when the connection with the client was closed.
The ‘client` argument is an Connection instance that represents the connection / the client.
Here’s a telnet based chat-room example:
require 'iodine'
# define the protocol for our service
module ChatHandler
def self.on_open(client)
# Set a connection timeout
client.timeout = 10
# subscribe to the chat channel.
client.subscribe :chat
# Write a welcome message
client.publish :chat, "new member entered the chat\r\n"
end
# this is called for incoming data - note data might be fragmented.
def self.(client, data)
# publish the data we received
client.publish :chat, data
# close the connection when the time comes
client.close if data =~ /^bye[\n\r]/
end
# called whenever timeout occurs.
def self.ping(client)
client.write "System: quite, isn't it...?\r\n"
end
# called if the connection is still open and the server is shutting down.
def self.on_shutdown(client)
# write the data we received
client.write "Chat server going away. Try again later.\r\n"
end
# returns the callback object (self).
def self.call
self
end
end
# we can bothe the `handler` keuword or a block, anything that answers #call.
Iodine.listen(port: "3000", handler: ChatHandler)
# start the service
Iodine.threads = 1
Iodine.start
Returns the handler object used.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'ext/iodine/iodine_tcp.c', line 216
static VALUE iodine_tcp_listen(VALUE self, VALUE args) {
// clang-format on
Check_Type(args, T_HASH);
VALUE rb_port = rb_hash_aref(args, port_id);
VALUE rb_address = rb_hash_aref(args, address_id);
VALUE rb_handler = rb_hash_aref(args, handler_id);
fio_str_s port = FIO_STR_INIT;
if (rb_handler == Qnil || rb_handler == Qfalse || rb_handler == Qtrue) {
rb_need_block();
rb_handler = rb_block_proc();
}
IodineStore.add(rb_handler);
if (rb_address != Qnil) {
Check_Type(rb_address, T_STRING);
}
if (rb_port != Qnil) {
if (rb_port == Qfalse) {
fio_str_write_i(&port, 0);
} else if (RB_TYPE_P(rb_port, T_STRING))
fio_str_write(&port, RSTRING_PTR(rb_port), RSTRING_LEN(rb_port));
else if (RB_TYPE_P(rb_port, T_FIXNUM))
fio_str_write_i(&port, FIX2LONG(rb_port));
else
rb_raise(rb_eTypeError,
"The `port` property MUST be either a String or a Number");
}
if (fio_listen(.port = fio_str_info(&port).data,
.address =
(rb_address == Qnil ? NULL : StringValueCStr(rb_address)),
.on_open = iodine_tcp_on_open,
.on_finish = iodine_tcp_on_finish,
.udata = (void *)rb_handler) == -1) {
IodineStore.remove(rb_handler);
rb_raise(rb_eRuntimeError,
"failed to listen to requested address, unknown error.");
}
return rb_handler;
(void)self;
}
|
.listen2http(opt) ⇒ Object
Listens to incoming HTTP connections and handles incoming requests using the Rack specification.
This is delegated to a lower level C HTTP and Websocket implementation, no Ruby object will be crated except the ‘env` object required by the Rack specifications.
Accepts a single Hash argument with the following properties:
(it’s possible to set default values using the DEFAULT_HTTP_ARGS Hash)
- app
-
the Rack application that handles incoming requests. Default: ‘nil`.
- port
-
the port to listen to. Default: 3000.
- address
-
the address to bind to. Default: binds to all possible addresses.
- log
-
enable response logging (Hijacked sockets aren’t logged). Default: off.
- public
-
The root public folder for static file service. Default: none.
- timeout
-
Timeout for inactive HTTP/1.x connections. Defaults: 40 seconds.
- max_body
-
The maximum body size for incoming HTTP messages in bytes. Default: ~50Mib.
- max_headers
-
The maximum total header length for incoming HTTP messages. Default: ~64Kib.
- max_msg
-
The maximum Websocket message size allowed. Default: ~250Kib.
- ping
-
The Websocket ‘ping` interval. Default: 40 seconds.
Either the ‘app` or the `public` properties are required. If niether exists, the function will fail. If both exist, Iodine will serve static files as well as dynamic requests.
When using the static file server, it’s possible to serve ‘gzip` versions of the static files by saving a compressed version with the `gz` extension (i.e. `styles.css.gz`).
‘gzip` will only be served to clients tat support the `gzip` transfer encoding.
Once HTTP/2 is supported (planned, but probably very far away), HTTP/2 timeouts will be dynamically managed by Iodine. The ‘timeout` option is only relevant to HTTP/1.x connections.
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'ext/iodine/iodine_http.c', line 826
static VALUE iodine_http_listen(VALUE self, VALUE opt) {
// clang-format on
uint8_t log_http = 0;
size_t ping = 0;
size_t max_body = 0;
size_t max_headers = 0;
size_t max_msg = 0;
Check_Type(opt, T_HASH);
/* copy from deafult hash */
/* test arguments */
VALUE app = rb_hash_aref(opt, ID2SYM(rb_intern("app")));
VALUE www = rb_hash_aref(opt, ID2SYM(rb_intern("public")));
VALUE port = rb_hash_aref(opt, ID2SYM(rb_intern("port")));
VALUE address = rb_hash_aref(opt, ID2SYM(rb_intern("address")));
VALUE tout = rb_hash_aref(opt, ID2SYM(rb_intern("timeout")));
if (www == Qnil) {
www = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("public")));
}
if (port == Qnil) {
port = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("port")));
}
if (address == Qnil) {
address = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("address")));
}
if (tout == Qnil) {
tout = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("timeout")));
}
VALUE tmp = rb_hash_aref(opt, ID2SYM(rb_intern("max_msg")));
if (tmp == Qnil) {
tmp = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("max_msg")));
}
if (tmp != Qnil && tmp != Qfalse) {
Check_Type(tmp, T_FIXNUM);
max_msg = FIX2ULONG(tmp);
}
tmp = rb_hash_aref(opt, ID2SYM(rb_intern("max_body")));
if (tmp == Qnil) {
tmp = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("max_body")));
}
if (tmp != Qnil && tmp != Qfalse) {
Check_Type(tmp, T_FIXNUM);
max_body = FIX2ULONG(tmp);
}
tmp = rb_hash_aref(opt, ID2SYM(rb_intern("max_headers")));
if (tmp == Qnil) {
tmp = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("max_headers")));
}
if (tmp != Qnil && tmp != Qfalse) {
Check_Type(tmp, T_FIXNUM);
max_headers = FIX2ULONG(tmp);
}
tmp = rb_hash_aref(opt, ID2SYM(rb_intern("ping")));
if (tmp == Qnil) {
tmp = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("ping")));
}
if (tmp != Qnil && tmp != Qfalse) {
Check_Type(tmp, T_FIXNUM);
ping = FIX2ULONG(tmp);
}
if (ping > 255) {
fprintf(stderr, "Iodine Warning: Websocket timeout value "
"is over 255 and will be ignored.\n");
ping = 0;
}
tmp = rb_hash_aref(opt, ID2SYM(rb_intern("log")));
if (tmp == Qnil) {
tmp = rb_hash_aref(iodine_default_args, ID2SYM(rb_intern("log")));
}
if (tmp != Qnil && tmp != Qfalse)
log_http = 1;
if ((app == Qnil || app == Qfalse) && (www == Qnil || www == Qfalse)) {
fprintf(stderr, "Iodine Warning: HTTP without application or public folder "
"(ignored).\n");
return Qfalse;
}
if ((www != Qnil && www != Qfalse)) {
Check_Type(www, T_STRING);
IodineStore.add(www);
rb_hash_aset(env_template_no_upgrade, XSENDFILE_TYPE, XSENDFILE);
rb_hash_aset(env_template_no_upgrade, XSENDFILE_TYPE_HEADER, XSENDFILE);
support_xsendfile = 1;
} else
www = 0;
if ((address != Qnil && address != Qfalse))
Check_Type(address, T_STRING);
else
address = 0;
if ((tout != Qnil && tout != Qfalse)) {
Check_Type(tout, T_FIXNUM);
tout = FIX2ULONG(tout);
} else
tout = 0;
if (tout > 255) {
fprintf(stderr, "Iodine Warning: HTTP timeout value "
"is over 255 and is silently ignored.\n");
tout = 0;
}
if (port != Qnil && port != Qfalse) {
if (!RB_TYPE_P(port, T_STRING) && !RB_TYPE_P(port, T_FIXNUM))
rb_raise(rb_eTypeError,
"The `port` property MUST be either a String or a Number");
if (RB_TYPE_P(port, T_FIXNUM))
port = rb_funcall2(port, iodine_to_s_method_id, 0, NULL);
IodineStore.add(port);
} else if (port == Qfalse)
port = 0;
else if (address &&
(StringValueCStr(address)[0] > '9' ||
StringValueCStr(address)[0] < '0') &&
StringValueCStr(address)[0] != ':' &&
(RSTRING_LEN(address) < 3 || StringValueCStr(address)[2] != ':')) {
/* address is likely a Unix domain socket address, not an IP address... */
port = Qnil;
} else {
port = rb_str_new("3000", 4);
IodineStore.add(port);
}
if ((app != Qnil && app != Qfalse))
IodineStore.add(app);
else
app = 0;
if (http_listen((port ? StringValueCStr(port) : NULL),
(address ? StringValueCStr(address) : NULL),
.on_request = on_rack_request, .on_upgrade = on_rack_upgrade,
.udata = (void *)app,
.timeout = (tout ? FIX2INT(tout) : tout), .ws_timeout = ping,
.ws_max_msg_size = max_msg, .max_header_size = max_headers,
.on_finish = free_iodine_http, .log = log_http,
.max_body_size = max_body,
.public_folder = (www ? StringValueCStr(www) : NULL)) == -1) {
FIO_LOG_ERROR("Failed to initialize a listening HTTP socket for port %s",
port ? StringValueCStr(port) : "3000");
rb_raise(rb_eRuntimeError, "Listening socket initialization failed");
return Qfalse;
}
if ((app == Qnil || app == Qfalse)) {
FIO_LOG_WARNING(
"(listen2http) no app, the HTTP service on port %s will only serve "
"static files.",
(port ? StringValueCStr(port) : "3000"));
}
if (www) {
FIO_LOG_INFO("Serving static files from %s", StringValueCStr(www));
}
return Qtrue;
(void)self;
}
|
.master? ⇒ Boolean
Returns ‘true` if this process is the master / root process, `false` otherwise.
Note that the master process might be a worker process as well, when running in single process mode (see workers).
266 267 268 |
# File 'ext/iodine/iodine.c', line 266 static VALUE iodine_master_is(VALUE self) { return fio_is_master() ? Qtrue : Qfalse; } |
.on_idle ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'ext/iodine/iodine.c', line 57
static VALUE iodine_sched_on_idle(VALUE self) {
// clang-format on
rb_need_block();
VALUE block = rb_block_proc();
IodineStore.add(block);
fio_state_callback_add(FIO_CALL_ON_IDLE, iodine_perform_on_idle_callback,
(void *)block);
return block;
(void)self;
}
|
.on_shutdown(&block) ⇒ Object
use on_state.
Sets a block of code to run once a Worker process shuts down (both in single process mode and cluster mode).
112 113 114 115 |
# File 'lib/iodine.rb', line 112 def self.on_shutdown(&block) warn "Iodine.on_shutdown is deprecated, use Iodine.on_state(:on_finish)." Iodine.on_state(:on_finish, &block) end |
.on_state(event) ⇒ Object
Sets a block of code to run when Iodine’s core state is updated.
The state event Symbol can be any of the following:
- :pre_start
-
the block will be called once before starting up the IO reactor.
- :before_fork
-
the block will be called before each time the IO reactor forks a new worker.
- :after_fork
-
the block will be called after each fork (both in parent and workers).
- :enter_child
-
the block will be called by a worker process right after forking.
- :enter_master
-
the block will be called by the master process after spawning a worker (after forking).
- :on_start
-
the block will be called every time a worker proceess starts. In single process mode, the master process is also a worker.
- :on_parent_crush
-
the block will be called by each worker the moment it detects the master process crashed.
- :on_child_crush
-
the block will be called by the parent (master) after a worker process crashed.
- :start_shutdown
-
the block will be called before starting the shutdown sequence.
- :on_finish
-
the block will be called just before finishing up (both on chlid and parent processes).
Code runs in both the parent and the child.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'ext/iodine/iodine_defer.c', line 320
static VALUE iodine_on_state(VALUE self, VALUE event) {
// clang-format on
rb_need_block();
Check_Type(event, T_SYMBOL);
VALUE block = rb_block_proc();
IodineStore.add(block);
ID state = rb_sym2id(event);
if (state == STATE_PRE_START) {
fio_state_callback_add(FIO_CALL_PRE_START,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_BEFORE_FORK) {
fio_state_callback_add(FIO_CALL_BEFORE_FORK,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_AFTER_FORK) {
fio_state_callback_add(FIO_CALL_AFTER_FORK,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ENTER_CHILD) {
fio_state_callback_add(FIO_CALL_IN_CHILD,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ENTER_MASTER) {
fio_state_callback_add(FIO_CALL_IN_MASTER,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ON_START) {
fio_state_callback_add(FIO_CALL_ON_START,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ON_PARENT_CRUSH) {
fio_state_callback_add(FIO_CALL_ON_PARENT_CRUSH,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ON_CHILD_CRUSH) {
fio_state_callback_add(FIO_CALL_ON_CHILD_CRUSH,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_START_SHUTDOWN) {
fio_state_callback_add(FIO_CALL_ON_SHUTDOWN,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == STATE_ON_FINISH) {
fio_state_callback_add(FIO_CALL_ON_FINISH,
iodine_perform_state_callback_persist,
(void *)block);
} else {
IodineStore.remove(block);
rb_raise(rb_eTypeError, "unknown event in Iodine.on_state");
}
return block;
(void)self;
}
|
.patch_rack ⇒ Object
Will monkey patch some Rack methods to increase their performance.
This is recommended, see Iodine::Rack::Utils for details.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/iodine.rb', line 65 def self.patch_rack begin require 'rack' rescue LoadError end ::Rack::Utils.class_eval do Iodine::Base::MonkeyPatch::RackUtils.methods(false).each do |m| ::Rack::Utils.define_singleton_method(m, Iodine::Base::MonkeyPatch::RackUtils.instance_method(m) ) end end end |
.publish(*args) ⇒ Object
Publishes a message to a channel.
Can be used using two Strings:
publish(to, )
The method accepts an optional ‘engine` argument:
publish(to, , my_pubsub_engine)
Alternatively, accepts the following named arguments:
- :to
-
The channel to publish to (required).
- :message
-
The message to be published (required).
- :engine
-
If provided, the engine to use for pub/sub. Otherwise the default engine is used.
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'ext/iodine/iodine_connection.c', line 686
static VALUE iodine_pubsub_publish(int argc, VALUE *argv, VALUE self) {
// clang-format on
VALUE rb_ch, rb_msg, rb_engine = Qnil;
const fio_pubsub_engine_s *engine = NULL;
switch (argc) {
case 3:
/* fallthrough */
rb_engine = argv[2];
case 2:
rb_ch = argv[0];
rb_msg = argv[1];
break;
case 1: {
/* single argument must be a Hash */
Check_Type(argv[0], T_HASH);
rb_ch = rb_hash_aref(argv[0], to_id);
if (rb_ch == Qnil || rb_ch == Qfalse) {
rb_ch = rb_hash_aref(argv[0], channel_id);
}
rb_msg = rb_hash_aref(argv[0], message_id);
rb_engine = rb_hash_aref(argv[0], engine_id);
} break;
default:
rb_raise(rb_eArgError, "method accepts 1-3 arguments.");
}
if (rb_msg == Qnil || rb_msg == Qfalse) {
rb_raise(rb_eArgError, "message is required.");
}
Check_Type(rb_msg, T_STRING);
if (rb_ch == Qnil || rb_ch == Qfalse)
rb_raise(rb_eArgError, "target / channel is required .");
if (TYPE(rb_ch) == T_SYMBOL)
rb_ch = rb_sym2str(rb_ch);
Check_Type(rb_ch, T_STRING);
if (rb_engine == Qfalse) {
engine = FIO_PUBSUB_PROCESS;
} else if (rb_engine != Qnil) {
// collect engine object
iodine_pubsub_s *e = iodine_pubsub_CData(rb_engine);
if (e) {
engine = e->engine;
}
}
fio_publish(.engine = engine, .channel = IODINE_RSTRINFO(rb_ch),
.message = IODINE_RSTRINFO(rb_msg));
return Qtrue;
(void)self;
}
|
.run ⇒ Object
Runs a block of code asyncronously (adds the code to the event queue).
Always returns the block of code to executed (Proc object).
Code will be executed only while Iodine is running (after start).
Code blocks that where scheduled to run before Iodine enters cluster mode will run on all child processes.
201 202 203 204 205 206 207 |
# File 'ext/iodine/iodine_defer.c', line 201
static VALUE iodine_defer_run(VALUE self) {
rb_need_block();
VALUE block = IodineStore.add(rb_block_proc());
fio_defer(iodine_defer_performe_once, (void *)block, NULL);
return block;
(void)self;
}
|
.run_after(milliseconds) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'ext/iodine/iodine_defer.c', line 217
static VALUE iodine_defer_run_after(VALUE self, VALUE milliseconds) {
(void)(self);
if (milliseconds == Qnil) {
return iodine_defer_run(self);
}
if (TYPE(milliseconds) != T_FIXNUM) {
rb_raise(rb_eTypeError, "milliseconds must be a number");
return Qnil;
}
size_t milli = FIX2UINT(milliseconds);
if (milli == 0) {
return iodine_defer_run(self);
}
// requires a block to be passed
rb_need_block();
VALUE block = rb_block_proc();
if (block == Qnil)
return Qfalse;
IodineStore.add(block);
if (fio_run_every(milli, 1, iodine_defer_run_timer, (void *)block,
(void (*)(void *))IodineStore.remove) == -1) {
perror("ERROR: Iodine couldn't initialize timer");
return Qnil;
}
return block;
}
|
.run_every(*args) ⇒ Object
Runs the required block after the specified number of milliseconds have passed. Time is counted only once Iodine started running (using start).
Accepts:
- milliseconds
-
the number of milliseconds between event repetitions.
- repetitions
-
the number of event repetitions. Defaults to 0 (never ending).
- block
-
(required) a block is required, as otherwise there is nothing to
perform.
The event will repeat itself until the number of repetitions had been delpeted.
Always returns a copy of the block object.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'ext/iodine/iodine_defer.c', line 260
static VALUE iodine_defer_run_every(int argc, VALUE *argv, VALUE self) {
(void)(self);
VALUE milliseconds, repetitions, block;
rb_scan_args(argc, argv, "11&", &milliseconds, &repetitions, &block);
if (TYPE(milliseconds) != T_FIXNUM) {
rb_raise(rb_eTypeError, "milliseconds must be a number.");
return Qnil;
}
if (repetitions != Qnil && TYPE(repetitions) != T_FIXNUM) {
rb_raise(rb_eTypeError, "repetitions must be a number or `nil`.");
return Qnil;
}
size_t milli = FIX2UINT(milliseconds);
size_t repeat = (repetitions == Qnil) ? 0 : FIX2UINT(repetitions);
// requires a block to be passed
rb_need_block();
IodineStore.add(block);
if (fio_run_every(milli, repeat, iodine_defer_run_timer, (void *)block,
(void (*)(void *))IodineStore.remove) == -1) {
perror("ERROR: Iodine couldn't initialize timer");
return Qnil;
}
return block;
}
|
.start ⇒ Object
This will block the calling (main) thread and start the Iodine reactor.
When using cluster mode (2 or more worker processes), it is important that no other threads are active.
For many reasons, ‘fork` should NOT be called while multi-threading, so cluster mode must always be initiated from the main thread in a single thread environment.
For information about why forking in multi-threaded environments should be avoided, see (for example): www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'ext/iodine/iodine.c', line 232
static VALUE iodine_start(VALUE self) {
if (fio_is_running()) {
rb_raise(rb_eRuntimeError, "Iodine already running!");
}
IodineCaller.set_GVL(1);
VALUE threads_rb = iodine_threads_get(self);
VALUE workers_rb = iodine_workers_get(self);
iodine_start_params_s params = {
.threads = NUM2SHORT(threads_rb),
.workers = NUM2SHORT(workers_rb),
};
iodine_print_startup_message(params);
IodineCaller.leaveGVL(iodine_run_outside_GVL, ¶ms);
return self;
}
|
.stop ⇒ Object
This will stop the iodine server, shutting it down.
If called within a worker process (rather than the root/master process), this will cause a hot-restart for the worker.
254 255 256 257 |
# File 'ext/iodine/iodine.c', line 254 static VALUE iodine_stop(VALUE self) { fio_stop(); return self; } |
.subscribe(*args) ⇒ Object
Subscribes to a Pub/Sub stream / channel or replaces an existing subscription.
The method accepts 1-2 arguments and an optional block. These are all valid ways to call the method:
subscribe("my_stream") {|source, msg| p msg }
subscribe("my_strea*", match: :redis) {|source, msg| p msg }
subscribe(to: "my_stream") {|source, msg| p msg }
# or use any object that answers `#call(source, msg)`
MyProc = Proc.new {|source, msg| p msg }
subscribe to: "my_stream", match: :redis, handler: MyProc
The first argument must be either a String or a Hash.
The second, optional, argument must be a Hash (if given).
The options Hash supports the following possible keys (other keys are ignored, all keys are Symbols):
- :match
-
The channel / subject name matching type to be used. Valid value is: ‘:redis`. Future versions hope to support `:nats` and `:rabbit` patern matching as well.
- :to
-
The channel / subject to subscribe to.
- :as
-
(only for WebSocket connections) accepts the optional value ‘:binary`. default is `:text`. Note that binary transmissions are illegal for some connections (such as SSE) and an attempted binary subscription will fail for these connections.
- :handler
-
Any object that answers ‘#call(source, msg)` where source is the stream / channel name.
Note: if an existing subscription with the same name exists, it will be replaced by this new subscription.
Returns the name of the subscription, which matches the name be used in unsubscribe (or nil on failure).
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'ext/iodine/iodine_connection.c', line 576
static VALUE iodine_pubsub_subscribe(int argc, VALUE *argv, VALUE self) {
// clang-format on
iodine_sub_args_s args = iodine_subscribe_args(argc, argv);
if (args.channel == Qnil) {
return Qnil;
}
iodine_connection_data_s *c = NULL;
if (TYPE(self) == T_MODULE) {
if (!args.block) {
rb_raise(rb_eArgError,
"block or :handler required for local subscriptions.");
}
} else {
c = iodine_connection_validate_data(self);
if (!c || (c->info.type == IODINE_CONNECTION_SSE && args.binary)) {
if (args.block) {
IodineStore.remove(args.block);
}
return Qnil; /* cannot subscribe a closed / invalid connection. */
}
if (args.block == Qnil) {
if (c->info.type == IODINE_CONNECTION_WEBSOCKET)
websocket_optimize4broadcasts((args.binary
? WEBSOCKET_OPTIMIZE_PUBSUB_BINARY
: WEBSOCKET_OPTIMIZE_PUBSUB),
1);
if (args.binary) {
args.block = Qtrue;
}
}
fio_atomic_add(&c->ref, 1);
}
subscription_s *sub =
fio_subscribe(.channel = IODINE_RSTRINFO(args.channel),
.on_message = iodine_on_pubsub,
.on_unsubscribe = iodine_on_unsubscribe, .udata1 = c,
.udata2 = (void *)args.block, .match = args.pattern);
if (c) {
fio_lock(&c->lock);
if (c->info.uuid == -1) {
fio_unsubscribe(sub);
fio_unlock(&c->lock);
return Qnil;
}
iodine_sub_add(&c->subscriptions, sub);
fio_unlock(&c->lock);
} else {
fio_lock(&sub_lock);
iodine_sub_add(&sub_global, sub);
fio_unlock(&sub_lock);
}
return args.channel;
}
|
.threads ⇒ Object
Returns the number of worker threads that will be used when start is called.
Negative numbers are translated as fractions of the number of CPU cores. i.e., -2 == half the number of detected CPU cores.
Zero values promise nothing (iodine will decide what to do with them).
96 97 98 99 100 101 |
# File 'ext/iodine/iodine.c', line 96
static VALUE iodine_threads_get(VALUE self) {
VALUE i = rb_ivar_get(self, rb_intern2("@threads", 8));
if (i == Qnil)
i = INT2NUM(0);
return i;
}
|
.threads=(val) ⇒ Object
Sets the number of worker threads that will be used when start is called.
Negative numbers are translated as fractions of the number of CPU cores. i.e., -2 == half the number of detected CPU cores.
Zero values promise nothing (iodine will decide what to do with them).
112 113 114 115 116 117 118 119 |
# File 'ext/iodine/iodine.c', line 112
static VALUE iodine_threads_set(VALUE self, VALUE val) {
Check_Type(val, T_FIXNUM);
if (NUM2SSIZET(val) >= (1 << 12)) {
rb_raise(rb_eRangeError, "requsted thread count is out of range.");
}
rb_ivar_set(self, rb_intern2("@threads", 8), val);
return val;
}
|
.unsubscribe(name) ⇒ Object
Unsubscribes from a Pub/Sub stream / channel.
The method accepts a single arguments, the name used for the subscription. i.e.:
subscribe("my_stream") {|source, msg| p msg }
unsubscribe("my_stream")
Returns ‘true` if the subscription was found.
Returns ‘false` if the subscription didn’t exist.
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'ext/iodine/iodine_connection.c', line 644
static VALUE iodine_pubsub_unsubscribe(VALUE self, VALUE name) {
// clang-format on
iodine_connection_data_s *c = NULL;
fio_lock_i *s_lock = &sub_lock;
fio_subhash_s *subs = &sub_global;
VALUE ret;
if (TYPE(self) != T_MODULE) {
c = iodine_connection_validate_data(self);
if (!c || c->info.uuid == -1) {
return Qnil; /* cannot unsubscribe a closed connection. */
}
s_lock = &c->lock;
subs = &c->subscriptions;
}
fio_lock(s_lock);
ret = iodine_sub_unsubscribe(subs, IODINE_RSTRINFO(name));
fio_unlock(s_lock);
return ret;
}
|
.verbosity ⇒ Object
Gets the logging level used for Iodine messages.
Levels range from 0-5, where:
0 == Quite (no messages) 1 == Fatal Errors only. 2 == Errors only (including fatal errors). 3 == Warnings and errors only. 4 == Informational messages, warnings and errors (default). 5 == Everything, including debug information.
Logging is always performed to the process’s STDERR and can be piped away.
NOTE: this does NOT effect HTTP logging.
137 138 139 140 |
# File 'ext/iodine/iodine.c', line 137
static VALUE iodine_logging_get(VALUE self) {
return INT2FIX(FIO_LOG_LEVEL);
(void)self;
}
|
.verbosity=(val) ⇒ Object
Gets the logging level used for Iodine messages.
Levels range from 0-5, where:
0 == Quite (no messages) 1 == Fatal Errors only. 2 == Errors only (including fatal errors). 3 == Warnings and errors only. 4 == Informational messages, warnings and errors (default). 5 == Everything, including debug information.
Logging is always performed to the process’s STDERR and can be piped away.
NOTE: this does NOT effect HTTP logging.
158 159 160 161 162 |
# File 'ext/iodine/iodine.c', line 158
static VALUE iodine_logging_set(VALUE self, VALUE val) {
Check_Type(val, T_FIXNUM);
FIO_LOG_LEVEL = FIX2INT(val);
return self;
}
|
.worker? ⇒ Boolean
Returns ‘true` if this process is a worker process or if iodine is running in a single process mode (the master is also a worker), `false` otherwise.
274 275 276 |
# File 'ext/iodine/iodine.c', line 274 static VALUE iodine_worker_is(VALUE self) { return fio_is_master() ? Qtrue : Qfalse; } |
.workers ⇒ Object
Returns the number of worker processes that will be used when start is called.
Negative numbers are translated as fractions of the number of CPU cores. i.e., -2 == half the number of detected CPU cores.
Zero values promise nothing (iodine will decide what to do with them).
1 == single process mode, the msater process acts as a worker process.
175 176 177 178 179 180 |
# File 'ext/iodine/iodine.c', line 175
static VALUE iodine_workers_get(VALUE self) {
VALUE i = rb_ivar_get(self, rb_intern2("@workers", 8));
if (i == Qnil)
i = INT2NUM(0);
return i;
}
|
.workers=(val) ⇒ Object
Sets the number of worker processes that will be used when start is called.
Negative numbers are translated as fractions of the number of CPU cores. i.e., -2 == half the number of detected CPU cores.
Zero values promise nothing (iodine will decide what to do with them).
1 == single process mode, the msater process acts as a worker process.
193 194 195 196 197 198 199 200 |
# File 'ext/iodine/iodine.c', line 193
static VALUE iodine_workers_set(VALUE self, VALUE val) {
Check_Type(val, T_FIXNUM);
if (NUM2SSIZET(val) >= (1 << 9)) {
rb_raise(rb_eRangeError, "requsted worker process count is out of range.");
}
rb_ivar_set(self, rb_intern2("@workers", 8), val);
return val;
}
|