Skip to content

我在preread_by_lua_file 阶段进行 sock 的 peek 操作,我想优雅的获取缓冲区的所有数据该怎么实现 #365

@wjsw1781

Description

@wjsw1781
local peek_num=1
  while true do
      local chunk, err =sock:peek(peek_num)
      ngx.log(ngx.ERR,"peek ..." ,chunk, ' == ',peek_num ,err )
      -- ngx.log(ngx.ERR,"peek ..." ,#chunk, ' == ',peek_num ,err,bytes_to_hex(chunk) )
      peek_num=peek_num+1
  end
 ngx.log(ngx.ERR,"退出循环 ..."  )

这样会卡死在最后一个缓冲区的下一个

但是当我添加了 preread_timeout 1s;


stream {
    preread_timeout 1s;

    server {
        listen 0.0.0.0:3889;  #监听本机地址和端口,当使用keeplived的情况下使用keeplived VIP
        preread_by_lua_file /root/socks_ss_gfw_ss_socks/nginx_redis_lua/test_socks.lua;
        proxy_pass backend; #
    }
    upstream backend{  
        server 127.0.0.2:1101;  # 爱写啥写啥  反正下面的代码也给你改了
        balancer_by_lua_file /root/socks_ss_gfw_ss_socks/nginx_redis_lua/balancer_by_lua_file.lua;
    } 

}
此时

        local chunk, err =pcall(function() return sock:peek(peek_num) end) 


这里的 pcall 还是捕获不到这种错误

我不信想使用 recive 这种消耗配合字节逐个解析的方法 可能还得配合超时来获取字节总是很浪费性能 难道就没有一个方法可以从缓冲区获取一次 tcp 交流的所有数据的方法?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions