@@ -531,7 +531,7 @@ impl<'conn> MasWriter<'conn> {
531
531
/// - If the database writer connection pool had an error.
532
532
#[ allow( clippy:: missing_panics_doc) ] // not a real panic
533
533
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
534
- pub async fn write_users ( & mut self , users : Vec < MasNewUser > ) -> Result < ( ) , Error > {
534
+ pub fn write_users ( & mut self , users : Vec < MasNewUser > ) -> BoxFuture < ' _ , Result < ( ) , Error > > {
535
535
self . writer_pool . spawn_with_connection ( move |conn| Box :: pin ( async move {
536
536
// `UNNEST` is a fast way to do bulk inserts, as it lets us send multiple rows in one statement
537
537
// without having to change the statement SQL thus altering the query plan.
@@ -577,7 +577,7 @@ impl<'conn> MasWriter<'conn> {
577
577
) . execute ( & mut * conn) . await . into_database ( "writing users to MAS" ) ?;
578
578
579
579
Ok ( ( ) )
580
- } ) ) . await
580
+ } ) ) . boxed ( )
581
581
}
582
582
583
583
/// Write a batch of user passwords to the database.
@@ -589,14 +589,10 @@ impl<'conn> MasWriter<'conn> {
589
589
/// - If the database writer connection pool had an error.
590
590
#[ allow( clippy:: missing_panics_doc) ] // not a real panic
591
591
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
592
- pub async fn write_passwords (
592
+ pub fn write_passwords (
593
593
& mut self ,
594
594
passwords : Vec < MasNewUserPassword > ,
595
- ) -> Result < ( ) , Error > {
596
- if passwords. is_empty ( ) {
597
- return Ok ( ( ) ) ;
598
- }
599
-
595
+ ) -> BoxFuture < ' _ , Result < ( ) , Error > > {
600
596
self . writer_pool . spawn_with_connection ( move |conn| Box :: pin ( async move {
601
597
let mut user_password_ids: Vec < Uuid > = Vec :: with_capacity ( passwords. len ( ) ) ;
602
598
let mut user_ids: Vec < Uuid > = Vec :: with_capacity ( passwords. len ( ) ) ;
@@ -631,17 +627,14 @@ impl<'conn> MasWriter<'conn> {
631
627
) . execute ( & mut * conn) . await . into_database ( "writing users to MAS" ) ?;
632
628
633
629
Ok ( ( ) )
634
- } ) ) . await
630
+ } ) ) . boxed ( )
635
631
}
636
632
637
633
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
638
- pub async fn write_email_threepids (
634
+ pub fn write_email_threepids (
639
635
& mut self ,
640
636
threepids : Vec < MasNewEmailThreepid > ,
641
- ) -> Result < ( ) , Error > {
642
- if threepids. is_empty ( ) {
643
- return Ok ( ( ) ) ;
644
- }
637
+ ) -> BoxFuture < ' _ , Result < ( ) , Error > > {
645
638
self . writer_pool . spawn_with_connection ( move |conn| {
646
639
Box :: pin ( async move {
647
640
let mut user_email_ids: Vec < Uuid > = Vec :: with_capacity ( threepids. len ( ) ) ;
@@ -678,17 +671,14 @@ impl<'conn> MasWriter<'conn> {
678
671
679
672
Ok ( ( ) )
680
673
} )
681
- } ) . await
674
+ } ) . boxed ( )
682
675
}
683
676
684
677
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
685
- pub async fn write_unsupported_threepids (
678
+ pub fn write_unsupported_threepids (
686
679
& mut self ,
687
680
threepids : Vec < MasNewUnsupportedThreepid > ,
688
- ) -> Result < ( ) , Error > {
689
- if threepids. is_empty ( ) {
690
- return Ok ( ( ) ) ;
691
- }
681
+ ) -> BoxFuture < ' _ , Result < ( ) , Error > > {
692
682
self . writer_pool . spawn_with_connection ( move |conn| {
693
683
Box :: pin ( async move {
694
684
let mut user_ids: Vec < Uuid > = Vec :: with_capacity ( threepids. len ( ) ) ;
@@ -723,17 +713,14 @@ impl<'conn> MasWriter<'conn> {
723
713
724
714
Ok ( ( ) )
725
715
} )
726
- } ) . await
716
+ } ) . boxed ( )
727
717
}
728
718
729
719
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
730
720
pub fn write_upstream_oauth_links (
731
721
& mut self ,
732
722
links : Vec < MasNewUpstreamOauthLink > ,
733
723
) -> BoxFuture < ' _ , Result < ( ) , Error > > {
734
- if links. is_empty ( ) {
735
- return async { Ok ( ( ) ) } . boxed ( ) ;
736
- }
737
724
self . writer_pool . spawn_with_connection ( move |conn| {
738
725
Box :: pin ( async move {
739
726
let mut link_ids: Vec < Uuid > = Vec :: with_capacity ( links. len ( ) ) ;
@@ -783,124 +770,6 @@ impl<'conn> MasWriter<'conn> {
783
770
// stream to two tables at once...)
784
771
const WRITE_BUFFER_BATCH_SIZE : usize = 4096 ;
785
772
786
- // TODO replace with just `MasWriteBuffer`
787
- pub struct MasUserWriteBuffer < ' writer , ' conn > {
788
- users : Vec < MasNewUser > ,
789
- passwords : Vec < MasNewUserPassword > ,
790
- writer : & ' writer mut MasWriter < ' conn > ,
791
- }
792
-
793
- impl < ' writer , ' conn > MasUserWriteBuffer < ' writer , ' conn > {
794
- pub fn new ( writer : & ' writer mut MasWriter < ' conn > ) -> Self {
795
- MasUserWriteBuffer {
796
- users : Vec :: with_capacity ( WRITE_BUFFER_BATCH_SIZE ) ,
797
- passwords : Vec :: with_capacity ( WRITE_BUFFER_BATCH_SIZE ) ,
798
- writer,
799
- }
800
- }
801
-
802
- pub async fn finish ( mut self ) -> Result < ( ) , Error > {
803
- self . flush_users ( ) . await ?;
804
- self . flush_passwords ( ) . await ?;
805
- Ok ( ( ) )
806
- }
807
-
808
- pub async fn flush_users ( & mut self ) -> Result < ( ) , Error > {
809
- // via copy: 13s
810
- // not via copy: 14s
811
- // difference probably gets worse with latency
812
- self . writer
813
- . write_users ( std:: mem:: take ( & mut self . users ) )
814
- . await ?;
815
-
816
- self . users . reserve_exact ( WRITE_BUFFER_BATCH_SIZE ) ;
817
- Ok ( ( ) )
818
- }
819
-
820
- pub async fn flush_passwords ( & mut self ) -> Result < ( ) , Error > {
821
- self . writer
822
- . write_passwords ( std:: mem:: take ( & mut self . passwords ) )
823
- . await ?;
824
- self . passwords . reserve_exact ( WRITE_BUFFER_BATCH_SIZE ) ;
825
-
826
- Ok ( ( ) )
827
- }
828
-
829
- pub async fn write_user ( & mut self , user : MasNewUser ) -> Result < ( ) , Error > {
830
- self . users . push ( user) ;
831
- if self . users . len ( ) >= WRITE_BUFFER_BATCH_SIZE {
832
- self . flush_users ( ) . await ?;
833
- }
834
- Ok ( ( ) )
835
- }
836
-
837
- pub async fn write_password ( & mut self , password : MasNewUserPassword ) -> Result < ( ) , Error > {
838
- self . passwords . push ( password) ;
839
- if self . passwords . len ( ) >= WRITE_BUFFER_BATCH_SIZE {
840
- self . flush_passwords ( ) . await ?;
841
- }
842
- Ok ( ( ) )
843
- }
844
- }
845
-
846
- // TODO replace with just `MasWriteBuffer`
847
- pub struct MasThreepidWriteBuffer < ' writer , ' conn > {
848
- email : Vec < MasNewEmailThreepid > ,
849
- unsupported : Vec < MasNewUnsupportedThreepid > ,
850
- writer : & ' writer mut MasWriter < ' conn > ,
851
- }
852
-
853
- impl < ' writer , ' conn > MasThreepidWriteBuffer < ' writer , ' conn > {
854
- pub fn new ( writer : & ' writer mut MasWriter < ' conn > ) -> Self {
855
- MasThreepidWriteBuffer {
856
- email : Vec :: with_capacity ( WRITE_BUFFER_BATCH_SIZE ) ,
857
- unsupported : Vec :: with_capacity ( WRITE_BUFFER_BATCH_SIZE ) ,
858
- writer,
859
- }
860
- }
861
-
862
- pub async fn finish ( mut self ) -> Result < ( ) , Error > {
863
- self . flush_emails ( ) . await ?;
864
- self . flush_unsupported ( ) . await ?;
865
- Ok ( ( ) )
866
- }
867
-
868
- pub async fn flush_emails ( & mut self ) -> Result < ( ) , Error > {
869
- self . writer
870
- . write_email_threepids ( std:: mem:: take ( & mut self . email ) )
871
- . await ?;
872
- self . email . reserve_exact ( WRITE_BUFFER_BATCH_SIZE ) ;
873
- Ok ( ( ) )
874
- }
875
-
876
- pub async fn flush_unsupported ( & mut self ) -> Result < ( ) , Error > {
877
- self . writer
878
- . write_unsupported_threepids ( std:: mem:: take ( & mut self . unsupported ) )
879
- . await ?;
880
- self . unsupported . reserve_exact ( WRITE_BUFFER_BATCH_SIZE ) ;
881
- Ok ( ( ) )
882
- }
883
-
884
- pub async fn write_email ( & mut self , user : MasNewEmailThreepid ) -> Result < ( ) , Error > {
885
- self . email . push ( user) ;
886
- if self . email . len ( ) >= WRITE_BUFFER_BATCH_SIZE {
887
- self . flush_emails ( ) . await ?;
888
- }
889
- Ok ( ( ) )
890
- }
891
-
892
- pub async fn write_password (
893
- & mut self ,
894
- unsupported : MasNewUnsupportedThreepid ,
895
- ) -> Result < ( ) , Error > {
896
- self . unsupported . push ( unsupported) ;
897
- if self . unsupported . len ( ) >= WRITE_BUFFER_BATCH_SIZE {
898
- self . flush_unsupported ( ) . await ?;
899
- }
900
- Ok ( ( ) )
901
- }
902
- }
903
-
904
773
/// A function that can accept and flush buffers from a `MasWriteBuffer`.
905
774
/// Intended uses are the methods on `MasWriter` such as `write_users`.
906
775
type WriteBufferFlusher < ' conn , T > =
@@ -934,6 +803,9 @@ impl<'conn, T> MasWriteBuffer<'conn, T> {
934
803
}
935
804
936
805
pub async fn flush ( & mut self , writer : & mut MasWriter < ' conn > ) -> Result < ( ) , Error > {
806
+ if self . rows . is_empty ( ) {
807
+ return Ok ( ( ) ) ;
808
+ }
937
809
let rows = std:: mem:: take ( & mut self . rows ) ;
938
810
self . rows . reserve_exact ( WRITE_BUFFER_BATCH_SIZE ) ;
939
811
( self . flusher ) ( writer, rows) . await ?;
0 commit comments