Module: Agoo::Server

Defined in:
ext/agoo/server.c

Class Method Summary collapse

Class Method Details

.add_mime(suffix, type) ⇒ Object

call-seq: add_mime(suffix, type)

Adds a mime type by associating a type string with a suffix. This is used for static files.



963
964
965
966
967
968
# File 'ext/agoo/server.c', line 963

static VALUE
add_mime(VALUE self, VALUE suffix, VALUE type) {
    mime_set(&the_server.pages, StringValuePtr(suffix), StringValuePtr(type));

    return Qnil;
}

.handle(method, pattern, handler) ⇒ Object

call-seq: handle(method, pattern, handler)

Registers a handler for the HTTP method and path pattern specified. The path pattern follows glob like rules in that a single * matches a single token bounded by the ‘/` character and a double ** matches all remaining.



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
# File 'ext/agoo/server.c', line 892

static VALUE
handle(VALUE self, VALUE method, VALUE pattern, VALUE handler) {
    Hook	hook;
    Method	meth = ALL;
    const char	*pat;

    rb_check_type(pattern, T_STRING);
    pat = StringValuePtr(pattern);

    if (connect_sym == method) {
	meth = CONNECT;
    } else if (delete_sym == method) {
	meth = DELETE;
    } else if (get_sym == method) {
	meth = GET;
    } else if (head_sym == method) {
	meth = HEAD;
    } else if (options_sym == method) {
	meth = OPTIONS;
    } else if (post_sym == method) {
	meth = POST;
    } else if (put_sym == method) {
	meth = PUT;
    } else if (Qnil == method) {
	meth = ALL;
    } else {
	rb_raise(rb_eArgError, "invalid method");
    }
    if (NULL == (hook = hook_create(meth, pat, handler))) {
	rb_raise(rb_eStandardError, "out of memory.");
    } else {
	Hook	h;
	Hook	prev = NULL;

	for (h = the_server.hooks; NULL != h; h = h->next) {
	    prev = h;
	}
	if (NULL != prev) {
	    prev->next = hook;
	} else {
	    the_server.hooks = hook;
	}
	rb_gc_register_address(&hook->handler);
    }
    return Qnil;
}

.handle_not_found(handler) ⇒ Object

call-seq: not_found_handle(handler)

Registers a handler to be called when no other hook is found and no static file is found.



946
947
948
949
950
951
952
953
954
# File 'ext/agoo/server.c', line 946

static VALUE
handle_not_found(VALUE self, VALUE handler) {
    if (NULL == (the_server.hook404 = hook_create(GET, "/", handler))) {
	rb_raise(rb_eStandardError, "out of memory.");
    }
    rb_gc_register_address(&the_server.hook404->handler);
    
    return Qnil;
}

.init(*args) ⇒ Object

call-seq: init(port, root, options)

Configures the server that will listen on the designated port and using the root as the root of the static resources. Logging is feature based and not level based and the options reflect that approach.

  • options [Hash] server options

    • :pedantic [true|false] if true response header and status codes are checked and an exception raised if they violate the rack spec at github.com/rack/rack/blob/master/SPEC, tools.ietf.org/html/rfc3875#section-4.1.18, or tools.ietf.org/html/rfc7230.

    • :thread_count [Integer] number of ruby worker threads. Defaults to one. If zero then the start function will not return but instead will proess using the thread that called start. Usually the default is best unless the workers are making IO calls.

    • :worker_count [Integer] number of workers to fork. Defaults to one which is not to fork.



272
273
274
275
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
# File 'ext/agoo/server.c', line 272

static VALUE
rserver_init(int argc, VALUE *argv, VALUE self) {
    struct _Err	err = ERR_INIT;
    int		port;
    const char	*root;
    VALUE	options = Qnil;

    if (argc < 2 || 3 < argc) {
	rb_raise(rb_eArgError, "Wrong number of arguments to Agoo::Server.configure.");
    }
    port = FIX2INT(argv[0]);
    rb_check_type(argv[1], T_STRING);
    root = StringValuePtr(argv[1]);
    if (3 <= argc) {
	options = argv[2];
    }
    memset(&the_server, 0, sizeof(struct _Server));
    pages_init(&the_server.pages);
    sub_init(&the_server.sub_cache);

    if (ERR_OK != configure(&err, port, root, options)) {
	rb_raise(rb_eArgError, "%s", err.msg);
    }
    queue_multi_init(&the_server.con_queue, 256, false, false);
    queue_multi_init(&the_server.pub_queue, 256, true, false);
    queue_multi_init(&the_server.eval_queue, 1024, false, true);

    pthread_mutex_init(&the_server.up_lock, 0);
    the_server.up_list = NULL;
    
    the_server.inited = true;

    return Qnil;
}

.path_group(path, dirs) ⇒ Object

call-seq: path_group(path, dirs)

Sets up a path group where the path defines a group of directories to search for a file. For example a path of ‘/assets’ could be mapped to a set of [ ‘home/user/images’, ‘/home/user/app/assets/images’ ].



978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'ext/agoo/server.c', line 978

static VALUE
path_group(VALUE self, VALUE path, VALUE dirs) {
    Group	g;

    rb_check_type(path, T_STRING);
    rb_check_type(dirs, T_ARRAY);

    if (NULL != (g = group_create(&the_server.pages, StringValuePtr(path)))) {
	int	i;
	int	dcnt = (int)RARRAY_LEN(dirs);
	VALUE	entry;
	
	for (i = dcnt - 1; 0 <= i; i--) {
	    entry = rb_ary_entry(dirs, i);
	    if (T_STRING != rb_type(entry)) {
		entry = rb_funcall(entry, rb_intern("to_s"), 0);
	    }
	    group_add(g, StringValuePtr(entry));
	}
    }
    return Qnil;
}

.shutdownObject

call-seq: shutdown()

Shutdown the server. Logs and queues are flushed before shutting down.



878
879
880
881
882
# File 'ext/agoo/server.c', line 878

static VALUE
rserver_shutdown(VALUE self) {
    server_shutdown();
    return Qnil;
}

.startObject

call-seq: start()

Start the server.



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
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
# File 'ext/agoo/server.c', line 786

static VALUE
server_start(VALUE self) {
    VALUE	*vp;
    int		i;
    double	giveup;
    int		pid;
    
    *the_server.worker_pids = getpid();
    setup_listen();
    the_server.active = true;

    for (i = 1; i < the_server.worker_cnt; i++) {
#if 0
	pid = fork();
#else
	{
	    VALUE	rpid = rb_funcall(rb_cObject, rb_intern("fork"), 0);

	    if (Qnil == rpid) {
		pid = 0;
	    } else {
		pid = NUM2INT(rpid);
	    }
	}
#endif
	if (0 > pid) { // error, use single process
	    log_cat(&error_cat, "Failed to fork. %s.", strerror(errno));
	    break;
	} else if (0 == pid) {
	    log_start(true);
	    break;
	} else {
	    the_server.worker_pids[i] = pid;
	}
    }
    pthread_create(&the_server.listen_thread, NULL, listen_loop, NULL);
    pthread_create(&the_server.con_thread, NULL, con_loop, NULL);
    
    giveup = dtime() + 1.0;
    while (dtime() < giveup) {
	if (2 <= atomic_load(&the_server.running)) {
	    break;
	}
	dsleep(0.01);
    }
    if (info_cat.on) {
	VALUE	agoo = rb_const_get_at(rb_cObject, rb_intern("Agoo"));
	VALUE	v = rb_const_get_at(agoo, rb_intern("VERSION"));
					       
	log_cat(&info_cat, "Agoo %s with pid %d is listening on port %d.", StringValuePtr(v), getpid(), the_server.port);
    }
    if (0 >= the_server.thread_cnt) {
	Req		req;

	while (the_server.active) {
	    if (NULL != (req = (Req)queue_pop(&the_server.eval_queue, 0.1))) {
		handle_protected(req, false);
		request_destroy(req);
	    } else {
		rb_thread_schedule();
	    }

	}
    } else {
	the_server.eval_threads = (VALUE*)malloc(sizeof(VALUE) * (the_server.thread_cnt + 1));
	DEBUG_ALLOC(mem_eval_threads, the_server.eval_threads);

	for (i = the_server.thread_cnt, vp = the_server.eval_threads; 0 < i; i--, vp++) {
	    *vp = rb_thread_create(wrap_process_loop, NULL);
	}
	*vp = Qnil;

	giveup = dtime() + 1.0;
	while (dtime() < giveup) {
	    // The processing threads will not start until this thread
	    // releases ownership so do that and then see if the threads has
	    // been started yet.
	    rb_thread_schedule();
	    if (2 + the_server.thread_cnt <= atomic_load(&the_server.running)) {
		break;
	    }
	}
    }
    return Qnil;
}