最近在做一个Apigateway项目,目标就是做成类似AWS的Apigateway那样,但是只需要支持HTTP协议即可,由于配置非常灵活,而且性能要求比较高,我还是选择了 openresty 来作为这个网关的技术栈。但是我们需要对nginx.conf文件做一个改造,要做到添加或删除 路径的配置,修改Upstream的内容等都不能reload重启nginx,所以nginx原生的路由匹配功能我们就无法利用了。只能通过lua代码自己去匹配。
记录一个ngx.say和ngx.print差异导致的线上bug
最近上线一个项目,利用openresty在前面做反向代理,部分地址通过lua的http请求后端接口进行返回,在线下测试都没问题,公司预发灰度测试都通过了,但是上线到微信站,就莫名其妙的报错了。
由ngx.say和ngx.print差异引发的血案
最近上线一个项目,利用openresty在前面做反向代理,部分地址通过lua的http请求后端接口进行返回,在线下测试都没问题,公司预发灰度测试都通过了,但是上线到微信站,就莫名其妙的报错了。
or保持50并发请求第三方接口
当时有一个需求,发送1w个请求,保持50个并发,用or实现代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61_M.parallel = function(self)
local coNum = 50
local reqList = {}
for i=1,10000,1 do
table.insert(reqList, 'http://127.0.0.1:6565/respParallel')
end
local count = 0
local reqFunc = function(i)
-- ngx.log(ngx.ERR, "============", count, #reqList, "=======")
while count <= #reqList do
ngx.sleep(0)
count = count + 1
local request_url = reqList[count]
if not request_url then
break
end
-- 发送http请求获取od和map的对应关系
local httpc = http.new()
httpc:set_timeout(60000)
-- 拼接请求参数
local opt = {
method = "GET",
}
local res, err = httpc:request_uri(request_url, opt)
if err then
--ngx.log(ngx.ERR, string.format("thread [%s] request error count [%s] error, %s", i, count, err))
else
--ngx.log(ngx.ERR, string.format("thread [%s] request complete count[%s], body [%s]", i, count, res.body))
end
end
local msg = "thread [%s] complete"
--ngx.log(ngx.ERR, string.format(msg, i))
return msg
end
local threads = {}
for i=0,coNum,1 do
table.insert(threads, (ngx.thread.spawn(reqFunc, i)))
end
for i = 1, #threads do
local result = ngx.thread.wait(threads[i])
ngx.say(result)
end
local result, err = ngx.shared.cachDict:get("pcount")
ngx.say(string.format("=== total: %s ===", result))
return ""
end
golang遍历结构体
很多时候,我们可能会需要结构体赋值,又不想一个个字段的写,可以利用结构体遍历赋值
1 | package main |
go里面的几种继承写法
go里面的几种继承写法
1 | package main |
用哪种看自己喜欢了
输出:1
22,2
2,2