1
+ use std:: sync:: Arc ;
2
+
1
3
use crate :: error:: RPCError ;
2
4
use async_trait:: async_trait;
3
5
use ckb_indexer:: IndexerHandle ;
@@ -7,6 +9,7 @@ use ckb_jsonrpc_types::{
7
9
} ;
8
10
use jsonrpc_core:: Result ;
9
11
use jsonrpc_utils:: rpc;
12
+ use tokio:: sync:: Semaphore ;
10
13
11
14
/// RPC Module Indexer.
12
15
#[ rpc( openrpc) ]
@@ -394,7 +397,7 @@ pub trait IndexerRpc {
394
397
/// }
395
398
/// ```
396
399
#[ rpc( name = "get_cells" ) ]
397
- fn get_cells (
400
+ async fn get_cells (
398
401
& self ,
399
402
search_key : IndexerSearchKey ,
400
403
order : IndexerOrder ,
@@ -810,7 +813,7 @@ pub trait IndexerRpc {
810
813
/// }
811
814
/// ```
812
815
#[ rpc( name = "get_transactions" ) ]
813
- fn get_transactions (
816
+ async fn get_transactions (
814
817
& self ,
815
818
search_key : IndexerSearchKey ,
816
819
order : IndexerOrder ,
@@ -877,7 +880,7 @@ pub trait IndexerRpc {
877
880
/// }
878
881
/// ```
879
882
#[ rpc( name = "get_cells_capacity" ) ]
880
- fn get_cells_capacity (
883
+ async fn get_cells_capacity (
881
884
& self ,
882
885
search_key : IndexerSearchKey ,
883
886
) -> Result < Option < IndexerCellsCapacity > > ;
@@ -886,11 +889,15 @@ pub trait IndexerRpc {
886
889
#[ derive( Clone ) ]
887
890
pub ( crate ) struct IndexerRpcImpl {
888
891
pub ( crate ) handle : IndexerHandle ,
892
+ pub ( crate ) slow_query_parallelism : Arc < Semaphore > ,
889
893
}
890
894
891
895
impl IndexerRpcImpl {
892
- pub fn new ( handle : IndexerHandle ) -> Self {
893
- IndexerRpcImpl { handle }
896
+ pub fn new ( handle : IndexerHandle , slow_query_limit : usize ) -> Self {
897
+ IndexerRpcImpl {
898
+ handle,
899
+ slow_query_parallelism : Arc :: new ( Semaphore :: new ( slow_query_limit) ) ,
900
+ }
894
901
}
895
902
}
896
903
@@ -902,34 +909,43 @@ impl IndexerRpc for IndexerRpcImpl {
902
909
. map_err ( |e| RPCError :: custom ( RPCError :: Indexer , e) )
903
910
}
904
911
905
- fn get_cells (
912
+ async fn get_cells (
906
913
& self ,
907
914
search_key : IndexerSearchKey ,
908
915
order : IndexerOrder ,
909
916
limit : Uint32 ,
910
917
after : Option < JsonBytes > ,
911
918
) -> Result < IndexerPagination < IndexerCell > > {
919
+ let _permit = Arc :: clone ( & self . slow_query_parallelism )
920
+ . acquire_owned ( )
921
+ . await ;
912
922
self . handle
913
923
. get_cells ( search_key, order, limit, after)
914
924
. map_err ( |e| RPCError :: custom ( RPCError :: Indexer , e) )
915
925
}
916
926
917
- fn get_transactions (
927
+ async fn get_transactions (
918
928
& self ,
919
929
search_key : IndexerSearchKey ,
920
930
order : IndexerOrder ,
921
931
limit : Uint32 ,
922
932
after : Option < JsonBytes > ,
923
933
) -> Result < IndexerPagination < IndexerTx > > {
934
+ let _permit = Arc :: clone ( & self . slow_query_parallelism )
935
+ . acquire_owned ( )
936
+ . await ;
924
937
self . handle
925
938
. get_transactions ( search_key, order, limit, after)
926
939
. map_err ( |e| RPCError :: custom ( RPCError :: Indexer , e) )
927
940
}
928
941
929
- fn get_cells_capacity (
942
+ async fn get_cells_capacity (
930
943
& self ,
931
944
search_key : IndexerSearchKey ,
932
945
) -> Result < Option < IndexerCellsCapacity > > {
946
+ let _permit = Arc :: clone ( & self . slow_query_parallelism )
947
+ . acquire_owned ( )
948
+ . await ;
933
949
self . handle
934
950
. get_cells_capacity ( search_key)
935
951
. map_err ( |e| RPCError :: custom ( RPCError :: Indexer , e) )
0 commit comments