Closed iwanbk closed 2 months ago
What are you trying to accomplish? We have a BackgroundService
you can use to run background services and expose them to pingora.
tokio::main
is just syntax sugar for
fn main() {
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// your code
})
}
So you can do that instead if you need to run async code before starting the pingora server.
What are you trying to accomplish?
@andrewhavck i need to initialize a redis client during startup, so it can be used on each request
So you can do that instead if you need to run async code before starting the pingora server.
Thanks @eaufavor , it answers my questions 👍
i need to initialize a redis client during startup, so it can be used on each request
FYI as mentioned here if you start pingora with the daemonize flag it will fork()
on run_forever()
and Tokio runtimes created before then will likely not work.
FYI as mentioned here if you start pingora with the daemonize flag it will fork() on run_forever() and Tokio runtimes created before then will https://github.com/tokio-rs/tokio/issues/4301.
I see, honestly i haven't tried the solution yet when i closed this issue, because i move to something else right now. So, the solution is using Background Service @andrewhavck ?
You can try to use like this:
#[tokio::main]
async fn run1() {
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/", get(root));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
fn run() -> Result<(), Box<dyn Error>> {
// init my server of pingora
std::thread::spawn(|| {
run1();
});
my_server.run_forever();
Ok(())
}
fn main() {
if let Err(err) = run() {
error!("{}", err.to_string());
}
}
I also find a way to run all service of pingora and I made a pull request, https://github.com/cloudflare/pingora/pull/175
So, the solution is using Background Service
I don't think so unless you need to query Redis out of band of making a request, on a fixed interval or something like that.
Is there a reason the Redis client needs to be initialized in a separate Runtime before Pingora starts? Can you initialize the client lazily and then invoke it later from the request filter?
This question has been stale for a week. It will be closed in an additional day if not updated.
This issue has been closed because it has been stalled with no activity.
@iwanbk I dont know if it is still valid for you, but I could get it to work using Tokios implementation of OnceCell
and its get_or_init
method in the upstream_peer
method. I am using the redis-rs
redis::aio::ConnectionManager
.
static REDIS_CLIENT: OnceCell<ConnectionManager> = OnceCell::const_new();
Describe the bug
i got this error when running the pingora app
Pingora info
Please include the following information about your environment:
Pingora version: 0.1.0 Rust version: 1.77 Operating system version: Mac OS Sonoma 14.4
Steps to reproduce
In my main, i need to initialize a (non pingora) client which need
await
. So, themain fn
need to becomeasync
and become like thisLooks like it is the one that create the problem.
Expected results
can run normally
Observed results
got above error
Additional context
My program need to talk to some backend service, hence the need to initialize the client.