Skip to content

Instantly share code, notes, and snippets.

View outsinre's full-sized avatar

Zachary Hu outsinre

View GitHub Profile
@outsinre
outsinre / call-graph.awk
Last active April 7, 2024 09:01 — forked from outro56/call-graph.awk
Parse lua code and print call graph
#!/usr/bin/awk -f
#
# call_graph.awk
#
# Usage:
# ./call_graph.awk my_program.lua | dot -Tsvg > call_graph.svg
#
# This is a script that generates a visual call graph for a Lua file.
# This script only shows calls made to functions defined within the
# input Lua file; that is, it excludes calls such as standard library
@outsinre
outsinre / pl_template.lua
Created March 18, 2024 07:00
A example of Lua template
#!/usr/bin/env lua
local pl_template = require "pl.template"
local tmpl_str = [[
<ul>
# for i,val in ipairs(T) do
<li>$(i) = $(val:upper())</li>
# end
</ul>
@outsinre
outsinre / jq-recursive-remove.sh
Last active January 31, 2024 09:03
Recurisvely remove a key if its value contains a substring
#!/usr/bin/env bash
jq 'walk( if (type == "object") and has("documentation") and (.documentation|contains("wJalrXUtnFEMI")) then del(.documentation) else . end )' sts-2011-06-15.normal.json >| output.json
# -or-
jq '(.. | select( (type == "object") and (.documentation != null) and (.documentation|contains("wJalrXUtnFEMI")) ) ) |= del(.documentation)' sts-2011-06-15.normal.json >| output.json
@outsinre
outsinre / monitor_table.lua
Last active March 18, 2024 07:01
Monitor Lua table access and update
-- table to monitor
ori_t = { a = "b" }
-- a proxy table - must be empty
proxy_t = {}
local mt
do
mt = {
__index = function (self, k)
@outsinre
outsinre / string_buffer_consumption.lua
Created October 17, 2023 14:46
string buffer consumption
local sb = require "string.buffer"
local buff = sb.new()
buff:put("a", "", "c")
print("buff len after put: ", #buff)
local str = buff:get()
print("1st get: ", str)
print("buff len after 1st get: ", #buff)
@outsinre
outsinre / resty-cosocket-reuse.lua
Created September 28, 2023 08:59
Reuse cosocket after close
local pl_pretty = require("pl.pretty").write
-- create cosocket
local sock = ngx.socket.tcp()
sock:settimeouts(1000, 15000, 15000)
ngx.say("sock 1 = ", pl_pretty(sock))
-- create kernel socket
@outsinre
outsinre / forward-declaration.lua
Created September 28, 2023 07:22
Lua forward declaration
local func -- Forward declaration. `local func = nil` is the same.
local function func2() -- Suppose you can't move this function lower.
return func() -- reference to name, not to its value
end
-- error
print(func2())
function func() -- defined here
@outsinre
outsinre / tcp_rst.lua
Created September 21, 2023 07:21
The send buffer has data
local socket = require("socket")
local host, port = "180.101.50.242", 80
local sock = socket.connect(host, port)
sock:settimeout(1)
sock:send("GET / HTTP/1.1\r\n\r\n")
sock:close()
@outsinre
outsinre / table_shrinking.lua
Created September 21, 2023 07:19
table_shrinking.lua
-- memory test
local size = 1000000
local t = {} -- create a table
local function report(title)
collectgarbage()
collectgarbage()
print(title, collectgarbage("count"))
end
local function list_iter (t)
local i = 0
local n = #t
return function ()
i = i + 1
if i <= n then return t[i] end
end
end
local t = { 10, 20, 30 }