关于tcpsock:send方法文档中说这是一个同步非阻塞的操作:
Sends data without blocking on the current TCP or Unix Domain Socket connection.
This method is a synchronous operation that will not return until all the data has been flushed into the system socket send buffer or an error occurs.
翻源码看到ngx_http_lua_socket_tcp.c/ngx_http_lua_socket_send()这个方法中有这样一段代码:
for (;;) {
n = c->send(c, b->pos, b->last - b->pos);
if (n >= 0) {
b->pos += n;
if (b->pos == b->last) {
.......
这里是在轮询这个非阻塞的方法,直到把所有的数据都发送完。
现在有一个疑问是,这里用非阻塞的好处是啥, 如果数据没有准备好的话,岂不是一直在做空轮询?用阻塞的方式还可以让出cpu的控制权。哪位大神可以帮忙科普下。