Skip to content

v3.0.0-alpha.3

Pre-release
Pre-release

Choose a tag to compare

@matsumotory matsumotory released this 28 Nov 07:35
6c8e05b

What 3.0.0-alpha.3 brings

ngx_mruby 3.0 is the next major version. In addition to ordinary web services, it aims to run Ruby rules in front of AI applications, model APIs and MCP servers. This third pre-release fixes an mruby body filter on proxied responses, and makes nginx's own test suite a check that the author of a pull request runs before it is ready. The AI gateway features are still planned for the pre-releases after it ("What comes next" below).

  • A body filter that adds to the body no longer sends bytes after the response. This applies to a response from an upstream (proxy_pass and the other *_pass directives) whose length is known, after an earlier phase handler left Nginx.rputs or Nginx.echo output and then declined. An mruby_output_body_filter that prepends or appends to f.body used to run again on the empty buffer that the upstream sends after the body, and its bytes were sent past the response's Content-Length. The body filter now passes that buffer on without running its code again. The entry is under "Behavior changes" below; 2.7.15 has the same fix.
  • Instructions per request as in 3.0.0-alpha.2. Before the tag, the tip of next before this fix (5191c8e83) was compared with 3.0.0-alpha.2 under callgrind. Every scenario changed by 0.05% or less; the largest change was in the scenario that relays a streamed Responses API answer (+0.05%, 27 instructions per request). The fix itself was compared with that tip: every scenario changed by 0.02% or less, the body filter scenario by +0.02% (3 instructions per request). These are instruction counts, not wall-clock times; the table is below.
  • nginx's own test suite before a pull request is ready. sh test/nginx-tests/run.sh runs nginx-tests on nginx with and without ngx_mruby, and exits with 1 when a test file fails only with ngx_mruby. The author of a pull request that changes the nginx binary, test.sh or the runner runs it before the pull request is ready (#880).
  • Try it locally. examples/start uses Docker Compose to run an nginx that routes requests by model to two mock APIs. No provider API key is needed. The migration guide lists what changes for a configuration or a script of 2.x.
The comparison with 3.0.0-alpha.2 (callgrind, before the tag)

Base v3.0.0-alpha.2 (2d87575b0), head 5191c8e83, in a Linux container on an arm64 host.

perf: N=20000 WARMUP=2000; Ir per request of nginx; WARN from 3.0%, FAIL from 5.0% on the number in the column judged: w/o GC/const (Ir/req minus the GC and the constant lookups that missed the cache), or w/o GC (minus the GC only) where the head clears the constant cache more often, or clears it and misses more lookups (const_clears, const_misses)
perf: units: stream_sleep = completed TCP session; other rows = HTTP request
perf: gems: 8 in both builds, 0 at different commits, 0 only in base, 0 only in head
perf: mruby: base 4.1.0, head 4.1.0; accepted differences (accepted.txt): none apply

scenario                   base Ir/req  head Ir/req  change  base w/o GC  head w/o GC  change w/o GC  base w/o GC/const  head w/o GC/const  change w/o GC/const     GC share        judged  result
hello                            11216        11216  -0.00%        10505        10505         -0.00%              10505              10505               -0.00%    6.3%/6.3%  w/o GC/const      ok
headers                          30068        30068  +0.00%        24883        24883         +0.00%              24883              24883               +0.00%  17.2%/17.2%  w/o GC/const      ok
var                              20364        20364  +0.00%        17327        17327         +0.00%              17327              17327               +0.00%  14.9%/14.9%  w/o GC/const      ok
filter                           14679        14679  +0.00%        12908        12909         +0.00%              12908              12909               +0.00%  12.1%/12.1%  w/o GC/const      ok
sleep                            15440        15439  -0.01%        14223        14222         -0.01%              14223              14222               -0.01%    7.9%/7.9%  w/o GC/const      ok
sub_request                      43813        43813  -0.00%        42153        42152         -0.00%              42153              42152               -0.00%    3.8%/3.8%  w/o GC/const      ok
file                             14302        14302  -0.00%        12532        12531         -0.00%              12532              12531               -0.00%  12.4%/12.4%  w/o GC/const      ok
proxy_plain_2k                   21812        21812  +0.00%        21812        21812         +0.00%              21812              21812               +0.00%    0.0%/0.0%  w/o GC/const      ok
proxy_plain_64k                  22124        22122  -0.01%        22124        22122         -0.01%              22124              22122               -0.01%    0.0%/0.0%  w/o GC/const      ok
proxy_stream_plain_50            54051        54053  +0.00%        54051        54053         +0.00%              54051              54053               +0.00%    0.0%/0.0%  w/o GC/const      ok
responses_stream_plain_50        55312        55339  +0.05%        55312        55339         +0.05%              55312              55339               +0.05%    0.0%/0.0%  w/o GC/const      ok
auth                             48121        48121  +0.00%        41933        41933         +0.00%              41933              41933               +0.00%  12.9%/12.9%  w/o GC/const      ok
route_json_2k                   106725       106725  -0.00%        98194        98194         -0.00%              98194              98194               -0.00%    8.0%/8.0%  w/o GC/const      ok
ruby_call_1                      10966        10966  +0.00%        10239        10239         +0.00%              10239              10239               +0.00%    6.6%/6.6%  w/o GC/const      ok
ruby_call_10                     41491        41491  -0.00%        34173        34172         -0.00%              34173              34172               -0.00%  17.6%/17.6%  w/o GC/const      ok
stream_sleep                     12234        12233  -0.01%        11290        11289         -0.01%              11290              11289               -0.01%    7.7%/7.7%  w/o GC/const      ok

What comes next

The next pre-releases are planned to add the parts of an AI gateway that 2.x does not have. None of them is in this pre-release.

  • A dictionary that all workers share, for budgets and rate-limit counters.
  • A filter that reads the server-sent events (SSE) of a streamed model response one event at a time, for example to count token usage.
  • Re-selecting the upstream, and a timeout for the first byte from the upstream, for model API fallback.
  • A reference agent proxy built on these parts.

Step 7 of the v3 plan lists the parts and their order. The uses they serve include MCP tool authorization, limits on bursts of agent calls, model API fallback and AI crawler control; the home page and the list of uses show the design status of each.

Behavior changes: read before upgrading

This section copies the entry of "Behavior changes" added since 3.0.0-alpha.2, the body filter fix above. The other change of the release notes since 3.0.0-alpha.2 is the entry of #602 under "Build and test changes", which #880 rewrote for the local run of nginx-tests. The release notes of this tag have all 80 entries.

An mruby body filter no longer runs a second time on the empty buffer at the end of a proxied response

  • Before: on a response from an upstream (proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass or memcached_pass), after an earlier phase handler left Nginx.rputs or Nginx.echo output in the module context and then declined, an mruby_output_body_filter(_code) ran a second time on the empty last buffer that the upstream sends after the body. A body filter whose output on an empty body is non-empty, such as one that prepends to or appends to f.body, produced bytes on that second run, and they were sent after the response, past its Content-Length.
  • Now: the body filter passes that trailing empty buffer on without running its code again, so no bytes are sent after the framed response.
  • Why it changed:
    • The defect: the bytes that a body filter whose output on an empty body is non-empty produced on the second run and that were sent past the response's Content-Length. On a connection that is kept alive, the client reads those bytes as the start of the next response on that connection; GHSA-5qjv-293c-64f8 has the details.
    • Why the old behavior could not be kept: the second run had no body to transform, so any bytes it produced could only follow a response that was already framed and sent.
    • What the documentation promised: docs/class_and_method/ did not describe the body filter running a second time, and docs/directives/ did not describe output after the response.
  • Affected: a location with mruby_output_body_filter(_code) whose output on an empty body is non-empty, served from an upstream whose Content-Length is known, with an earlier handler that leaves Nginx.rputs or Nginx.echo output and then returns a negative code the request declines past (in practice Nginx::DECLINED). A body filter that leaves an empty body empty, such as f.body = f.body.upcase, a static file, and a response sent chunked are unchanged.
  • What to do: nothing; rebuild the module with this release. A body filter that was guarded against an empty body (for example with unless f.body.empty?) can keep the guard or drop it.

3.0.0-alpha.3で得られるもの

ngx_mruby 3.0は、次のメジャーバージョンです。通常のWebサービスに加えて、AIアプリケーション、モデルAPI、MCPサーバーの前でRubyのルールを動かすことを目指しています。この3つ目のプレリリースでは、proxyのレスポンスでのmrubyのボディフィルタの動作を直しました。また、nginx本体のテストスイートを、PRの作者がreadyにする前に流す検査にしました。AIゲートウェイの機能は、引き続きこの後のプレリリースで足す予定です。下の「これから入るもの」を見てください。

  • 本文に足すボディフィルタが、レスポンスの後ろにバイトを送らなくなりました。 対象は、長さの分かっているupstream(proxy_passほかの*_pass)のレスポンスで、前段のフェーズのハンドラがNginx.rputsかNginx.echoの出力を残してDECLINEDした場合です。f.bodyの前や後ろに足すmruby_output_body_filterは、upstreamがボディのあとに送る空のバッファに対してもう一度走り、そのバイトがレスポンスのContent-Lengthを超えて送られていました。いまは、ボディフィルタがそのバッファを、コードを走らせずに次へ渡します。項目は下の「更新前に確認する挙動の変更」にあります。2.7.15にも同じ修正が入っています。
  • リクエストあたりの命令数は、3.0.0-alpha.2とほぼ同じです。 タグを付ける前の確認で、この修正を入れる前のnextの先端(5191c8e83)と3.0.0-alpha.2を、callgrindで比べました。どのシナリオも、変化は0.05%以下でした。いちばん大きく変わったのは、Responses APIのストリーミングの応答を中継するシナリオで、0.05%(1リクエストあたり27命令)増えました。修正そのものも、その先端と比べました。どのシナリオも、変化は0.02%以下で、ボディフィルタのシナリオは0.02%(1リクエストあたり3命令)増えました。これは命令数で、実行時間ではありません。表は下にあります。
  • PRをreadyにする前に、nginx本体のテストスイートを流します。 sh test/nginx-tests/run.shは、nginx-testsを、ngx_mrubyを組み込んだnginxと組み込まないnginxの両方で流します。ngx_mrubyを組み込んだときだけ落ちるテストのファイルがあると、終了コード1で終わります。nginxのバイナリ、test.sh、このスクリプトを変えるPRでは、作者がreadyにする前に流します(#880)。
  • 手元で試せます。 examples/startは、Docker Composeで、リクエストをモデルごとに2つのモックAPIへ振り分けるnginxを動かします。プロバイダのAPIキーは不要です。2.xの設定やスクリプトで変わる点は、移行の手引きにあります。
3.0.0-alpha.2との比較(callgrind、タグを付ける前)

baseはv3.0.0-alpha.2(2d87575b0)、headは5191c8e83で、arm64のホストのLinuxのコンテナで測りました。

perf: N=20000 WARMUP=2000; Ir per request of nginx; WARN from 3.0%, FAIL from 5.0% on the number in the column judged: w/o GC/const (Ir/req minus the GC and the constant lookups that missed the cache), or w/o GC (minus the GC only) where the head clears the constant cache more often, or clears it and misses more lookups (const_clears, const_misses)
perf: units: stream_sleep = completed TCP session; other rows = HTTP request
perf: gems: 8 in both builds, 0 at different commits, 0 only in base, 0 only in head
perf: mruby: base 4.1.0, head 4.1.0; accepted differences (accepted.txt): none apply

scenario                   base Ir/req  head Ir/req  change  base w/o GC  head w/o GC  change w/o GC  base w/o GC/const  head w/o GC/const  change w/o GC/const     GC share        judged  result
hello                            11216        11216  -0.00%        10505        10505         -0.00%              10505              10505               -0.00%    6.3%/6.3%  w/o GC/const      ok
headers                          30068        30068  +0.00%        24883        24883         +0.00%              24883              24883               +0.00%  17.2%/17.2%  w/o GC/const      ok
var                              20364        20364  +0.00%        17327        17327         +0.00%              17327              17327               +0.00%  14.9%/14.9%  w/o GC/const      ok
filter                           14679        14679  +0.00%        12908        12909         +0.00%              12908              12909               +0.00%  12.1%/12.1%  w/o GC/const      ok
sleep                            15440        15439  -0.01%        14223        14222         -0.01%              14223              14222               -0.01%    7.9%/7.9%  w/o GC/const      ok
sub_request                      43813        43813  -0.00%        42153        42152         -0.00%              42153              42152               -0.00%    3.8%/3.8%  w/o GC/const      ok
file                             14302        14302  -0.00%        12532        12531         -0.00%              12532              12531               -0.00%  12.4%/12.4%  w/o GC/const      ok
proxy_plain_2k                   21812        21812  +0.00%        21812        21812         +0.00%              21812              21812               +0.00%    0.0%/0.0%  w/o GC/const      ok
proxy_plain_64k                  22124        22122  -0.01%        22124        22122         -0.01%              22124              22122               -0.01%    0.0%/0.0%  w/o GC/const      ok
proxy_stream_plain_50            54051        54053  +0.00%        54051        54053         +0.00%              54051              54053               +0.00%    0.0%/0.0%  w/o GC/const      ok
responses_stream_plain_50        55312        55339  +0.05%        55312        55339         +0.05%              55312              55339               +0.05%    0.0%/0.0%  w/o GC/const      ok
auth                             48121        48121  +0.00%        41933        41933         +0.00%              41933              41933               +0.00%  12.9%/12.9%  w/o GC/const      ok
route_json_2k                   106725       106725  -0.00%        98194        98194         -0.00%              98194              98194               -0.00%    8.0%/8.0%  w/o GC/const      ok
ruby_call_1                      10966        10966  +0.00%        10239        10239         +0.00%              10239              10239               +0.00%    6.6%/6.6%  w/o GC/const      ok
ruby_call_10                     41491        41491  -0.00%        34173        34172         -0.00%              34173              34172               -0.00%  17.6%/17.6%  w/o GC/const      ok
stream_sleep                     12234        12233  -0.01%        11290        11289         -0.01%              11290              11289               -0.01%    7.7%/7.7%  w/o GC/const      ok

これから入るもの

この後のプレリリースでは、2.xに無いAIゲートウェイの機能を足す予定です。どれも、このプレリリースにはまだありません。

  • すべてのworkerから使える共有辞書を足します。予算やレート制限のカウンターに使います。
  • ストリーミングで返るモデルの応答から、SSE(server-sent events)のイベントを1つずつ読むフィルタを足します。トークンの使用量を数えるときなどに使います。
  • 中継先を選び直す仕組みと、upstreamから最初のバイトを受け取るまでのタイムアウトを足します。モデルAPIのフォールバックに使います。
  • これらの機能で作る、リファレンス実装のエージェントプロキシを足します。

機能とその順序は、v3の計画のステップ7にあります。これらの機能を使う用途には、MCPのツール呼び出しの認可、エージェントが短時間に繰り返す呼び出しの制限、モデルAPIのフォールバック、AIクローラーの制御などがあります。用途ごとの設計の状態は、トップページと用途の一覧にあります。

更新前に確認する挙動の変更

3.0.0-alpha.2から追加された項目を写します。上に書いたボディフィルタの修正の項目です。3.0.0-alpha.2からのリリースノートの変更は、ほかに「Build and test changes」の#602の項目があり、nginx-testsを手元で流す形に#880が書き直しました。全80項目は、このタグのリリースノートにあります。

proxyのレスポンスの最後の空のバッファで、mrubyのボディフィルタが2回目に走らなくなった

  • 変更前:upstream(proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass、memcached_pass)からのレスポンスで、前段のフェーズのハンドラがNginx.rputsかNginx.echoの出力をmodule contextに残してDECLINEDしたあと、mruby_output_body_filter(_code)が、upstreamがボディのあとに送る空のlast bufferに対して2回目に走っていました。空のボディへの出力が非空になるボディフィルタ、たとえばf.bodyに前置きや後置きをするものは、この2回目でバイトを作り、そのバイトがレスポンスのContent-Lengthを超えて送られていました。
  • 変更後:ボディフィルタは、この空のlast bufferをコードを走らせずに次へ渡します。枠付けされたレスポンスの後ろには何も送られません。
  • 変更の理由:
    • 不具合:空のボディへの出力が非空になるボディフィルタが2回目に作り、レスポンスのContent-Lengthを超えて送られていたバイトです。keepaliveの接続では、クライアントがこのバイトを、その接続の次のレスポンスの先頭として読みます。詳細はGHSA-5qjv-293c-64f8にあります。
    • 前の挙動を残せなかった理由:2回目には変換するボディが無いので、作られたバイトは、すでに枠付けして送ったレスポンスの後ろに続くしかありませんでした。
    • ドキュメントが示していた挙動:docs/class_and_method/は、ボディフィルタが2回目に走ることを書いていません。docs/directives/も、レスポンスの後ろに出力が出ることを書いていません。
  • 影響の範囲:mruby_output_body_filter(_code)で空のボディへの出力が非空になるものがあり、Content-Lengthの分かるupstreamから配信され、前段のハンドラがNginx.rputsかNginx.echoの出力を残して、リクエストがdeclineして通り過ぎる負のコード(実際にはNginx::DECLINED)を返す場合です。空のボディを空のまま返すボディフィルタ(たとえばf.body = f.body.upcase)、静的ファイル、chunkedで送るレスポンスは変わりません。
  • 必要な対応:ありません。このリリースでモジュールをビルドし直してください。空のボディをガードしていたボディフィルタ(たとえばunless f.body.empty?)は、ガードを残しても外してもかまいません。