{"id":21970887,"url":"https://github.com/kingluo/lua-resty-ffi-grpc","last_synced_at":"2025-04-28T11:41:14.894Z","repository":{"id":185026107,"uuid":"631131710","full_name":"kingluo/lua-resty-ffi-grpc","owner":"kingluo","description":"openresty grpc client library based on rust tonic","archived":false,"fork":false,"pushed_at":"2023-04-22T07:47:18.000Z","size":52,"stargazers_count":20,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T09:11:36.971Z","etag":null,"topics":["ffi","grpc-client","luajit","nginx","openresty","rust","tonic"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kingluo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-04-22T03:10:41.000Z","updated_at":"2024-11-10T04:04:05.000Z","dependencies_parsed_at":"2023-07-31T12:49:48.042Z","dependency_job_id":null,"html_url":"https://github.com/kingluo/lua-resty-ffi-grpc","commit_stats":null,"previous_names":["kingluo/lua-resty-ffi-grpc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingluo%2Flua-resty-ffi-grpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingluo%2Flua-resty-ffi-grpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingluo%2Flua-resty-ffi-grpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingluo%2Flua-resty-ffi-grpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kingluo","download_url":"https://codeload.github.com/kingluo/lua-resty-ffi-grpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251306656,"owners_count":21568308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ffi","grpc-client","luajit","nginx","openresty","rust","tonic"],"created_at":"2024-11-29T14:43:57.228Z","updated_at":"2025-04-28T11:41:14.871Z","avatar_url":"https://github.com/kingluo.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lua-resty-ffi-grpc\n\nopenresty grpc client library based on rust [tonic](https://github.com/hyperium/tonic).\n\n**It works for all versions of openresty, and it's a non-intrusive library without need to re-compile openresty.**\n\n## Background\n\nhttp://luajit.io/posts/implement-grpc-client-in-rust-for-openresty/\n\n[GRPC](https://en.wikipedia.org/wiki/GRPC) is an important RPC protocol, especially for k8s (cloud native env).\n\nBut OpenResty does not have a GRPC client library, especially in non-intrusive way\n(i.e. do not require re-compile the openresty due to C module implementation).\n\n\u003e tonic is a gRPC over HTTP/2 implementation focused on high performance, interoperability, and flexibility. This library was created to have first class support of async/await and to act as a core building block for production systems written in Rust.\n\nWhy not encapsulate tonic so that we could reuse it in openresty?\n\n[lua-resty-ffi](https://github.com/kingluo/lua-resty-ffi) provides an efficient and generic API to do hybrid programming\nin openresty with mainstream languages (Go, Python, Java, Rust, Nodejs).\n\n**lua-resty-ffi-grpc = lua-resty-ffi + tonic**\n\n## Synopsis\n\n```lua\nlocal grpc = require(\"resty.ffi.grpc\")\n\ngrpc.loadfile(\"helloworld.proto\")\ngrpc.loadfile(\"route_guide.proto\")\ngrpc.loadfile(\"echo.proto\")\n\n--\n-- unary call\n--\nlocal ok, conn = grpc.connect(\"[::1]:50051\")\nlocal ok, res = conn:unary(\n    \"/helloworld.Greeter/SayHello\",\n    {name = \"foobar\"}\n)\nassert(ok)\nassert(res.message == \"Hello foobar!\")\n\n--\n-- tls/mtls enabled unary call\n--\nlocal ok, conn = grpc.connect(\"[::1]:50051\",\n    {\n        host = \"example.com\",\n        ca = grpc.readfile(\"/opt/tonic/examples/data/tls/ca.pem\"),\n        cert = grpc.readfile(\"/opt/tonic/examples/data/tls/client1.pem\"),\n        priv_key = grpc.readfile(\"/opt/tonic/examples/data/tls/client1.key\"),\n    }\n)\nassert(ok)\n\nlocal ok, res = conn:unary(\n    \"/grpc.examples.echo.Echo/UnaryEcho\",\n    {message = \"hello\"}\n)\nassert(ok)\nassert(res.message == \"hello\")\n\n--\n-- A server-to-client streaming RPC.\n--\nlocal ok, stream = conn:new_stream(\"/routeguide.RouteGuide/ListFeatures\")\nassert(ok)\n\nlocal rectangle = {\n    lo = {latitude = 400000000, longitude = -750000000},\n    hi = {latitude = 420000000, longitude = -730000000},\n}\nlocal ok = stream:send(rectangle)\nassert(ok)\n\nlocal ok = stream:close_send()\nassert(ok)\n\nwhile true do\n    local ok, res = stream:recv()\n    assert(ok)\n    if not res then\n        break\n    end\n    ngx.say(cjson.encode(res))\nend\n\n--\n-- A client-to-server streaming RPC.\n--\nlocal ok, stream = conn:new_stream(\"/routeguide.RouteGuide/RecordRoute\")\nassert(ok)\n\nfor i=1,3 do\n    local point = {latitude = 409146138 + i*100, longitude = -746188906 + i*50}\n    local ok = stream:send(point)\n    assert(ok)\nend\n\nlocal ok = stream:close_send()\nassert(ok)\n\nlocal ok, res = stream:recv()\nassert(ok)\nngx.say(cjson.encode(res))\n\nlocal ok, res = stream:recv()\nassert(ok and not res)\n\nlocal ok = stream:close()\nassert(ok)\n\nlocal ok = conn:close()\nassert(ok)\n\n--\n-- A Bidirectional streaming RPC.\n--\nlocal ok, stream = conn:new_stream(\"/routeguide.RouteGuide/RouteChat\")\nassert(ok)\n\nfor i=1,3 do\n    local note = {\n        location = {latitude = 409146138 + i*100, longitude = -746188906 + i*50},\n        message = string.format(\"note-%d\", i),\n    }\n    local ok = stream:send(note)\n    assert(ok)\n\n    local ok, res = stream:recv()\n    assert(ok)\n    ngx.say(cjson.encode(res))\nend\n\nlocal ok = stream:close()\nassert(ok)\n\nlocal ok = conn:close()\nassert(ok)\n```\n\n## Demo\n\n```bash\n# prepare the toolchain\n# refer to https://github.com/kingluo/lua-resty-ffi/blob/main/build.sh\n# for centos7\nsource scl_source enable devtoolset-9\n# for centos8\nsource /opt/rh/gcc-toolset-9/enable\n# for ubuntu or debian\napt install build-essential\n\n# install lua-resty-ffi\n# https://github.com/kingluo/lua-resty-ffi#install-lua-resty-ffi-via-luarocks\n# set `OR_SRC` to your openresty source path\nluarocks config variables.OR_SRC /tmp/tmp.Z2UhJbO1Si/openresty-1.21.4.1\nluarocks install lua-resty-ffi\n\n# Install protoc-3 if not yet\n# https://grpc.io/docs/protoc-installation/\nPB_REL=\"https://github.com/protocolbuffers/protobuf/releases\"\ncurl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip\nunzip protoc-3.15.8-linux-x86_64.zip -d /usr/local\n\n# install lua-protobuf\nluarocks install lua-protobuf\n\n# install rust if not yet\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\nsource \"$HOME/.cargo/env\"\n\ncd /opt\ngit clone https://github.com/kingluo/lua-resty-ffi-grpc\n\n# build libgrpc_client.so\ncd /opt/lua-resty-ffi-grpc/grpc_client\ncargo build --release\n\n# run nginx\ncd /opt/lua-resty-ffi-grpc/demo\nmkdir logs\nLD_LIBRARY_PATH=/opt/lua-resty-ffi-grpc/grpc_client/target/release:/usr/local/lib/lua/5.1 \\\nnginx -p $PWD -c nginx.conf\n\n# run tonic helloworld-server\ncd /opt\ngit clone https://github.com/hyperium/tonic\ncd /opt/tonic\ncargo run --release --bin helloworld-server\n\n# hello world\ncurl localhost:20000/say_hello\nok\n\n# run tonic tls-client-auth-server\ncd /opt/tonic\ncargo run --release --bin tls-client-auth-server\n\n# mtls\ncurl localhost:20000/tls\nok\n```\n\n## Benchmark\n\n### ffi overhead\n\nThe most important question is, since we wrap the tonic, so how much is the overhead?\n\nI use AWS EC2 t2.medium (Ubuntu 22.04) to do the benchmark.\n\n![grpc client benchmark, overhead](grpc_client_benchmark.png)\n\nSend 100,000 calls, use lua-resty-ffi and tonic helloworld client example respectively.\n\nThe result is in seconds, lower is better.\n\nYou could see that the overhead is 20% to 30%, depending the CPU affinity. No too bad, right?\n\n### How about [grpc-client-nginx-module](https://github.com/api7/grpc-client-nginx-module)?\n\n![grpc client benchmark](grpc_client_benchmark2.png)\n\n**lower is better**.\n\n`affinity` means CPU affinity of client program set by `taskset`:\n\n* `affinity 0,1`: bound to both CPU-0 and CPU-1\n* `affinity 1`: bound to CPU-1, then client and server runs in seperate CPU exclusively\n* `affinity 0`: client and server runs in the same CPU-0\n\nThanks to the [high efficient IPC design](https://github.com/kingluo/lua-resty-ffi/blob/main/benchmark.md), lua-resty-ffi-grpc is the better. In fact, grpc-go and tonic are almost the same performance, so the only difference is the overhead of encapsulation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingluo%2Flua-resty-ffi-grpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingluo%2Flua-resty-ffi-grpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingluo%2Flua-resty-ffi-grpc/lists"}