0

I use actix-web and want to generate pairs of (password, password hash).
It takes some time (0.5s).

Instead of generating each pair on demand:

pub async fn signup (data: web::Data<AppData>) -> impl Responder {
    // Generate password
    let password = data.password_generator.generate_one().unwrap();
    let password_hash = password::hash_encoded(password.as_bytes());  // 0.5s
    // ...
}

I want to always have 10-20 of them pre-generated and just get already existing pair. After that generate a new one in the background.

How can I do it using actix-web?

I think of some kind of refilling "queue". But don't know how to implement it and use it correctly in multiple actix threads.

Sergey
  • 19,487
  • 13
  • 44
  • 68

1 Answers1

1

You can just use a regular thread (as in std::thread::spawn): while actix probably has some sort of blocking facility which executes blocking functions off the scheduler, these are normally intended for blocking tasks which ultimately terminate. Here you want something which just lives forever. So an stdlib thread is exactly what you want.

Then set up a buffered, blocking channel between the two, either an mpsc or a crossbeam mpmc (the latter is more convenient because you can clone the endpoints). With the right buffering, the procuder thread just loops around producing entries, and once the channel is "full" the producer will get blocked on sending the extra entry (21st or whatever).

Once a consumer fetches an entry from the channel, the producer will be unblocked, add the new entry, generate a new one, and wait until it can enqueue that.

Masklinn
  • 34,759
  • 3
  • 38
  • 57