Skip to content

Commit

Permalink
Add fork_wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Feb 3, 2024
1 parent 05a972b commit a43b5d3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 0 additions & 1 deletion ext/agoo/con.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ con_header_read(agooCon c, size_t *mlenp) {
pend = proto;
for (; '\r' != *pend; pend++) {
}
*pend = '\0';
if (AGOO_GET == method) {
char root_buf[20148];
const char *root = NULL;
Expand Down
14 changes: 14 additions & 0 deletions ext/agoo/rserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ configure(agooErr err, int port, const char *root, VALUE options) {
}
agoo_server.thread_cnt = 0;
the_rserver.worker_cnt = 1;
the_rserver.forker = Qnil;
the_rserver.uses = NULL;
atomic_init(&agoo_server.running, 0);
agoo_server.listen_thread = 0;
Expand Down Expand Up @@ -132,6 +133,8 @@ configure(agooErr err, int port, const char *root, VALUE options) {
rb_raise(rb_eArgError, "thread_count must be between 1 and %d.", MAX_WORKERS);
}
}
the_rserver.forker = rb_hash_lookup(options, ID2SYM(rb_intern("fork_wrap")));

if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("poll_timeout"))))) {
double timeout = rb_num2dbl(v);

Expand Down Expand Up @@ -816,6 +819,7 @@ rserver_start(VALUE self) {
struct _agooErr err = AGOO_ERR_INIT;
VALUE agoo = rb_const_get_at(rb_cObject, rb_intern("Agoo"));
VALUE v = rb_const_get_at(agoo, rb_intern("VERSION"));
ID after = rb_intern("after");

*the_rserver.worker_pids = getpid();

Expand All @@ -830,6 +834,13 @@ rserver_start(VALUE self) {
if (AGOO_ERR_OK != setup_listen(&err)) {
rb_raise(rb_eIOError, "%s", err.msg);
}
if (1 < the_rserver.worker_cnt && the_rserver.forker != Qnil) {
ID before = rb_intern("before");

if (rb_respond_to(the_rserver.forker, before)) {
rb_funcall(the_rserver.forker, before, 0);
}
}
for (i = 1; i < the_rserver.worker_cnt; i++) {
VALUE rpid = rb_funcall(rb_cObject, rb_intern("fork"), 0);

Expand All @@ -850,6 +861,9 @@ rserver_start(VALUE self) {
the_rserver.worker_pids[i] = pid;
}
}
if (1 < the_rserver.worker_cnt && the_rserver.forker != Qnil && rb_respond_to(the_rserver.forker, after)) {
rb_funcall(the_rserver.forker, after, 0);
}
if (AGOO_ERR_OK != agoo_server_start(&err, "Agoo", StringValuePtr(v))) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
Expand Down
1 change: 1 addition & 0 deletions ext/agoo/rserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct _rServer {
int worker_cnt;
int worker_pids[MAX_WORKERS];
VALUE *eval_threads; // Qnil terminated
VALUE forker;
RUse uses;
} *RServer;

Expand Down
1 change: 1 addition & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ todo

- binding https://github.com/ohler55/agoo/issues/122
- before and after fork hook
- fork-wrap with before and after methods


- add additional optional plan and request to method calls
Expand Down

0 comments on commit a43b5d3

Please sign in to comment.