Skip to content

Commit

Permalink
what's in a name?
Browse files Browse the repository at this point in the history
  • Loading branch information
boazsegev committed Oct 22, 2024
1 parent 35cec3a commit b0e9627
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 51 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: false # runs 'bundle install' and caches installed gems automatically
- name: Install Gems
run: bundle install
- name: Build and Test Iodine
Expand All @@ -38,9 +37,7 @@ jobs:
echo cflags = $cflags
echo HOME = $HOME
ruby -e 'puts Gem.default_dir'
bundle exec rake install
env VERBOSE=1 bundle exec rake install --trace
# env VERBOSE=1 bundle exec rspec --format documentation
# - name: Run tests
# run: bundle exec rake
# - name: Install dependencies
# run: sudo apt-get install -y build-essential libssl-dev zlib1g-dev ruby-dev
30 changes: 15 additions & 15 deletions ext/iodine/fio-stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11231,8 +11231,8 @@ typedef enum {
FIO_CALL_ON_CHILD_CRUSH,
/** Called by each worker thread in a Server Async queue as it ends. */
FIO_CALL_ON_WORKER_THREAD_END,
/** Called just before finishing up (both on child and parent processes). */
FIO_CALL_ON_FINISH,
/** Called when wither a *Worker* or *Master* stopped. */
FIO_CALL_ON_STOP,
/** An alternative to the system's at_exit. */
FIO_CALL_AT_EXIT,
/** used for testing and array allocation - must be last. */
Expand Down Expand Up @@ -11322,7 +11322,7 @@ static const char *FIO___STATE_TASKS_NAMES[FIO_CALL_NEVER + 1] = {
[FIO_CALL_ON_PARENT_CRUSH] = "ON_PARENT_CRUSH",
[FIO_CALL_ON_CHILD_CRUSH] = "ON_CHILD_CRUSH",
[FIO_CALL_ON_WORKER_THREAD_END] = "ON_WORKER_THREAD_END",
[FIO_CALL_ON_FINISH] = "ON_FINISH",
[FIO_CALL_ON_STOP] = "ON_FINISH",
[FIO_CALL_AT_EXIT] = "AT_EXIT",
[FIO_CALL_NEVER] = "NEVER",
};
Expand Down Expand Up @@ -31461,7 +31461,7 @@ typedef struct fio_srv_listen_args {
*
* This will be called separately for every process before exiting.
*/
void (*on_finish)(fio_protocol_s *protocol, void *udata);
void (*on_stop)(fio_protocol_s *protocol, void *udata);
/**
* Selects a queue that will be used to schedule a pre-accept task.
* May be used to test user thread stress levels before accepting connections.
Expand Down Expand Up @@ -31689,7 +31689,7 @@ SFUNC void fio_srv_run_every(fio_timer_schedule_args_s args);
* * Opaque user data:
* void *udata2
* * Called when the timer is done (finished):
* void (*on_finish)(void *, void *)
* void (*on_stop)(void *, void *)
* * Timer interval, in milliseconds:
* uint32_t every
* * The number of times the timer should be performed. -1 == infinity:
Expand Down Expand Up @@ -32626,7 +32626,7 @@ FIO_SFUNC void fio_s_destroy(fio_s *io) {
/* store info, as it might be freed if the protocol is freed. */
if (FIO_LIST_IS_EMPTY(&io->pr->reserved.ios))
FIO_LIST_REMOVE_RESET(&io->pr->reserved.protocols);
/* call on_finish / free callbacks . */
/* call on_stop / free callbacks . */
io->pr->io_functions.cleanup(io->tls);
io->pr->on_close(io->udata); /* may destroy protocol object! */
fio___srv_env_safe_destroy(&io->env);
Expand Down Expand Up @@ -33116,7 +33116,7 @@ FIO_SFUNC void fio___srv_work(int is_worker) {
}
fio_queue_perform_all(fio___srv_tasks);
fio_queue_perform_all(fio___srv_tasks);
fio_state_callback_force(FIO_CALL_ON_FINISH);
fio_state_callback_force(FIO_CALL_ON_STOP);
fio_queue_perform_all(fio___srv_tasks);
fio___srvdata.workers = 0;
}
Expand All @@ -33138,7 +33138,7 @@ static void *fio___srv_worker_sentinel(void *pid_data) {
int status = 0;
(void)status;
fio_thread_t thr = fio_thread_current();
fio_state_callback_add(FIO_CALL_ON_FINISH,
fio_state_callback_add(FIO_CALL_ON_STOP,
fio___srv_wait_for_worker,
(void *)thr);
if (fio_thread_waitpid(pid, &status, 0) != pid && !fio___srvdata.stop)
Expand All @@ -33151,7 +33151,7 @@ static void *fio___srv_worker_sentinel(void *pid_data) {
FIO_ASSERT_DEBUG(
0,
"DEBUG mode prevents worker re-spawning, now crashing parent.");
fio_state_callback_remove(FIO_CALL_ON_FINISH,
fio_state_callback_remove(FIO_CALL_ON_STOP,
fio___srv_wait_for_worker,
(void *)thr);
fio_thread_detach(&thr);
Expand Down Expand Up @@ -33476,7 +33476,7 @@ typedef struct {
fio_queue_s *queue;
fio_s *io;
void (*on_start)(fio_protocol_s *protocol, void *udata);
void (*on_finish)(fio_protocol_s *protocol, void *udata);
void (*on_stop)(fio_protocol_s *protocol, void *udata);
int owner;
int fd;
size_t ref_count;
Expand Down Expand Up @@ -33518,8 +33518,8 @@ static void fio___srv_listen_free(void *l_) {
}
#endif

if (l->on_finish)
l->on_finish(l->protocol, l->udata);
if (l->on_stop)
l->on_stop(l->protocol, l->udata);

if (l->hide_from_log)
FIO_LOG_DEBUG2("(%d) stopped listening @ %.*s",
Expand Down Expand Up @@ -33704,7 +33704,7 @@ SFUNC void *fio_srv_listen FIO_NOOP(struct fio_srv_listen_args args) {
.tls_ctx = built_tls,
.queue_for_accept = args.queue_for_accept,
.on_start = args.on_start,
.on_finish = args.on_finish,
.on_stop = args.on_stop,
.owner = fio___srvdata.pid,
.url_len = url_buf.len,
.hide_from_log = args.hide_from_log,
Expand Down Expand Up @@ -42437,7 +42437,7 @@ static void fio___http_listen_on_start(fio_protocol_s *protocol, void *u) {
: fio_srv_queue());
}

static void fio___http_listen_on_finished(fio_protocol_s *p, void *u) {
static void fio___http_listen_on_stop(fio_protocol_s *p, void *u) {
(void)u;
fio___http_protocol_free(
FIO_PTR_FROM_FIELD(fio___http_protocol_s,
Expand All @@ -42455,7 +42455,7 @@ SFUNC void *fio_http_listen FIO_NOOP(const char *url, fio_http_settings_s s) {
.protocol = &p->state[FIO___HTTP_PROTOCOL_ACCEPT].protocol,
.tls = s.tls,
.on_start = fio___http_listen_on_start,
.on_finish = fio___http_listen_on_finished,
.on_stop = fio___http_listen_on_stop,
.queue_for_accept = p->settings.queue);
return listener;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/iodine/iodine.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void Init_iodine_ext(void) {
IODINE_CONST_ID_STORE(IODINE_STATE_ON_START, "on_start");
IODINE_CONST_ID_STORE(IODINE_STATE_ON_PARENT_CRUSH, "on_parent_crush");
IODINE_CONST_ID_STORE(IODINE_STATE_ON_CHILD_CRUSH, "on_child_crush");
IODINE_CONST_ID_STORE(IODINE_STATE_START_SHUTDOWN, "start_shutdown");
IODINE_CONST_ID_STORE(IODINE_STATE_ON_FINISH, "on_finish");
IODINE_CONST_ID_STORE(IODINE_STATE_ON_SHUTDOWN, "on_shutdown");
IODINE_CONST_ID_STORE(IODINE_STATE_ON_STOP, "on_stop");

STORE.hold(IODINE_RACK_HIJACK_SYM = rb_id2sym(IODINE_RACK_HIJACK_ID));
STORE.hold(IODINE_RACK_HIJACK_STR = rb_str_new_static("rack.hijack", 11));
Expand Down
38 changes: 20 additions & 18 deletions ext/iodine/iodine_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ static VALUE iodine_connection_create_from_io(fio_s *io) {

/** Creates (and allocates) a new Iodine::Connection object. */
static VALUE iodine_connection_create_from_http(fio_http_s *h) {
VALUE m = fio_http_udata2(h) ? (VALUE)fio_http_udata2(h)
: rb_obj_alloc(iodine_rb_IODINE_CONNECTION);
VALUE m = fio_http_udata2(h)
? (VALUE)fio_http_udata2(h)
: iodine_connection_alloc(iodine_rb_IODINE_CONNECTION);
STORE.hold(m);
iodine_connection_s *c = iodine_connection_ptr(m);
c->store[IODINE_CONNECTION_STORE_handler] = (VALUE)fio_http_udata(h);
Expand Down Expand Up @@ -317,10 +318,10 @@ FIO_SFUNC int iodine_connection_map_headers_task(fio_http_s *h,
}
if (RB_TYPE_P(tmp, RUBY_T_STRING)) {
ary = rb_ary_new();
STORE.hold(ary);
c->store[IODINE_CONNECTION_STORE_rack] = ary; /* unused, so use it */
rb_ary_push(ary, tmp);
iodine_hmap_set(&c->map.map, k, ary, NULL);
STORE.release(ary);
c->store[IODINE_CONNECTION_STORE_rack] = Qnil;
tmp = ary;
}
rb_ary_push(tmp, rb_str_new(value.buf, value.len));
Expand Down Expand Up @@ -477,7 +478,7 @@ typedef struct {
} service;
} iodine_connection_args_s;

static void iodine_tcp_on_finish(fio_protocol_s *p, void *udata);
static void iodine_tcp_on_stop(fio_protocol_s *p, void *udata);
static void *iodine_tcp_listen(iodine_connection_args_s args);

/* *****************************************************************************
Expand Down Expand Up @@ -571,7 +572,7 @@ static int iodine_connection_cookie_each_task(fio_http_s *h,
VALUE *argv = (VALUE *)info;
++argv;
argv[0] = rb_usascii_str_new(name.buf, name.len);
STORE.hold(argv[0]);
STORE.hold(argv[0]); /* TODO, avoid STORE for fast path if possible */
argv[1] = rb_usascii_str_new(value.buf, value.len);
STORE.hold(argv[1]);
rb_yield_values2(2, argv);
Expand Down Expand Up @@ -996,6 +997,7 @@ static VALUE iodine_handler_deafult_on_http(VALUE handler, VALUE client) {
if (RB_TYPE_P(r, RUBY_T_ARRAY) && RARRAY_LEN(r) >= 3)
rb_check_funcall(RARRAY_PTR(r)[2], IODINE_CLOSE_ID, 0, NULL);
fio_http_send_error_response(c->http, 500);
c->store[IODINE_CONNECTION_STORE_rack] = Qnil;
goto after_reply;
}

Expand Down Expand Up @@ -1137,10 +1139,10 @@ static int iodine_env_populate_header_data(fio_http_s *h,
c->store[IODINE_CONNECTION_STORE_tmp] = key;
/* copy value */
VALUE val = rb_str_new(v.buf, v.len);
c->map.tmp = val; /* protect from GC leveraging minimap logic */
c->store[IODINE_CONNECTION_STORE_rack] = val; /* unused at this point */
/* finish up */
rb_hash_aset(c->store[IODINE_CONNECTION_STORE_env], key, val);
c->map.tmp = 0;
c->store[IODINE_CONNECTION_STORE_rack] = Qnil;
c->store[IODINE_CONNECTION_STORE_tmp] = Qnil;
return 0;
(void)h;
Expand Down Expand Up @@ -1386,18 +1388,18 @@ static void iodine_io_raw_on_attach(fio_s *io) {

static void *iodine_io_raw_on_data_in_GVL(void *info_) {
iodine_io_raw_on_data_info_s *i = (iodine_io_raw_on_data_info_s *)info_;
VALUE buf = rb_usascii_str_new(i->buf, (long)i->len);
STORE.hold(buf);
VALUE connection = (VALUE)fio_udata_get(i->io);
if (!connection || connection == Qnil)
return NULL;
iodine_connection_s *c = iodine_connection_ptr(connection);
VALUE buf = rb_usascii_str_new(i->buf, (long)i->len);
c->store[IODINE_CONNECTION_STORE_tmp] = buf;
VALUE args[] = {connection, buf};
iodine_ruby_call_inside(c->store[IODINE_CONNECTION_STORE_handler],
IODINE_ON_MESSAGE_ID,
2,
args);
STORE.release(buf);
c->store[IODINE_CONNECTION_STORE_tmp] = Qnil;
return NULL;
}

Expand Down Expand Up @@ -1603,12 +1605,12 @@ static void *iodine_io_http_on_eventsource_reconnect_internal(void *info_) {
return NULL;
iodine_connection_s *c = iodine_connection_ptr(connection);
VALUE args[] = {connection, rb_str_new(i->id.buf, i->id.len)};
STORE.hold(args[1]);
c->store[IODINE_CONNECTION_STORE_tmp] = args[1];
iodine_ruby_call_inside(c->store[IODINE_CONNECTION_STORE_handler],
IODINE_ON_EVENTSOURCE_RECONNECT_ID,
2,
args);
STORE.release(args[1]);
c->store[IODINE_CONNECTION_STORE_tmp] = Qnil;
return NULL;
}

Expand Down Expand Up @@ -1671,14 +1673,14 @@ static void *iodine_io_http_on_message_internal(void *info) {
return NULL;
iodine_connection_s *c = iodine_connection_ptr(connection);
VALUE args[] = {connection, rb_str_new(i->msg.buf, i->msg.len)};
STORE.hold(args[1]);
c->store[IODINE_CONNECTION_STORE_tmp] = args[1];
rb_enc_associate(args[1],
i->is_text ? IodineUTF8Encoding : IodineBinaryEncoding);
iodine_ruby_call_inside(c->store[IODINE_CONNECTION_STORE_handler],
IODINE_ON_MESSAGE_ID,
2,
args);
STORE.release(args[1]);
c->store[IODINE_CONNECTION_STORE_tmp] = Qnil;
return NULL;
}

Expand Down Expand Up @@ -2050,7 +2052,7 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
.protocol = protocol,
.udata = args.settings.udata,
.tls = args.settings.tls,
.on_failed = iodine_tcp_on_finish);
.on_failed = iodine_tcp_on_stop);
}
c->store[IODINE_CONNECTION_STORE_handler] = (VALUE)args.settings.udata;
c->flags |= IODINE_CONNECTION_CLIENT;
Expand Down Expand Up @@ -2504,7 +2506,7 @@ static VALUE iodine_connection_publish_klass(int argc,
Listen to incoming TCP/IP Connections
***************************************************************************** */

static void iodine_tcp_on_finish(fio_protocol_s *p, void *udata) {
static void iodine_tcp_on_stop(fio_protocol_s *p, void *udata) {
STORE.release((VALUE)udata);
FIO_MEM_FREE(p, sizeof(*p));
}
Expand All @@ -2521,7 +2523,7 @@ static void *iodine_tcp_listen(iodine_connection_args_s args) {
.protocol = protocol,
.udata = args.settings.udata,
.tls = args.settings.tls,
.on_finish = iodine_tcp_on_finish,
.on_stop = iodine_tcp_on_stop,
.queue_for_accept = 0);
}

Expand Down
10 changes: 5 additions & 5 deletions ext/iodine/iodine_defer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static ID IODINE_STATE_ENTER_MASTER;
static ID IODINE_STATE_ON_START;
static ID IODINE_STATE_ON_PARENT_CRUSH;
static ID IODINE_STATE_ON_CHILD_CRUSH;
static ID IODINE_STATE_START_SHUTDOWN;
static ID IODINE_STATE_ON_FINISH;
static ID IODINE_STATE_ON_SHUTDOWN;
static ID IODINE_STATE_ON_STOP;

/* performs a Ruby state callback without clearing the Ruby object's memory */
static void iodine_perform_state_callback_persist(void *blk_) {
Expand Down Expand Up @@ -94,12 +94,12 @@ static VALUE iodine_on_state(VALUE self, VALUE event) { // clang-format on
fio_state_callback_add(FIO_CALL_ON_CHILD_CRUSH,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == IODINE_STATE_START_SHUTDOWN) {
} else if (state == IODINE_STATE_ON_SHUTDOWN) {
fio_state_callback_add(FIO_CALL_ON_SHUTDOWN,
iodine_perform_state_callback_persist,
(void *)block);
} else if (state == IODINE_STATE_ON_FINISH) {
fio_state_callback_add(FIO_CALL_ON_FINISH,
} else if (state == IODINE_STATE_ON_STOP) {
fio_state_callback_add(FIO_CALL_ON_STOP,
iodine_perform_state_callback_persist,
(void *)block);
} else {
Expand Down
5 changes: 0 additions & 5 deletions ext/iodine/iodine_minimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ static VALUE iodine_minimap_alloc(VALUE klass) {
*m = (iodine_minimap_s)FIO_MAP_INIT;
return self;
// return TypedData_Wrap_Struct(klass, &IODINE_MINIMAP_DATA_TYPE, m);

no_memory:
FIO_LOG_FATAL("Memory allocation failed");
fio_srv_stop();
return Qnil;
}

/* *****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion ext/iodine/iodine_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static struct value_reference_counter_store_s {
FIO_MAP_INIT,
FIO_MAP_INIT,
FIO_ARRAY_INIT,
256,
228,
FIO_THREAD_MUTEX_INIT,
iodine_store___hold,
iodine_store___release,
Expand Down
2 changes: 1 addition & 1 deletion lib/iodine/documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def self.listen(url = "0.0.0.0:3000", handler = nil, service = nil); end
# | `: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). |
# | `:on_stop` | the block will be called just before stopping iodine (both on child and parent processes). |
#
# Code runs in both the parent and the child.
def self.on_state(state, &block); end
Expand Down

0 comments on commit b0e9627

Please sign in to comment.