Replies: 1 comment
-
So, I have solved it by making a bit of compromise... Instead of insisting on using the same instance of a client, I am making new instance of a wrapper that holds actual instance of httpx client. Wrapper has implementation to clone itself to use same underlying client to reuse connection pool. I said compromise because this means that I can no longer manage event hooks the way I was before (since multiple wrapper instances share same underlying client and it has a set of hooks on them), but that is a different problem for which I have found a workaround. With this setup, I can inject wrapper instances as dependencies. If anyone has some better or smarter idea on how to approach this, please do respond. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I am in a process of evaluating
litestar
(and some other async frameworks). So far,litestar
sits best with me, but I do have a use case I am not sure how to solve.I currently have Flask application that I am looking to rewrite in
litestar
. In its work, this application issues requests to other services. While doing so, some of the headers in original request should be proxied to other services (not all, just configured ones).Trivially, endpoint handlers can do this manually by settings headers that are read from current request. However, I don't want to do it manually since it is boilerplate and can be forgotten. So, I am looking into a way to automate this.
Another trivial solution would be to create instance of a client on every request (e.g. as dependency) and populate it with header values. However, I want to be mindful of reusing TCP connection pool, persist cookies, etc (all the nice stuff explained here) so I am trying to reuse same client instance. This means that I need a way to modify existing instance in a scope of a current request and to do it in data-race free way. Currently client instances do not have any state to deal with.
Let's say I have an incoming header
X-Custom-Info
that I want to proxy to all the clients. I have clients written using HTTPX, but any library would fit question nicely (e.g. I had sync clients written usingrequests
and situation is the same, except thatrequests
calls this aSession
instead of aClient
).In Flask implementation, this is relatively easy thing to do since their
request
object is global and only the proxy to currently ongoing request. Here I could register a before_request hook to HTTPX client, which wouldfrom flask import request
and just read headers I want and modify HTTPX request.I am having trouble doing something similar with
litestar
and would appreciate some guidance.My approach to using the same client instance is by using dependency with
use_cache=True
inProvide
(I am open to other approaches, but this is convenient since I only want one instance and I am using nested DI to get application configuration to get URL and credentials for creating client instance). Than, I have installed a hook, but I have trouble accessing current request from the hook. One attempt was to uselitestar
before_request
to store current request in some global object and access it from the httpx hook. However, this can become ugly very quickly (dealing with potential data races, introducing probably ContextVars, copying context, not even sure how to do it safely from the top of my mind).Is there a mechanism in
litestar
that would make this possible in a sane way? I am fine writing quite complex code in order to automate this and avoid having to proxy headers manually.Any help is appreciated.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions