summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin V / vvval <[email protected]>2020-04-21 13:10:36 +0300
committerValentin V / vvval <[email protected]>2020-04-21 13:10:36 +0300
commitd08ae2b32037e197547eb55c09d557e88f406b01 (patch)
treed4d7a177102fda74029fcb6615207f6577d40153 /src
parent325dd39b610726df4b0ea7f70f5e475ffe63062d (diff)
#305 Reduce syscalls
Fix is based on #41 fix in goridge
Diffstat (limited to 'src')
-rw-r--r--src/Worker.php51
1 files changed, 24 insertions, 27 deletions
diff --git a/src/Worker.php b/src/Worker.php
index 5a567677..74296801 100644
--- a/src/Worker.php
+++ b/src/Worker.php
@@ -66,32 +66,12 @@ class Worker
}
if ($flags & Relay::PAYLOAD_ERROR) {
- return new \Error((string) $body);
+ return new \Error((string)$body);
}
return $body;
}
-// /**
-// * Respond to the server with result of task execution and execution context.
-// *
-// * Example:
-// * $worker->respond((string)$response->getBody(), json_encode($response->getHeaders()));
-// *
-// * @param string|null $payload
-// * @param string|null $header
-// */
-// public function send(string $payload = null, string $header = null): void
-// {
-// if ($header === null) {
-// $this->relay->send('', Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_NONE);
-// } else {
-// $this->relay->send($header, Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_RAW);
-// }
-//
-// $this->relay->send((string) $payload, Relay::PAYLOAD_RAW);
-// }
-
/**
* Respond to the server with result of task execution and execution context.
*
@@ -103,14 +83,31 @@ class Worker
*/
public function send(string $payload = null, string $header = null): void
{
- $flag = Relay::PAYLOAD_CONTROL;
- $flag = $flag | (empty($header) ? Relay::PAYLOAD_NONE : Relay::PAYLOAD_RAW);
+ if ($header === null) {
+ $this->relay->send('', Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_NONE);
+ } else {
+ $this->relay->send($header, Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_RAW);
+ }
- $head = pack('', $header, $flag);
- $body = pack('', $payload, Relay::PAYLOAD_RAW);
+ $this->relay->send((string)$payload, Relay::PAYLOAD_RAW);
+ }
- $this->relay->send($head . $body);
+ /**
+ * Respond to the server with result of task execution and execution context. Uses less amount of sys_calls.
+ *
+ * @param string|null $payload
+ * @param string|null $header
+ */
+ public function sendPackage(string $payload = null, string $header = null): void
+ {
+ $this->relay->sendPackage(
+ (string)$header,
+ Relay::PAYLOAD_CONTROL | ($header ? Relay::PAYLOAD_NONE : Relay::PAYLOAD_RAW),
+ (string)$payload,
+ Relay::PAYLOAD_RAW
+ );
}
+
/**
* Respond to the server with an error. Error must be treated as TaskError and might not cause
* worker destruction.
@@ -156,7 +153,7 @@ class Worker
private function handleControl(string $body = null, &$header = null, int $flags = 0): bool
{
$header = $body;
- if (is_null($body) || $flags & Relay::PAYLOAD_RAW) {
+ if ($body === null || $flags & Relay::PAYLOAD_RAW) {
// empty or raw prefix
return true;
}