@@ -476,21 +476,40 @@ impl Database {
476
476
) -> Result < u32 > {
477
477
let mut nr_retries = 0 ;
478
478
loop {
479
- let client = reqwest:: Client :: new ( ) ;
480
- let mut builder = client. post ( uri. to_owned ( ) ) ;
479
+ // TODO(lucio): add custom connector + tls support here
480
+ let client = hyper:: client:: Client :: builder ( ) . build_http :: < hyper:: Body > ( ) ;
481
+
482
+ let mut req = http:: Request :: post ( uri. clone ( ) ) ;
483
+
481
484
match auth_token {
482
485
Some ( ref auth_token) => {
483
- builder = builder
484
- . header ( "Authorization" , format ! ( "Bearer {}" , auth_token. to_owned( ) ) ) ;
486
+ let auth_header =
487
+ http:: HeaderValue :: try_from ( format ! ( "Bearer {}" , auth_token. to_owned( ) ) )
488
+ . unwrap ( ) ;
489
+
490
+ req. headers_mut ( )
491
+ . expect ( "valid http request" )
492
+ . insert ( "Authorization" , auth_header) ;
485
493
}
486
494
None => { }
487
495
}
488
- let res = builder. body ( frame. to_vec ( ) ) . send ( ) . await . unwrap ( ) ;
496
+
497
+ // TODO(lucio): convert this to use bytes to make this clone cheap, it should be
498
+ // to possible use BytesMut when reading frames from the WAL and efficiently use Bytes
499
+ // from that.
500
+ let req = req. body ( frame. clone ( ) . into ( ) ) . expect ( "valid body" ) ;
501
+
502
+ let res = client. request ( req) . await . unwrap ( ) ;
503
+
504
+ // TODO(lucio): only retry on server side errors
489
505
if res. status ( ) . is_success ( ) {
490
- let resp = res. json :: < serde_json:: Value > ( ) . await . unwrap ( ) ;
506
+ let res_body = hyper:: body:: to_bytes ( res. into_body ( ) ) . await . unwrap ( ) ;
507
+ let resp = serde_json:: from_slice :: < serde_json:: Value > ( & res_body[ ..] ) . unwrap ( ) ;
508
+
491
509
let max_frame_no = resp. get ( "max_frame_no" ) . unwrap ( ) . as_u64 ( ) . unwrap ( ) ;
492
510
return Ok ( max_frame_no as u32 ) ;
493
511
}
512
+
494
513
if nr_retries > max_retries {
495
514
return Err ( crate :: errors:: Error :: ConnectionFailed ( format ! (
496
515
"Failed to push frame: {}" ,
0 commit comments