Skip to content

Commit

Permalink
updates for Swoole 4.5-3RC1
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Aug 24, 2020
1 parent 9082983 commit feffd70
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 22 deletions.
4 changes: 2 additions & 2 deletions output/swoole/constants.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

define('SWOOLE_VERSION', '4.5.3-beta');
define('SWOOLE_VERSION', '4.5.3RC1');
define('SWOOLE_VERSION_ID', 40503);
define('SWOOLE_MAJOR_VERSION', 4);
define('SWOOLE_MINOR_VERSION', 5);
define('SWOOLE_RELEASE_VERSION', 3);
define('SWOOLE_EXTRA_VERSION', 'beta');
define('SWOOLE_EXTRA_VERSION', 'RC1');
define('SWOOLE_DEBUG', '');
define('SWOOLE_HAVE_COMPRESSION', '1');
define('SWOOLE_HAVE_ZLIB', '1');
Expand Down
2 changes: 1 addition & 1 deletion output/swoole/namespace/Coroutine/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function rollback($timeout = null)
/**
* @return mixed
*/
public function escape($String, $flags = null)
public function escape($string, $flags = null)
{
}

Expand Down
10 changes: 10 additions & 0 deletions output/swoole_library/src/core/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Iterator;
use RuntimeException;
use Serializable;
use Swoole\Exception\ArrayKeyNotExists;

class ArrayObject implements ArrayAccess, Serializable, Countable, Iterator
{
Expand Down Expand Up @@ -95,6 +96,9 @@ public function next()
*/
public function get($key)
{
if (!$this->exists($key)) {
throw new ArrayKeyNotExists($key);
}
return static::detectType($this->array[$key]);
}

Expand Down Expand Up @@ -314,6 +318,12 @@ public function pushFront($value)
return array_unshift($this->array, $value);
}

public function append(...$values): ArrayObject
{
array_push($this->array, ...$values);
return $this;
}

/**
* @param mixed $value
* @return int
Expand Down
30 changes: 18 additions & 12 deletions output/swoole_library/src/core/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,28 @@ class Constant

public const OPTION_DNS_SERVER = 'dns_server';

public const OPTION_SOCKET_DNS_TIMEOUT = 'socket_dns_timeout';

public const OPTION_SOCKET_CONNECT_TIMEOUT = 'socket_connect_timeout';

public const OPTION_SOCKET_WRITE_TIMEOUT = 'socket_write_timeout';

public const OPTION_SOCKET_SEND_TIMEOUT = 'socket_send_timeout';

public const OPTION_SOCKET_READ_TIMEOUT = 'socket_read_timeout';

public const OPTION_SOCKET_RECV_TIMEOUT = 'socket_recv_timeout';

public const OPTION_SOCKET_BUFFER_SIZE = 'socket_buffer_size';

public const OPTION_SOCKET_TIMEOUT = 'socket_timeout';

public const OPTION_ENABLE_SIGNALFD = 'enable_signalfd';

public const OPTION_WAIT_SIGNAL = 'wait_signal';

public const OPTION_DNS_CACHE_REFRESH_TIME = 'dns_cache_refresh_time';

public const OPTION_SOCKET_BUFFER_SIZE = 'socket_buffer_size';

public const OPTION_THREAD_NUM = 'thread_num';

public const OPTION_MIN_THREAD_NUM = 'min_thread_num';
Expand Down Expand Up @@ -188,16 +200,6 @@ class Constant

public const OPTION_STACK_SIZE = 'stack_size';

public const OPTION_SOCKET_DNS_TIMEOUT = 'socket_dns_timeout';

public const OPTION_SOCKET_CONNECT_TIMEOUT = 'socket_connect_timeout';

public const OPTION_SOCKET_TIMEOUT = 'socket_timeout';

public const OPTION_SOCKET_READ_TIMEOUT = 'socket_read_timeout';

public const OPTION_SOCKET_WRITE_TIMEOUT = 'socket_write_timeout';

public const OPTION_DNS_CACHE_EXPIRE = 'dns_cache_expire';

public const OPTION_DNS_CACHE_CAPACITY = 'dns_cache_capacity';
Expand All @@ -214,6 +216,8 @@ class Constant

public const OPTION_DEFER = 'defer';

public const OPTION_LOWERCASE_HEADER = 'lowercase_header';

public const OPTION_KEEP_ALIVE = 'keep_alive';

public const OPTION_WEBSOCKET_MASK = 'websocket_mask';
Expand Down Expand Up @@ -372,6 +376,8 @@ class Constant

public const OPTION_TCP_KEEPCOUNT = 'tcp_keepcount';

public const OPTION_TCP_USER_TIMEOUT = 'tcp_user_timeout';

public const OPTION_TCP_FASTOPEN = 'tcp_fastopen';

public const OPTION_PACKAGE_BODY_START = 'package_body_start';
Expand Down
64 changes: 64 additions & 0 deletions output/swoole_library/src/core/Coroutine/Barrier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Coroutine;

use Swoole\Coroutine;
use Swoole\Exception;
use Swoole\Timer;

class Barrier
{
private $cid = -1;

private $timer = -1;

private static $cancel_list = [];

public function __destruct()
{
if ($this->timer != -1) {
Timer::clear($this->timer);
if (static::$cancel_list[$this->cid]) {
unset(static::$cancel_list[$this->cid]);
return;
}
}
if ($this->cid != -1) {
Coroutine::resume($this->cid);
}
}

public static function make()
{
return new static();
}

/**
* @throws Exception
*/
public static function wait(Barrier &$barrier, float $timeout = -1)
{
if ($barrier->cid != -1) {
throw new Exception('The barrier is waiting, cannot wait again.');
}
$cid = Coroutine::getCid();
$barrier->cid = $cid;
if ($timeout > 0 && ($timeout_ms = intval($timeout * 1000)) > 0) {
$barrier->timer = Timer::after($timeout_ms, function () use ($cid) {
self::$cancel_list[$cid] = true;
Coroutine::resume($cid);
});
}
$barrier = null;
Coroutine::yield();
}
}
5 changes: 4 additions & 1 deletion output/swoole_library/src/core/Coroutine/WaitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class WaitGroup

protected $waiting = false;

public function __construct()
public function __construct(int $delta = 0)
{
$this->chan = new Channel(1);
if ($delta > 0) {
$this->add($delta);
}
}

public function add(int $delta = 1): void
Expand Down
14 changes: 14 additions & 0 deletions output/swoole_library/src/core/Coroutine/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ function batch(array $tasks, float $timeout = -1): array
$wg->wait($timeout);
return $tasks;
}

function parallel(int $n, callable $fn): void
{
$count = $n;
$wg = new WaitGroup();
$wg->add($n);
while ($count--) {
Coroutine::create(function () use ($fn, $wg) {
$fn();
$wg->done();
});
}
$wg->wait();
}
9 changes: 8 additions & 1 deletion output/swoole_library/src/core/Curl/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,15 @@ private function setOption(int $opt, $value): bool
$this->method = (string) $value;
break;
case CURLOPT_PROTOCOLS:
if ($value > 3) {
if (($value & ~(CURLPROTO_HTTP | CURLPROTO_HTTPS)) != 0) {
throw new CurlException("swoole_curl_setopt(): CURLOPT_PROTOCOLS[{$value}] is not supported");
}
break;
case CURLOPT_REDIR_PROTOCOLS:
if (($value & ~(CURLPROTO_HTTP | CURLPROTO_HTTPS)) != 0) {
throw new CurlException("swoole_curl_setopt(): CURLOPT_REDIR_PROTOCOLS[{$value}] is not supported");
}
break;
case CURLOPT_HTTP_VERSION:
if ($value != CURL_HTTP_VERSION_1_1) {
trigger_error("swoole_curl_setopt(): CURLOPT_HTTP_VERSION[{$value}] is not supported", E_USER_WARNING);
Expand Down Expand Up @@ -710,6 +715,8 @@ private function execute()
$errCode = $client->errCode;
if ($errCode == SWOOLE_ERROR_DNSLOOKUP_RESOLVE_FAILED or $errCode == SWOOLE_ERROR_DNSLOOKUP_RESOLVE_TIMEOUT) {
$this->setError(CURLE_COULDNT_RESOLVE_HOST, 'Could not resolve host: ' . $client->host);
} else {
$this->setError($errCode, $client->errMsg);
}
$this->info['total_time'] = microtime(true) - $timeBegin;
return false;
Expand Down
2 changes: 1 addition & 1 deletion output/swoole_library/src/core/Database/MysqliPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(MysqliConfig $config, int $size = self::DEFAULT_SIZE
$this->config->getUnixSocket()
);
if ($mysqli->connect_errno) {
throw new MysqliException($mysqli->connect_errno, $mysqli->connect_errno);
throw new MysqliException($mysqli->connect_error, $mysqli->connect_errno);
}
return $mysqli;
}, $size, MysqliProxy::class);
Expand Down
16 changes: 16 additions & 0 deletions output/swoole_library/src/core/Exception/ArrayKeyNotExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of Swoole.
*
* @link https://www.swoole.com
* @contact [email protected]
* @license https://github.com/swoole/library/blob/master/LICENSE
*/

declare(strict_types=1);

namespace Swoole\Exception;

class ArrayKeyNotExists extends \RuntimeException
{
}
19 changes: 15 additions & 4 deletions output/swoole_library/src/core/StringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,25 @@ public function startsWith(string $needle): bool
return strpos($this->string, $needle) === 0;
}

public function contains(string $subString): bool
public function endsWith(string $needle): bool
{
return strpos($this->string, $subString) !== false;
return strrpos($this->string, $needle) === (strlen($this->string) - strlen($needle));
}

public function endsWith(string $needle): bool
public function equals($str, bool $strict = false): bool
{
return strrpos($this->string, $needle) === (strlen($this->string) - strlen($needle));
if ($str instanceof StringObject) {
$str = strval($str);
}
if ($strict) {
return $this->string === $str;
}
return $this->string == $str;
}

public function contains(string $subString): bool
{
return strpos($this->string, $subString) !== false;
}

public function split(string $delimiter, int $limit = PHP_INT_MAX): ArrayObject
Expand Down

0 comments on commit feffd70

Please sign in to comment.