onBufferFull
Description:
callback Worker::$onBufferFull
Each connection has a separate application layer send buffer. If the client's receiving speed is lower than the server's sending speed, data will be temporarily stored in the application layer buffer. If the buffer is full, the onBufferFull callback will be triggered.
The buffer size is large as TcpConnection::$maxSendBufferSize, with a default value of 1MB. The buffer size can be dynamically set for the current connection, for example:
// Set the send buffer size of the current connection in bytes
$connection->maxSendBufferSize = 102400;
You can also use TcpConnection::$defaultMaxSendBufferSize to set the default buffer size for all connections, for example:
use Workerman\Connection\TcpConnection;
// Set the default application layer send buffer size for all connections in bytes
TcpConnection::$defaultMaxSendBufferSize = 2*1024*1024;
This callback may be triggered immediately after calling Connection::send, for example when sending large data or sending data quickly in succession. Due to network reasons, data is heavily queued in the corresponding connection's send buffer. When it exceeds the limit of TcpConnection::$maxSendBufferSize, it triggers the event.
When the onBufferFull event occurs, developers generally need to take measures such as stopping data transmission to the other end and waiting for the data in the send buffer to be completely sent (on onBufferDrain event), etc.
When calling Connection::send($A) leads to triggering onBufferFull, regardless of how large the data $A is (even if larger than TcpConnection::$maxSendBufferSize), the data to be sent this time will still be placed into the send buffer. This means that the actual data placed in the send buffer can far exceed TcpConnection::$maxSendBufferSize. If the data in the send buffer has already exceeded TcpConnection::$maxSendBufferSize and Connection::send($B) is still invoked, then the data $B this time will not be added to the send buffer but will be discarded, triggering the onError callback.
In summary, as long as the send buffer is not full, even with just one byte of space, calling Connection::send($A) will definitely place $A into the send buffer. If the size of the send buffer exceeds the limitation of TcpConnection::$maxSendBufferSize after placing data into it, then the onBufferFull callback will be triggered.
Callback Function Parameters
$connection
The connection object, which is a TcpConnection instance, used to operate on the client connection, such as sending data, closing the connection, etc.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onBufferFull = function(TcpConnection $connection)
{
echo "bufferFull and do not send again\n";
};
// Run the worker
Worker::runAll();
Tip: In addition to using anonymous functions as callbacks, you can also refer here to use other callback writing methods.
See Also
onBufferDrain Triggered when the application's send buffer data for the connection is completely sent.