-
Notifications
You must be signed in to change notification settings - Fork 2
pipe一些修改 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pengsven
wants to merge
17
commits into
siyuan.make_ngx_resp
Choose a base branch
from
test.shuwen
base: siyuan.make_ngx_resp
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
pipe一些修改 #4
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c72876c
fix async_wait_co_sema timeout
a47bd08
add comment for write_data_to_ngx
7a011c6
set default semaphore timeout 600s
cd9a33b
remove http related functions to httplib
77a7b0b
add make_read_max_size_filter
7abe6f5
add make_quorum_http_writers
eec869c
add make_aws_put_s3_writer
1aeace7
log writer/reader indent
85b4447
modify reader writer return value
5535554
add make_aws_put_s3_writer
cb4fb40
support http opts
e76db3d
add write client rpc log
42a7f82
fixup: error range parameter
fe6108e
write_data_to_ngx add body_sha1 optional arg
1016c8b
fixup typo
5a43e5d
add make_kill_low_write_speed_filter
82d1c7e
check sha1 before sending all data
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
local httpclient = require("acid.httpclient") | ||
local tableutil = require("acid.tableutil") | ||
local strutil = require("acid.strutil") | ||
|
||
local resty_sha1 = require( "resty.sha1" ) | ||
local resty_md5 = require( "resty.md5" ) | ||
local resty_string = require( "resty.string" ) | ||
|
||
local _M = { _VERSION = '1.0' } | ||
|
||
local to_str = strutil.to_str | ||
|
||
local BLOCK_SIZE = 1024 * 1024 | ||
local SOCKET_TIMEOUTS = {5 * 1000, 100 * 1000, 100 * 1000} | ||
|
||
function _M.connect_http(ips, port, verb, uri, opts) | ||
opts = opts or {} | ||
|
||
local try_times = math.max(opts.try_times or 1, 1) | ||
|
||
local http, _, err_code, err_msg | ||
|
||
for _, ip in ipairs(ips) do | ||
local headers = tableutil.dup(opts.headers or {}, true) | ||
headers.Host = headers.Host or ip | ||
|
||
local req = { | ||
ip = ip, | ||
port = port, | ||
uri = uri, | ||
verb = verb, | ||
headers = headers, | ||
} | ||
|
||
if opts.signature_cb ~= nil then | ||
req = opts.signature_cb(req) | ||
end | ||
|
||
http = httpclient:new(ip, port, opts.timeouts or SOCKET_TIMEOUTS, opts.http_opts) | ||
|
||
local h_opts = {method=req.verb, headers=req.headers} | ||
for i=1, try_times, 1 do | ||
_, err_code, err_msg = http:send_request(req.uri, h_opts) | ||
if err_code == nil then | ||
return http | ||
end | ||
end | ||
end | ||
|
||
if err_code ~= nil then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里可以直接返回错误 |
||
err_msg = to_str(err_code, ':', err_msg) | ||
err_code = 'ConnectError' | ||
end | ||
|
||
return nil, err_code, err_msg | ||
end | ||
|
||
function _M.get_http_response(http, opts) | ||
opts = opts or {} | ||
|
||
local _, err_code, err_msg = http:finish_request() | ||
if err_code ~= nil then | ||
return nil, err_code, err_msg | ||
end | ||
|
||
if opts.success_status ~= nil and opts.success_status ~= http.status then | ||
return nil, 'InvalidHttpStatus', to_str('response http status:', http.status) | ||
end | ||
|
||
local resp = { | ||
status = http.status, | ||
headers = http.ori_headers, | ||
} | ||
|
||
if opts.read_body == false then | ||
return resp | ||
end | ||
|
||
local body = {} | ||
|
||
while true do | ||
local data, err_code, err_msg = http:read_body(BLOCK_SIZE) | ||
if err_code ~= nil then | ||
return resp, err_code, err_msg | ||
end | ||
|
||
if data == '' then | ||
break | ||
end | ||
|
||
table.insert(body, data) | ||
end | ||
resp.body = table.concat(body) | ||
|
||
return resp | ||
end | ||
|
||
function _M.loop_http_read(pobj, ident, http, block_size, opts) | ||
opts = opts or {} | ||
|
||
local rst = { | ||
size = 0, | ||
md5 = nil, | ||
sha1 = nil, | ||
} | ||
|
||
local md5_alg | ||
if opts.calc_md5 == true then | ||
md5_alg = resty_md5:new() | ||
end | ||
|
||
local sha1_alg | ||
if opts.calc_sha1 == true then | ||
sha1_alg = resty_sha1:new() | ||
end | ||
|
||
while true do | ||
local data, err_code, err_msg = | ||
http:read_body(block_size or BLOCK_SIZE) | ||
if err_code ~= nil then | ||
return nil, err_code, err_msg | ||
end | ||
|
||
local _, err_code, err_msg = pobj:write_pipe(ident, data) | ||
if err_code ~= nil then | ||
return nil, err_code, err_msg | ||
end | ||
|
||
if opts.calc_md5 == true then | ||
md5_alg:update(data) | ||
end | ||
|
||
if opts.calc_sha1 == true then | ||
sha1_alg:update(data) | ||
end | ||
|
||
rst.size = rst.size + #data | ||
|
||
if data == '' then | ||
if opts.calc_md5 == true then | ||
rst.md5 = resty_string.to_hex(md5_alg:final()) | ||
end | ||
|
||
if opts.calc_sha1 == true then | ||
rst.sha1 = resty_string.to_hex(sha1_alg:final()) | ||
end | ||
|
||
break | ||
end | ||
end | ||
|
||
return rst | ||
end | ||
|
||
function _M.loop_http_write(pobj, ident, http) | ||
local bytes = 0 | ||
|
||
while true do | ||
local data, err_code, err_msg = pobj:read_pipe(ident) | ||
if err_code ~= nil then | ||
return nil, err_code, err_msg | ||
end | ||
|
||
if data == '' then | ||
break | ||
end | ||
|
||
local now = ngx.now() | ||
local _, err_code, err_msg = http:send_body(data) | ||
|
||
ngx.update_time() | ||
pobj:incr_stat(ident, "write_size", #data) | ||
pobj:incr_stat(ident, "write_time", ngx.now()-now) | ||
|
||
bytes = bytes + #data | ||
end | ||
|
||
return bytes | ||
end | ||
|
||
return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面这些初始化和赋值是不是可以从for循环中拿出去