Skip to content

Commit

Permalink
Adapt to Swoole\Coroutine\Http\Server (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy authored Nov 28, 2024
1 parent 3187ca1 commit 954971b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pecl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
matrix:
os:
- ubuntu-20.04
- macos-12
# - macos-12
version:
- php: "8.2"
swoole: "5.1.1"
Expand Down
49 changes: 34 additions & 15 deletions src/plugin/plugin_swoole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
}
}
Expand All @@ -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)]
Expand Down

0 comments on commit 954971b

Please sign in to comment.