@@ -57,6 +57,7 @@ pub enum KvError {
5757pub struct Kv {
5858 pub package_id : PackageId ,
5959 pub db : String ,
60+ pub timeout : u64 ,
6061}
6162
6263impl 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, .. } ) => {
0 commit comments