(
&mut self,
command: common::ClientCommands,
key: u64,
value: Option<u64>,
)
| 61 | } |
| 62 | |
| 63 | pub async fn execute_command( |
| 64 | &mut self, |
| 65 | command: common::ClientCommands, |
| 66 | key: u64, |
| 67 | value: Option<u64>, |
| 68 | ) -> std::result::Result<u64, ClientApiError> { |
| 69 | debug!("recevied command = {:?}", &command); |
| 70 | let mut retries = 0; |
| 71 | loop { |
| 72 | // Handle subcommands |
| 73 | match command { |
| 74 | ClientCommands::Put => { |
| 75 | let value = value.unwrap(); |
| 76 | |
| 77 | info!("put {}:{}", key, value); |
| 78 | match self.client.put(safe_kv_bytes(key), safe_kv_bytes(value)).await { |
| 79 | Ok(res) => { |
| 80 | debug!("Put Success: {:?}", res); |
| 81 | return Ok(key); |
| 82 | } |
| 83 | Err(e) if e.code() == ErrorCode::NotLeader && retries < MAX_RETRIES => { |
| 84 | retries += 1; |
| 85 | self.refresh_client().await?; |
| 86 | |
| 87 | sleep(Duration::from_millis(RETRY_DELAY_MS * 2u64.pow(retries))).await; |
| 88 | } |
| 89 | Err(e) |
| 90 | if e.code() == ErrorCode::ConnectionTimeout |
| 91 | && retries < MAX_RETRIES => |
| 92 | { |
| 93 | retries += 1; |
| 94 | |
| 95 | sleep(Duration::from_millis(RETRY_DELAY_MS * 2u64.pow(retries))).await; |
| 96 | } |
| 97 | Err(e) => { |
| 98 | error!("ClientCommands::Put, ErrorCode = {:?}", e.code()); |
| 99 | return Err(e); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | ClientCommands::Delete => match self.client.delete(safe_kv_bytes(key)).await { |
| 104 | Ok(res) => { |
| 105 | debug!("Delete Success: {:?}", res); |
| 106 | return Ok(key); |
| 107 | } |
| 108 | Err(e) if e.code() == ErrorCode::NotLeader && retries < MAX_RETRIES => { |
| 109 | retries += 1; |
| 110 | self.refresh_client().await?; |
| 111 | |
| 112 | sleep(Duration::from_millis(RETRY_DELAY_MS * 2u64.pow(retries))).await; |
| 113 | } |
| 114 | Err(e) if e.code() == ErrorCode::ConnectionTimeout && retries < MAX_RETRIES => { |
| 115 | retries += 1; |
| 116 | |
| 117 | sleep(Duration::from_millis(RETRY_DELAY_MS * 2u64.pow(retries))).await; |
| 118 | } |
| 119 | Err(e) => { |
| 120 | error!("Error: {:?}", e); |
no test coverage detected