55
66use buffer:: LazyBuffer ;
77use conn:: Connection ;
8- use sqlite_vfs:: register;
8+ use reqwest:: Client ;
9+ use sqlite_vfs:: { register, RegisterError } ;
910use std:: sync:: { Arc , Once , RwLock } ;
1011use utils:: AtomicRuntime ;
1112use vfs:: HttpVfs ;
@@ -16,6 +17,8 @@ pub const HTTP_VFS: &str = "http";
1617pub struct HttpVfsRegister {
1718 /// how many pages in block, default is 8MB, 2048 pages
1819 block_size : usize ,
20+ /// default client
21+ client : Option < Client > ,
1922 /// read the first few pages of each block without downloading the entire block
2023 download_threshold : usize ,
2124 /// sqlite's page size is 4KB by default
@@ -25,6 +28,7 @@ pub struct HttpVfsRegister {
2528impl HttpVfsRegister {
2629 pub fn new ( ) -> Self {
2730 Self {
31+ client : None ,
2832 block_size : SQLITE_PAGE_SIZE * 1024 * 2 ,
2933 download_threshold : 0 ,
3034 page_size : SQLITE_PAGE_SIZE ,
@@ -38,6 +42,13 @@ impl HttpVfsRegister {
3842 }
3943 }
4044
45+ pub fn with_client ( self , client : Client ) -> Self {
46+ Self {
47+ client : Some ( client) ,
48+ ..self
49+ }
50+ }
51+
4152 /// Set how many page read don't download full block
4253 pub fn with_download_threshold ( self , page_num : usize ) -> Self {
4354 Self {
@@ -50,21 +61,33 @@ impl HttpVfsRegister {
5061 Self { page_size, ..self }
5162 }
5263
53- pub fn register ( self ) {
54- const ONCE : Once = Once :: new ( ) ;
55-
64+ pub fn register ( self ) -> Result < ( ) , RegisterError > {
5665 let vfs_instance = HttpVfs {
66+ client : self . client ,
5767 block_size : self . block_size ,
5868 download_threshold : self . download_threshold ,
5969 } ;
60-
61- ONCE . call_once ( || {
62- let _ = register ( HTTP_VFS , vfs_instance, true ) ;
63- } )
70+ register ( HTTP_VFS , vfs_instance, true )
6471 }
6572}
6673
74+ /// register http vfs, use `Once` internally to ensure only register once
6775#[ inline( always) ]
6876pub fn register_http_vfs ( ) {
69- HttpVfsRegister :: new ( ) . register ( ) ;
77+ const ONCE : Once = Once :: new ( ) ;
78+
79+ ONCE . call_once ( || {
80+ let _ = HttpVfsRegister :: new ( ) . register ( ) ;
81+ } )
82+ }
83+
84+ /// register http vfs with custom client
85+ /// use `Once` internally to ensure only register once
86+ #[ inline( always) ]
87+ pub fn register_http_vfs_with_custom ( cb : impl FnOnce ( HttpVfsRegister ) -> HttpVfsRegister ) {
88+ const ONCE : Once = Once :: new ( ) ;
89+
90+ ONCE . call_once ( || {
91+ let _ = cb ( HttpVfsRegister :: new ( ) ) . register ( ) ;
92+ } )
7093}
0 commit comments