Skip to content

Commit 68b6a1a

Browse files
committed
kv & sql: timeout options
1 parent 3232423 commit 68b6a1a

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/kv.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub enum KvError {
5757
pub struct Kv {
5858
pub package_id: PackageId,
5959
pub db: String,
60+
pub timeout: u64,
6061
}
6162

6263
impl Kv {
@@ -69,7 +70,7 @@ impl Kv {
6970
db: self.db.clone(),
7071
action: KvAction::Get { key },
7172
})?)
72-
.send_and_await_response(5)?;
73+
.send_and_await_response(self.timeout)?;
7374

7475
match res {
7576
Ok(Message::Response { body, .. }) => {
@@ -101,7 +102,7 @@ impl Kv {
101102
action: KvAction::Set { key, tx_id },
102103
})?)
103104
.blob_bytes(value)
104-
.send_and_await_response(5)?;
105+
.send_and_await_response(self.timeout)?;
105106

106107
match res {
107108
Ok(Message::Response { body, .. }) => {
@@ -126,7 +127,7 @@ impl Kv {
126127
db: self.db.clone(),
127128
action: KvAction::Delete { key, tx_id },
128129
})?)
129-
.send_and_await_response(5)?;
130+
.send_and_await_response(self.timeout)?;
130131

131132
match res {
132133
Ok(Message::Response { body, .. }) => {
@@ -151,7 +152,7 @@ impl Kv {
151152
db: self.db.clone(),
152153
action: KvAction::BeginTx,
153154
})?)
154-
.send_and_await_response(5)?;
155+
.send_and_await_response(self.timeout)?;
155156

156157
match res {
157158
Ok(Message::Response { body, .. }) => {
@@ -176,7 +177,7 @@ impl Kv {
176177
db: self.db.clone(),
177178
action: KvAction::Commit { tx_id },
178179
})?)
179-
.send_and_await_response(5)?;
180+
.send_and_await_response(self.timeout)?;
180181

181182
match res {
182183
Ok(Message::Response { body, .. }) => {
@@ -194,15 +195,17 @@ impl Kv {
194195
}
195196

196197
/// Opens or creates a kv db.
197-
pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Kv> {
198+
pub fn open(package_id: PackageId, db: &str, timeout: Option<u64>) -> anyhow::Result<Kv> {
199+
let timeout = timeout.unwrap_or(5);
200+
198201
let res = Request::new()
199202
.target(("our", "kv", "distro", "sys"))
200203
.body(serde_json::to_vec(&KvRequest {
201204
package_id: package_id.clone(),
202205
db: db.to_string(),
203206
action: KvAction::Open,
204207
})?)
205-
.send_and_await_response(5)?;
208+
.send_and_await_response(timeout)?;
206209

207210
match res {
208211
Ok(Message::Response { body, .. }) => {
@@ -212,6 +215,7 @@ pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Kv> {
212215
KvResponse::Ok => Ok(Kv {
213216
package_id,
214217
db: db.to_string(),
218+
timeout,
215219
}),
216220
KvResponse::Err { error } => Err(error.into()),
217221
_ => Err(anyhow::anyhow!("kv: unexpected response {:?}", response)),
@@ -222,15 +226,17 @@ pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Kv> {
222226
}
223227

224228
/// Removes and deletes a kv db.
225-
pub fn remove_db(package_id: PackageId, db: &str) -> anyhow::Result<()> {
229+
pub fn remove_db(package_id: PackageId, db: &str, timeout: Option<u64>) -> anyhow::Result<()> {
230+
let timeout = timeout.unwrap_or(5);
231+
226232
let res = Request::new()
227233
.target(("our", "kv", "distro", "sys"))
228234
.body(serde_json::to_vec(&KvRequest {
229235
package_id: package_id.clone(),
230236
db: db.to_string(),
231237
action: KvAction::RemoveDb,
232238
})?)
233-
.send_and_await_response(5)?;
239+
.send_and_await_response(timeout)?;
234240

235241
match res {
236242
Ok(Message::Response { body, .. }) => {

src/sqlite.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub enum SqliteError {
7979
pub struct Sqlite {
8080
pub package_id: PackageId,
8181
pub db: String,
82+
pub timeout: u64,
8283
}
8384

8485
impl Sqlite {
@@ -96,7 +97,7 @@ impl Sqlite {
9697
action: SqliteAction::Read { query },
9798
})?)
9899
.blob_bytes(serde_json::to_vec(&params)?)
99-
.send_and_await_response(5)?;
100+
.send_and_await_response(self.timeout)?;
100101

101102
match res {
102103
Ok(Message::Response { body, .. }) => {
@@ -141,7 +142,7 @@ impl Sqlite {
141142
action: SqliteAction::Write { statement, tx_id },
142143
})?)
143144
.blob_bytes(serde_json::to_vec(&params)?)
144-
.send_and_await_response(5)?;
145+
.send_and_await_response(self.timeout)?;
145146

146147
match res {
147148
Ok(Message::Response { body, .. }) => {
@@ -169,7 +170,7 @@ impl Sqlite {
169170
db: self.db.clone(),
170171
action: SqliteAction::BeginTx,
171172
})?)
172-
.send_and_await_response(5)?;
173+
.send_and_await_response(self.timeout)?;
173174

174175
match res {
175176
Ok(Message::Response { body, .. }) => {
@@ -197,7 +198,7 @@ impl Sqlite {
197198
db: self.db.clone(),
198199
action: SqliteAction::Commit { tx_id },
199200
})?)
200-
.send_and_await_response(5)?;
201+
.send_and_await_response(self.timeout)?;
201202

202203
match res {
203204
Ok(Message::Response { body, .. }) => {
@@ -218,15 +219,17 @@ impl Sqlite {
218219
}
219220

220221
/// Open or create sqlite database.
221-
pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Sqlite> {
222+
pub fn open(package_id: PackageId, db: &str, timeout: Option<u64>) -> anyhow::Result<Sqlite> {
223+
let timeout = timeout.unwrap_or(5);
224+
222225
let res = Request::new()
223226
.target(("our", "sqlite", "distro", "sys"))
224227
.body(serde_json::to_vec(&SqliteRequest {
225228
package_id: package_id.clone(),
226229
db: db.to_string(),
227230
action: SqliteAction::Open,
228231
})?)
229-
.send_and_await_response(5)?;
232+
.send_and_await_response(timeout)?;
230233

231234
match res {
232235
Ok(Message::Response { body, .. }) => {
@@ -236,6 +239,7 @@ pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Sqlite> {
236239
SqliteResponse::Ok => Ok(Sqlite {
237240
package_id,
238241
db: db.to_string(),
242+
timeout,
239243
}),
240244
SqliteResponse::Err { error } => Err(error.into()),
241245
_ => Err(anyhow::anyhow!(
@@ -249,15 +253,17 @@ pub fn open(package_id: PackageId, db: &str) -> anyhow::Result<Sqlite> {
249253
}
250254

251255
/// Remove and delete sqlite database.
252-
pub fn remove_db(package_id: PackageId, db: &str) -> anyhow::Result<()> {
256+
pub fn remove_db(package_id: PackageId, db: &str, timeout: Option<u64>) -> anyhow::Result<()> {
257+
let timeout = timeout.unwrap_or(5);
258+
253259
let res = Request::new()
254260
.target(("our", "sqlite", "distro", "sys"))
255261
.body(serde_json::to_vec(&SqliteRequest {
256262
package_id: package_id.clone(),
257263
db: db.to_string(),
258264
action: SqliteAction::RemoveDb,
259265
})?)
260-
.send_and_await_response(5)?;
266+
.send_and_await_response(timeout)?;
261267

262268
match res {
263269
Ok(Message::Response { body, .. }) => {

0 commit comments

Comments
 (0)