dify icon indicating copy to clipboard operation
dify copied to clipboard

[Refactor/Chore] Reuse one HTTP client for enterprise API calls instead of building one per call

Open Theysua opened this issue 3 weeks ago • 1 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for refactors or chores; if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Description

BaseRequest.send_request in api/services/enterprise/base.py builds a new httpx.Client for every call and closes it afterwards:

with httpx.Client(mounts=mounts) as client:
    ...
    response = client.request(method, url, **request_kwargs)

Constructing the client is expensive, mostly because it builds a TLS context, and a new client can never reuse a connection. send_inner_rbac_request in the same file already keeps a process-local client (_get_rbac_client); send_request, which carries all other enterprise and plugin-manager calls, does not.

I propose keeping one process-local client per request class for send_request, with the same rules the RBAC client follows: connections are shared, credentials, headers and timeouts stay request-local, response cookies are rejected, and the pool owns shutdown.

Motivation

Measured inside the 1.19.0 API container against a local HTTP endpoint, 500 sequential calls, two runs each:

Time per call (median) CPU per call Calls per second
Current code 12.2–12.3 ms 12.8 ms 77
One client per request class 0.24 ms 0.21 ms about 4,000

Almost all of the cost is CPU, not network. The API runs as a gevent process, so those 13 ms block every other request in the process.

In enterprise deployments these calls sit on admission paths: controllers/web/wraps.py calls the enterprise service one to three times for each web app request (get_app_access_mode_by_id, is_user_allowed_to_access_webapp, and the SSO last-update lookups). That is 13–39 ms of blocked CPU per request before any work starts, and a ceiling of roughly 77 enterprise calls per second for one API process.

Additional Context

The endpoint in the measurement was a local stand-in, not a real enterprise service; the numbers describe the cost of the client handling itself. I will open a pull request with the change and tests.

Theysua avatar Nov 13 '26 08:11 Theysua

An end-to-end number for this, since the first measurement was of the client construction alone.

With the enterprise runtime credential check on, a chat message makes six of these requests (see #46440 for why six). Chat app, short answers, 10 requests in flight, 360 requests per row, the check answered by a stand-in that replies at once:

API CPU per request Latency p50 / p95 Requests per second
Check off (community behaviour) 45 ms 2.18 s / 2.48 s 4.47
Check on, current code 215 ms 2.73 s / 4.23 s 3.20
Check on, with #46246 57 ms 2.38 s / 2.63 s 4.15

So in the enterprise edition the per-call client is about three quarters of the CPU of a chat message, not a detail. With two knowledge bases the same request goes from 394 ms to 160 ms.

Theysua avatar Nov 15 '26 02:11 Theysua