File tree 2 files changed +12
-7
lines changed 2 files changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -171,7 +171,7 @@ impl Parseable {
171
171
}
172
172
173
173
// Gets write privileges only for creating the stream when it doesn't already exist.
174
- self . streams . create (
174
+ self . streams . get_or_create (
175
175
self . options . clone ( ) ,
176
176
stream_name. to_owned ( ) ,
177
177
LogStreamMetadata :: default ( ) ,
@@ -342,7 +342,7 @@ impl Parseable {
342
342
schema_version,
343
343
log_source,
344
344
) ;
345
- self . streams . create (
345
+ self . streams . get_or_create (
346
346
self . options . clone ( ) ,
347
347
stream_name. to_string ( ) ,
348
348
metadata,
@@ -652,7 +652,7 @@ impl Parseable {
652
652
SchemaVersion :: V1 , // New stream
653
653
log_source,
654
654
) ;
655
- self . streams . create (
655
+ self . streams . get_or_create (
656
656
self . options . clone ( ) ,
657
657
stream_name. to_string ( ) ,
658
658
metadata,
Original file line number Diff line number Diff line change @@ -737,17 +737,22 @@ pub struct Streams(RwLock<HashMap<String, StreamRef>>);
737
737
// 4. When first event is sent to stream (update the schema)
738
738
// 5. When set alert API is called (update the alert)
739
739
impl Streams {
740
- pub fn create (
740
+ /// Checks after getting an excluse lock whether already stream exists, else creates it.
741
+ /// NOTE: This is done to ensure we don't have contention among threads.
742
+ pub fn get_or_create (
741
743
& self ,
742
744
options : Arc < Options > ,
743
745
stream_name : String ,
744
746
metadata : LogStreamMetadata ,
745
747
ingestor_id : Option < String > ,
746
748
) -> StreamRef {
749
+ let mut guard = self . write ( ) . expect ( LOCK_EXPECT ) ;
750
+ if let Some ( stream) = guard. get ( & stream_name) {
751
+ return stream. clone ( ) ;
752
+ }
753
+
747
754
let stream = Stream :: new ( options, & stream_name, metadata, ingestor_id) ;
748
- self . write ( )
749
- . expect ( LOCK_EXPECT )
750
- . insert ( stream_name, stream. clone ( ) ) ;
755
+ guard. insert ( stream_name, stream. clone ( ) ) ;
751
756
752
757
stream
753
758
}
You can’t perform that action at this time.
0 commit comments