Skip to content

Commit 8bdef58

Browse files
committed
ext/openssl/xp_ssl: eliminate poll() when MSG_DONTWAIT is available
If there is a zero timeout and MSG_DONTWAIT is available (or the socket is non-blocking), the poll() call is not necessary, and we can just call recv() right away.
1 parent 42817cf commit 8bdef58

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

ext/openssl/xp_ssl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,10 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
23952395

23962396
if (sslsock->s.socket == -1) {
23972397
alive = 0;
2398-
} else if (php_pollfd_for(sslsock->s.socket, PHP_POLLREADABLE|POLLPRI, &tv) > 0) {
2398+
} else if ((!sslsock->ssl_active && value == 0 && (MSG_DONTWAIT || !sslsock->s.is_blocked)) ||
2399+
php_pollfd_for(sslsock->s.socket, PHP_POLLREADABLE|POLLPRI, &tv) > 0) {
2400+
/* the poll() call was skipped if the socket is non-blocking (or MSG_DONTWAIT is available) and if the timeout is zero */
2401+
/* additionally, we don't use this optimization if SSL is active because in that case, we're not using MSG_DONTWAIT */
23992402
if (sslsock->ssl_active) {
24002403
int n = SSL_peek(sslsock->ssl_handle, &buf, sizeof(buf));
24012404
if (n <= 0) {
@@ -2413,7 +2416,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
24132416
alive = 0;
24142417
}
24152418
}
2416-
} else if (0 == recv(sslsock->s.socket, &buf, sizeof(buf), MSG_PEEK) && php_socket_errno() != EAGAIN) {
2419+
} else if (0 == recv(sslsock->s.socket, &buf, sizeof(buf), MSG_PEEK|MSG_DONTWAIT) && php_socket_errno() != EAGAIN) {
24172420
alive = 0;
24182421
}
24192422
}

0 commit comments

Comments
 (0)