openresty / openresty/stream-lua-nginx-module

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

Open
#365 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
749
Forks
210
Avg merge
4h 31m
Merged PRs (30d)
3

Description

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 交流的所有数据的方法?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the stream preread_by_lua_file entry point and the sock:peek behavior described in the issue, including what happens after the final buffered byte and when preread_timeout is set. Check the module's existing documentation and tests for peek, buffering, and timeout handling; done would be a documented supported approach or a clearly scoped implementation for obtaining the available exchange data without consuming it.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.