You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "onewe (via GitHub)" <gi...@apache.org> on 2024/03/16 12:18:11 UTC

[PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

onewe opened a new pull request, #184:
URL: https://github.com/apache/dubbo-rust/pull/184

   - Add a new struct called LoadExtensionPromise
   - Remove async modifier in ExtensionDirectory
   
   Close #183


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

Posted by "kwsc98 (via GitHub)" <gi...@apache.org>.
kwsc98 commented on code in PR #184:
URL: https://github.com/apache/dubbo-rust/pull/184#discussion_r1527477678


##########
dubbo/src/extension/mod.rs:
##########
@@ -89,47 +87,134 @@ impl ExtensionDirectory {
             ExtensionType::Registry => match extension_factories {
                 ExtensionFactories::RegistryExtensionFactory(registry_extension_factory) => {
                     self.registry_extension_loader
-                        .register(extension_name, registry_extension_factory)
-                        .await;
+                        .register(extension_name, registry_extension_factory);
                     Ok(())
                 }
             },
         }
     }
 
-    async fn remove(
+    fn remove(
         &mut self,
         extension_name: String,
         extension_type: ExtensionType,
     ) -> Result<(), StdError> {
         match extension_type {
             ExtensionType::Registry => {
-                self.registry_extension_loader.remove(extension_name).await;
+                self.registry_extension_loader.remove(extension_name);
                 Ok(())
             }
         }
     }
 
-    async fn load(
+    fn load(
         &mut self,
         url: Url,
         extension_type: ExtensionType,
-    ) -> Result<Extensions, StdError> {
+        callback: oneshot::Sender<Result<Extensions, StdError>>,
+    ) {
         match extension_type {
             ExtensionType::Registry => {
-                let extension = self.registry_extension_loader.load(&url).await;
+                let extension = self.registry_extension_loader.load(url);
                 match extension {
-                    Ok(extension) => Ok(Extensions::Registry(extension)),
+                    Ok(mut extension) => {
+                        tokio::spawn(async move {
+                            let extension = extension.resolve().await;
+                            match extension {
+                                Ok(extension) => {
+                                    let _ = callback.send(Ok(Extensions::Registry(extension)));
+                                }
+                                Err(err) => {
+                                    error!("load extension failed: {}", err);
+                                    let _ = callback.send(Err(err));
+                                }
+                            }
+                        });
+                    }
                     Err(err) => {
                         error!("load extension failed: {}", err);
-                        Err(err)
+                        let _ = callback.send(Err(err));
                     }
                 }
             }
         }
     }
 }
 
+pub(crate) struct LoadExtensionPromise<T> {
+    extension: Arc<Option<T>>,

Review Comment:
   advise maintain a RwLock , Avoid using unsafe 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

Posted by "kwsc98 (via GitHub)" <gi...@apache.org>.
kwsc98 commented on code in PR #184:
URL: https://github.com/apache/dubbo-rust/pull/184#discussion_r1528190054


##########
dubbo/src/extension/mod.rs:
##########
@@ -141,61 +141,95 @@ impl ExtensionDirectory {
     }
 }
 
+type ExtensionCreator<T> = Box<
+    dyn Fn(Url) -> Pin<Box<dyn Future<Output = Result<T, StdError>> + Send + 'static>>
+        + Send
+        + Sync
+        + 'static,
+>;
+pub(crate) struct ExtensionPromiseResolver<T> {
+    resolved_data: Option<T>,
+    creator: ExtensionCreator<T>,
+    url: Url,
+}
+
+impl<T> ExtensionPromiseResolver<T>
+where
+    T: Send + Clone + 'static,
+{
+    fn new(creator: ExtensionCreator<T>, url: Url) -> Self {
+        ExtensionPromiseResolver {
+            resolved_data: None,
+            creator,
+            url,
+        }
+    }
+
+    fn resolved_data(&self) -> Option<T> {
+        self.resolved_data.clone()
+    }
+
+    async fn resolve(&mut self) -> Result<T, StdError> {
+        match (self.creator)(self.url.clone()).await {
+            Ok(data) => {
+                self.resolved_data = Some(data.clone());
+                Ok(data)
+            }
+            Err(err) => {
+                error!("create extension failed: {}", err);
+                Err(LoadExtensionError::new(
+                    "load extension failed, create extension occur an error".to_string(),
+                )
+                .into())
+            }
+        }
+    }
+}
+
 pub(crate) struct LoadExtensionPromise<T> {
-    extension: Arc<Option<T>>,
-    fut: Option<Pin<Box<dyn Future<Output = Result<T, StdError>> + Send + 'static>>>,
-    semaphore: Arc<Semaphore>,
+    resolver: Arc<RwLock<ExtensionPromiseResolver<T>>>,
 }
 
 impl<T> LoadExtensionPromise<T>
 where
     T: Send + Clone + 'static,
 {
-    pub(crate) fn new(
-        fut: Pin<Box<dyn Future<Output = Result<T, StdError>> + Send + 'static>>,
-    ) -> Self {
+    pub(crate) fn new(creator: ExtensionCreator<T>, url: Url) -> Self {
+        let resolver = ExtensionPromiseResolver::new(creator, url);
         LoadExtensionPromise {
-            extension: Arc::new(None),
-            fut: Some(fut),
-            semaphore: Arc::new(Semaphore::new(0)),
+            resolver: Arc::new(RwLock::new(resolver)),
         }
     }
 
-    fn get_extension(&self) -> Option<T> {
-        self.extension.as_ref().as_ref().map(|a| a.clone())
-    }
-
     pub(crate) async fn resolve(&mut self) -> Result<T, StdError> {

Review Comment:
   Can be simplified like
   ```rust
       pub(crate) async fn resolve(&mut self) -> Result<T, StdError> {
           // get read lock
           let resolver_read_lock = self.resolver.read().await;
           // if extension is not None, return it
           if let Some(extension) = resolver_read_lock.resolved_data() {
               return Ok(extension);
           }
           drop(resolver_read_lock);
           let mut resolver_write_lock = self.resolver.write().await;
           Ok(match resolver_write_lock.resolved_data() {
               Some(data) => data,
               None => resolver_write_lock.resolve().await?,
           })
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

Posted by "kwsc98 (via GitHub)" <gi...@apache.org>.
kwsc98 commented on PR #184:
URL: https://github.com/apache/dubbo-rust/pull/184#issuecomment-2005617134

   good work !


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

Posted by "yang20150702 (via GitHub)" <gi...@apache.org>.
yang20150702 merged PR #184:
URL: https://github.com/apache/dubbo-rust/pull/184


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] Fix: get stuck when load extension in the concurrency environment [dubbo-rust]

Posted by "yang20150702 (via GitHub)" <gi...@apache.org>.
yang20150702 commented on PR #184:
URL: https://github.com/apache/dubbo-rust/pull/184#issuecomment-2009380425

   Good work!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org