Skip to content

Commit

Permalink
Improve behaviour when all instances are failed
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Sep 11, 2024
1 parent 3fef7c7 commit 9671def
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkcs11/src/backend/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,24 @@ impl LoginCtx {
}

fn next_instance(&self) -> &InstanceData {
for _ in 0..self.slot.instances.len() {
let index = self.slot.instance_balancer.fetch_add(1, Relaxed);
let index = index % self.slot.instances.len();
let instance = &self.slot.instances[index];
let index = self.slot.instance_balancer.fetch_add(1, Relaxed);
let index = index % self.slot.instances.len();
let instance = &self.slot.instances[index];
match instance.should_try() {
InstanceAttempt::Failed => {}
InstanceAttempt::Working | InstanceAttempt::Retry => return instance,
}
for i in 0..self.slot.instances.len() - 1 {
let instance = &self.slot.instances[index + i];

match instance.should_try() {
InstanceAttempt::Failed => continue,
InstanceAttempt::Working | InstanceAttempt::Retry => return instance,
InstanceAttempt::Working | InstanceAttempt::Retry => {
// This not true round-robin in case of multithreaded acces
// This is degraded mode so best-effort is attempted at best
self.slot.instance_balancer.fetch_add(i, Relaxed);
return instance;
}
}
}

Expand Down Expand Up @@ -255,7 +265,10 @@ impl LoginCtx {
retry_count += 1;
let api_call_clone = api_call.clone();
match api_call_clone(&instance.config) {
Ok(result) => return Ok(result),
Ok(result) => {
instance.clear_failed();
return Ok(result);
}

// If the server is in an unusable state, skip retries and try the next one
Err(apis::Error::ResponseError(err @ ResponseContent { status: 500, .. }))
Expand Down

0 comments on commit 9671def

Please sign in to comment.