Skip to content

Instantly share code, notes, and snippets.

-- handler.lua
local BasePlugin = require "kong.plugins.base_plugin"
local kong = kong
local CustomHandler = BasePlugin:extend()
CustomHandler.VERSION = "1.0.0"
CustomHandler.PRIORITY = 10
@ggzeng
ggzeng / kong_pdk_tbl_cache.lua
Created January 11, 2020 09:16
kong 和table相关pdk的cache操作 kong: 1.4.x
-- ----------------------------
-- 下面cache的key为PK或者uniq字段
-- ----------------------------
-- 查表从数据库中取出
local function load_{tablename}_by_{filedname}(var_filedname)
local row, err = kong.db.{tablename}:select_by_{filedname}(var_filedname) -- 3. cache中没有时从db中获取数据
if err then
return nil, err
@ggzeng
ggzeng / go_pipline_demo.go
Last active December 31, 2019 10:10
https://gist.github.com/claudiofahey/3afcf4f4fb3d8d3b35cadb100d4fb9b7 https://blog.golang.org/pipelines 基本思想: 1. pipeline中各stage的组合通过自定义函数实现 2. 每个stage中都通过go routine并行处理,处理结果与处理过程中的错误都通过channel返回 3. 每个stage都会传入ctx和输入channel 4. 下一个stage使用上一个stage的输出c
//
// Companion code to https://medium.com/statuscode/pipeline-patterns-in-go-a37bb3a7e61d
//
// To run:
// go get github.com/pkg/errors
// go run -race pipeline_demo.go
//
package main
import (
@ggzeng
ggzeng / gist:244340c0a53720c13a70b38046b7b19e
Last active April 21, 2021 06:46
git config --global commit.template ~/.git-commit-template.txt
We couldn’t find that file to show.
@ggzeng
ggzeng / caputre_sys_signal.go
Created November 21, 2019 11:51
捕获并处理系统的signal
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
@ggzeng
ggzeng / cmd_exec_in_output.go
Created November 21, 2019 10:18
golang调用外部命令
package main
import (
"fmt"
"io/ioutil"
"os/exec"
)
func main() {
@ggzeng
ggzeng / ratelimit.go
Created November 21, 2019 04:00
golang 限速的实现方式
package main
import (
"fmt"
"time"
)
func main() {
requests := make(chan int, 5)
@ggzeng
ggzeng / multi_producer_single_consumer.go
Last active November 20, 2019 13:09
一些生成和消费者模式
package main
// 多生成,多消费者;注意下面的注意事项
// https://www.toutiao.com/a6506067818100818445/
import (
"fmt"
"math/rand"
"sync"
"time"
)
@ggzeng
ggzeng / gracefull_exit.go
Last active December 17, 2021 17:10
golang优雅退出的方法 Channel关闭原则:不要在消费端关闭channel,不要在有多个并行的生产者时对channel执行关闭操作。 https://github.com/Shitaibin/golang_goroutine_exit https://www.toutiao.com/a6506067818100818445/
// 一个循环里只从一个工作channel里读数据
go func(in <-chan int) {
for x := range in { // 当channel "in"被关闭时会结束循环
fmt.Printf("Process %d\n", x)
}
}(inCh)
// 一个循环里需要从一个工作channel里读数据,同时还需响应控制channel
go func() {
@ggzeng
ggzeng / protobuf_grpc_greeter_client.go
Last active May 29, 2020 07:45
protocol buffer grpc examples
// Package main implements a client for Greeter service.
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"