You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2024/11/28 14:41:16 UTC

(skywalking-php) branch master updated: Adapt to `Swoole\Coroutine\Http\Server` (#120)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-php.git


The following commit(s) were added to refs/heads/master by this push:
     new 954971b  Adapt to `Swoole\Coroutine\Http\Server` (#120)
954971b is described below

commit 954971b76455552c14e407f217bd9e08b883d616
Author: jmjoy <jm...@apache.org>
AuthorDate: Thu Nov 28 22:41:11 2024 +0800

    Adapt to `Swoole\Coroutine\Http\Server` (#120)
---
 .github/workflows/pecl.yml  |  2 +-
 src/plugin/plugin_swoole.rs | 49 +++++++++++++++++++++++++++++++--------------
 2 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/.github/workflows/pecl.yml b/.github/workflows/pecl.yml
index 58dffa2..1b45382 100644
--- a/.github/workflows/pecl.yml
+++ b/.github/workflows/pecl.yml
@@ -48,7 +48,7 @@ jobs:
       matrix:
         os:
           - ubuntu-20.04
-          - macos-12
+          # - macos-12
         version:
           - php: "8.2"
             swoole: "5.1.1"
diff --git a/src/plugin/plugin_swoole.rs b/src/plugin/plugin_swoole.rs
index c8f4485..46112f1 100644
--- a/src/plugin/plugin_swoole.rs
+++ b/src/plugin/plugin_swoole.rs
@@ -30,7 +30,7 @@ pub struct SwooleServerPlugin;
 impl Plugin for SwooleServerPlugin {
     #[inline]
     fn class_names(&self) -> Option<&'static [&'static str]> {
-        Some(&["Swoole\\Server"])
+        Some(&[r"Swoole\Server", r"Swoole\Coroutine\Http\Server"])
     }
 
     #[inline]
@@ -39,10 +39,11 @@ impl Plugin for SwooleServerPlugin {
     }
 
     fn hook(
-        &self, _class_name: Option<&str>, function_name: &str,
+        &self, class_name: Option<&str>, function_name: &str,
     ) -> Option<(Box<BeforeExecuteHook>, Box<AfterExecuteHook>)> {
-        match function_name {
-            "on" => Some(self.hook_on()),
+        match (class_name, function_name) {
+            (Some(r"Swoole\Server"), "on") => Some(self.hook_on()),
+            (Some(r"Swoole\Coroutine\Http\Server"), "handle") => Some(self.hook_handle()),
             _ => None,
         }
     }
@@ -64,25 +65,43 @@ impl SwooleServerPlugin {
                     return Ok(Box::new(()));
                 }
 
-                // Hack the closure with the
-                // [`crate::request::skywalking_hack_swoole_on_request`].
                 let closure = execute_data.get_mut_parameter(1);
-                let ori_closure = replace(
-                    closure,
-                    ZVal::from(ZString::new(HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME)),
-                );
+                Self::hack_callback(closure);
 
-                ORI_SWOOLE_ON_REQUEST.store(
-                    Box::into_raw(Box::new(ori_closure)).cast(),
-                    Ordering::Relaxed,
-                );
-                IS_SWOOLE.store(true, Ordering::Relaxed);
+                Ok(Box::new(()))
+            }),
+            Noop::noop(),
+        )
+    }
+
+    fn hook_handle(&self) -> (Box<BeforeExecuteHook>, Box<AfterExecuteHook>) {
+        (
+            Box::new(|_, execute_data| {
+                validate_num_args(execute_data, 2)?;
+
+                let closure = execute_data.get_mut_parameter(1);
+                Self::hack_callback(closure);
 
                 Ok(Box::new(()))
             }),
             Noop::noop(),
         )
     }
+
+    /// Hack the closure with the
+    /// [`crate::request::skywalking_hack_swoole_on_request`].
+    fn hack_callback(closure: &mut ZVal) {
+        let ori_closure = replace(
+            closure,
+            ZVal::from(ZString::new(HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME)),
+        );
+
+        ORI_SWOOLE_ON_REQUEST.store(
+            Box::into_raw(Box::new(ori_closure)).cast(),
+            Ordering::Relaxed,
+        );
+        IS_SWOOLE.store(true, Ordering::Relaxed);
+    }
 }
 
 #[derive(Default, Clone)]