Skip to content

Instantly share code, notes, and snippets.

@matsumotory
Last active June 3, 2018 19:19
Show Gist options
  • Save matsumotory/247e207c3b12be434d595e48f682edb6 to your computer and use it in GitHub Desktop.
Save matsumotory/247e207c3b12be434d595e48f682edb6 to your computer and use it in GitHub Desktop.
    server {
        listen       48080;
        server_name  localhost;
        root /home/ubuntu/DEV/ngx_mruby/build/nginx/html/;

        # test for hello world and cache option
        location /mruby {
            mruby_rewrite_handler_code 'Nginx::Async.sleep 3000; Nginx.echo "done"';
        }
    }

こういう別のnginxを作って、

        location /sub_req_proxy_pass {
            proxy_pass http://127.0.0.1:48080/mruby;
            mruby_output_body_filter_code '
              Nginx.log Nginx::LOG_INFO, "read subrequest proxy pass"
            ';
        }

        location /async_http_sub_request_with_proxy_pass {
            mruby_rewrite_handler_code '
              Nginx::Async::HTTP.sub_request "/sub_req_proxy_pass"
              res = Nginx::Async::HTTP.last_response
              Nginx.rputs res.body
            ';
        }
     

こういうhttp clientを作って、

[ubuntu@ubuntu-xenial:~/DEV/mruby-simplehttpserver]$ ps auwxf | grep ngin[x]
ubuntu    8463  0.6  0.1  34880 11136 pts/1    S+   03:59   0:00  |           \_ ./build/nginx/sbin/nginx
ubuntu    8420  0.1  0.1  33000  8676 pts/4    S+   03:58   0:00              \_ ./build2/nginx/sbin/nginx -c /home/ubuntu/DEV/ngx_mruby/build2/nginx/conf/nginx.conf

どっちもシングルプロセスなのに、

[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass &
[1] 8559
[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass &
[2] 8561
[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass &
[3] 8563
[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ done

real    0m3.014s
user    0m0.004s
sys     0m0.000s
done

real    0m3.016s
user    0m0.000s
sys     0m0.004s
done

real    0m3.023s
user    0m0.004s
sys     0m0.000s

同時に3つリクエスト投げると、http clientもsleepもブロックされることなく、ちゃんと1プロセスで同時に3つ受けたリクエストを同時に3つレスポンスとしても返せている....!

@matsumotory
Copy link
Author

matsumotory commented Jun 3, 2018

もちろんこういうのも。

[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ for i in `seq 1 20`; do time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass & done
[1] 8607
[2] 8608
[3] 8611
[4] 8612
[5] 8613
[6] 8614
[7] 8615
[8] 8616
[9] 8617
[10] 8618
[11] 8619
[12] 8620
[13] 8621
[14] 8623
[15] 8625
[16] 8626
[17] 8628
[18] 8630
[19] 8633
[20] 8635
[ubuntu@ubuntu-xenial:~/DEV/ngx_mruby]$ done

real    0m3.023s
user    0m0.000s
sys     0m0.004s
done

real    0m3.046s
user    0m0.004s
sys     0m0.000s
done

real    0m3.069s
user    0m0.004s
sys     0m0.000s
done

real    0m3.067s
user    0m0.004s
sys     0m0.000s
done

real    0m3.077s
user    0m0.000s
sys     0m0.004s
done

real    0m3.080s
user    0m0.000s
sys     0m0.004s
done

real    0m3.084s
user    0m0.004s
sys     0m0.000s
done

real    0m3.093s
user    0m0.004s
sys     0m0.000s
done

real    0m3.083s
user    0m0.004s
sys     0m0.000s
done

real    0m3.086s
user    0m0.004s
sys     0m0.000s
done

real    0m3.088s
user    0m0.000s
sys     0m0.004s
done

real    0m3.110s
user    0m0.004s
sys     0m0.000s
done

real    0m3.125s
user    0m0.004s
sys     0m0.000s
done

real    0m3.129s
user    0m0.004s
sys     0m0.000s
done

real    0m3.133s
user    0m0.004s
sys     0m0.000s
done

real    0m3.126s
user    0m0.000s
sys     0m0.008s
done

real    0m3.148s
user    0m0.004s
sys     0m0.000s
done

real    0m3.146s
user    0m0.004s
sys     0m0.000s
done

real    0m3.146s
user    0m0.000s
sys     0m0.004s
done

real    0m3.148s
user    0m0.000s
sys     0m0.008s

[1]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[2]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[3]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[4]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[5]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[6]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[7]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[8]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[9]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[10]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[11]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[12]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[13]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[14]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[15]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[16]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[17]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[18]   Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[19]-  Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass
[20]+  Done                    time curl 127.0.0.1:58080/async_http_sub_request_with_proxy_pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment