1- use std:: cell:: Cell ;
2-
31use nanorand:: { Rng , WyRand } ;
4- use ntex:: util:: { Bytes , BytesVec } ;
2+ use ntex:: util:: Bytes ;
53use smallvec:: SmallVec ;
64use tokio_postgres:: { connect, Client , Statement } ;
75use yarte:: TemplateBytesTrait ;
@@ -29,11 +27,10 @@ pub struct FortunesTemplate<'a, 'b> {
2927/// Postgres interface
3028pub struct PgConnection {
3129 cl : Client ,
30+ rng : WyRand ,
3231 fortune : Statement ,
3332 world : Statement ,
34- rng : WyRand ,
3533 updates : Statement ,
36- buf : Cell < Option < BytesVec > > ,
3734}
3835
3936impl PgConnection {
@@ -51,33 +48,30 @@ impl PgConnection {
5148
5249 PgConnection {
5350 cl,
54- fortune,
5551 world,
5652 updates,
53+ fortune,
5754 rng : WyRand :: new ( ) ,
58- buf : Cell :: new ( Some ( BytesVec :: with_capacity ( 10 * 1024 * 1024 ) ) ) ,
5955 }
6056 }
6157}
6258
6359impl PgConnection {
6460 pub async fn get_world ( & self ) -> Bytes {
6561 let random_id = ( self . rng . clone ( ) . generate :: < u32 > ( ) % 10_000 + 1 ) as i32 ;
66-
6762 let row = self . cl . query_one ( & self . world , & [ & random_id] ) . await . unwrap ( ) ;
6863
69- let mut body = self . buf . take ( ) . unwrap ( ) ;
70- sonic_rs:: to_writer (
71- utils:: BVecWriter :: new ( & mut body) ,
72- & World {
73- id : row. get ( 0 ) ,
74- randomnumber : row. get ( 1 ) ,
75- } ,
76- )
77- . unwrap ( ) ;
78- let result = body. take_bytes ( ) ;
79- self . buf . set ( Some ( body) ) ;
80- result
64+ utils:: buffer ( 256 , |body| {
65+ sonic_rs:: to_writer (
66+ utils:: BVecWriter ( body) ,
67+ & World {
68+ id : row. get ( 0 ) ,
69+ randomnumber : row. get ( 1 ) ,
70+ } ,
71+ )
72+ . unwrap ( ) ;
73+ body. take ( )
74+ } )
8175 }
8276
8377 pub async fn get_worlds ( & self , num : usize ) -> Bytes {
@@ -97,11 +91,10 @@ impl PgConnection {
9791 } )
9892 }
9993
100- let mut body = self . buf . take ( ) . unwrap ( ) ;
101- sonic_rs:: to_writer ( utils:: BVecWriter :: new ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
102- let result = body. take_bytes ( ) ;
103- self . buf . set ( Some ( body) ) ;
104- result
94+ utils:: buffer ( 2 * 1024 , |body| {
95+ sonic_rs:: to_writer ( utils:: BVecWriter ( body) , & worlds[ ..] ) . unwrap ( ) ;
96+ body. take ( )
97+ } )
10598 }
10699
107100 pub async fn update ( & self , num : usize ) -> Bytes {
@@ -130,14 +123,12 @@ impl PgConnection {
130123 for query in queries {
131124 let _rand: i32 = query. await . unwrap ( ) . get ( 1 ) ;
132125 }
133-
134126 update. await . unwrap ( ) ;
135127
136- let mut body = self . buf . take ( ) . unwrap ( ) ;
137- sonic_rs:: to_writer ( utils:: BVecWriter :: new ( & mut body) , & worlds[ ..] ) . unwrap ( ) ;
138- let result = body. take_bytes ( ) ;
139- self . buf . set ( Some ( body) ) ;
140- result
128+ utils:: buffer ( 2 * 1024 , |body| {
129+ sonic_rs:: to_writer ( utils:: BVecWriter ( body) , & worlds[ ..] ) . unwrap ( ) ;
130+ body. take ( )
131+ } )
141132 }
142133
143134 pub async fn tell_fortune ( & self ) -> Bytes {
@@ -154,16 +145,13 @@ impl PgConnection {
154145 } ) ) ;
155146 fortunes. sort_by ( |it, next| it. message . cmp ( & next. message ) ) ;
156147
157- let mut body = self . buf . take ( ) . unwrap ( ) ;
158- utils:: reserve ( & mut body, 4 * 1024 ) ;
159- FortunesTemplate {
160- fortunes : & fortunes,
161- }
162- . write_call ( & mut body) ;
163- fortunes. clear ( ) ;
148+ utils:: buffer ( 4 * 1024 , |body| {
149+ FortunesTemplate {
150+ fortunes : & fortunes,
151+ }
152+ . write_call ( body) ;
164153
165- let result = body. take_bytes ( ) ;
166- self . buf . set ( Some ( body) ) ;
167- result
154+ body. take ( )
155+ } )
168156 }
169157}
0 commit comments