From 72109b72d272c87e889b47f2b4d7111e80bf4e72 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Tue, 10 May 2011 09:36:07 +0200 Subject: [PATCH] various rack fixes --- buildconf/modular.ini | 2 +- fibers.ru | 31 ++++++++++++++++++-- plugins/rack/rack_plugin.c | 54 ++++++++++++++++++++++++++++++++++- plugins/ruby19/uwsgiplugin.py | 6 ++-- 4 files changed, 86 insertions(+), 7 deletions(-) diff --git a/buildconf/modular.ini b/buildconf/modular.ini index b208f039..aa39e124 100644 --- a/buildconf/modular.ini +++ b/buildconf/modular.ini @@ -1,5 +1,5 @@ [uwsgi] inherit = default plugins = python, ping, rack, psgi, cache, http, fastrouter -plugin_dir = . +plugin_dir = plugins embedded_plugins = null diff --git a/fibers.ru b/fibers.ru index 9e42a866..8de53956 100644 --- a/fibers.ru +++ b/fibers.ru @@ -2,11 +2,36 @@ require 'fiber' class SuspendingBody def each - for i in 1..100 + for i in 1..3 yield "number: #{i}\n" - UWSGI.suspend - #Fiber.yield + UWSGI.async_sleep(1) + puts "sleep..." + Fiber.yield end + + fd = UWSGI.async_connect("81.174.68.52:80") + UWSGI.wait_fd_write(fd, 3) + Fiber.yield + io = IO.new(fd) + puts "connected" + io.syswrite("GET /uwsgi/export/1081%3A362d695b2f25/plugins/fiber/fiber.c HTTP/1.0\r\n") + io.syswrite("Host: projects.unbit.it\r\n") + io.syswrite("\r\n") + + UWSGI.wait_fd_read(fd, 3) + Fiber.yield + + puts "data available" + + begin + while body = io.sysread(fd) + yield body + Fiber.yield + end + rescue + end + + io.close end end diff --git a/plugins/rack/rack_plugin.c b/plugins/rack/rack_plugin.c index 264c8c6f..38d61629 100644 --- a/plugins/rack/rack_plugin.c +++ b/plugins/rack/rack_plugin.c @@ -277,7 +277,55 @@ VALUE require_rails(VALUE arg) { VALUE init_rack_app(VALUE); -VALUE uwsgi_ruby_suspend(VALUE *arg) { +VALUE uwsgi_ruby_wait_fd_read(VALUE *class, VALUE arg1, VALUE arg2) { + + struct wsgi_request *wsgi_req = current_wsgi_req(); + + int fd = NUM2INT(arg1); + int timeout = NUM2INT(arg2); + + if (fd >= 0) { + async_add_fd_read(wsgi_req, fd, timeout); + } + + return Qtrue; +} + +VALUE uwsgi_ruby_wait_fd_write(VALUE *class, VALUE arg1, VALUE arg2) { + + struct wsgi_request *wsgi_req = current_wsgi_req(); + + int fd = NUM2INT(arg1); + int timeout = NUM2INT(arg2); + + if (fd >= 0) { + async_add_fd_write(wsgi_req, fd, timeout); + } + + return Qtrue; +} + +VALUE uwsgi_ruby_async_connect(VALUE *class, VALUE arg) { + + int fd = uwsgi_connect(RSTRING_PTR(arg), 0, 1); + + return INT2FIX(fd); +} + + +VALUE uwsgi_ruby_async_sleep(VALUE *class, VALUE arg) { + + struct wsgi_request *wsgi_req = current_wsgi_req(); + int timeout = NUM2INT(arg); + + if (timeout >= 0) { + async_add_timeout(wsgi_req, timeout); + } + + return Qtrue; +} + +VALUE uwsgi_ruby_suspend(VALUE *class) { struct wsgi_request *wsgi_req = current_wsgi_req(); @@ -319,6 +367,10 @@ int uwsgi_rack_init(){ VALUE rb_uwsgi_embedded = rb_define_module("UWSGI"); rb_define_module_function(rb_uwsgi_embedded, "suspend", uwsgi_ruby_suspend, 0); + rb_define_module_function(rb_uwsgi_embedded, "async_sleep", uwsgi_ruby_async_sleep, 1); + rb_define_module_function(rb_uwsgi_embedded, "wait_fd_read", uwsgi_ruby_wait_fd_read, 2); + rb_define_module_function(rb_uwsgi_embedded, "wait_fd_write", uwsgi_ruby_wait_fd_write, 2); + rb_define_module_function(rb_uwsgi_embedded, "async_connect", uwsgi_ruby_async_connect, 1); if (ur.rack) { ur.dispatcher = rb_protect(init_rack_app, rb_str_new2(ur.rack), &error); diff --git a/plugins/ruby19/uwsgiplugin.py b/plugins/ruby19/uwsgiplugin.py index 91d593f3..a1754ca1 100644 --- a/plugins/ruby19/uwsgiplugin.py +++ b/plugins/ruby19/uwsgiplugin.py @@ -2,8 +2,10 @@ import os,sys NAME='ruby19' -#RUBYPATH = '/opt/ruby19/bin/ruby' -RUBYPATH = '/Users/roberto/RUBY/bin/ruby' +try: + RUBYPATH = os.environ['UWSGICONFIG_RUBYPATH'] +except: + RUBYPATH = 'ruby' GCC_LIST = ['../rack/rack_plugin'] CFLAGS = os.popen(RUBYPATH + " -e \"require 'rbconfig';print Config::CONFIG['CFLAGS']\"").read().rstrip().split()