跳过正文

Tech

Sentinel Golang
·1702 字
限流规则 # // Strategy type TokenCalculateStrategy int32 const ( Direct TokenCalculateStrategy = iota WarmUp MemoryAdaptive ) // Rule describes the strategy of flow control, the flow control strategy is based on QPS statistic metric type Rule struct { // ID represents the unique ID of the rule (optional). ID string `json:"id,omitempty"` // Resource represents the resource name. Resource string `json:"resource"` TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"` ControlBehavior ControlBehavior `json:"controlBehavior"` // Threshold means the threshold during StatIntervalInMs // If StatIntervalInMs is 1000(1 second), Threshold means QPS Threshold float64 `json:"threshold"` RelationStrategy RelationStrategy `json:"relationStrategy"` RefResource string `json:"refResource"` // MaxQueueingTimeMs only takes effect when ControlBehavior is Throttling. // When MaxQueueingTimeMs is 0, it means Throttling only controls interval of requests, // and requests exceeding the threshold will be rejected directly. MaxQueueingTimeMs uint32 `json:"maxQueueingTimeMs"` WarmUpPeriodSec uint32 `json:"warmUpPeriodSec"` WarmUpColdFactor uint32 `json:"warmUpColdFactor"` // StatIntervalInMs indicates the statistic interval and it's the optional setting for flow Rule. // If user doesn't set StatIntervalInMs, that means using default metric statistic of resource. // If the StatIntervalInMs user specifies can not reuse the global statistic of resource, // sentinel will generate independent statistic structure for this rule. StatIntervalInMs uint32 `json:"statIntervalInMs"` // adaptive flow control algorithm related parameters // limitation: LowMemUsageThreshold > HighMemUsageThreshold && MemHighWaterMarkBytes > MemLowWaterMarkBytes // if the current memory usage is less than or equals to MemLowWaterMarkBytes, threshold == LowMemUsageThreshold // if the current memory usage is more than or equals to MemHighWaterMarkBytes, threshold == HighMemUsageThreshold // if the current memory usage is in (MemLowWaterMarkBytes, MemHighWaterMarkBytes), threshold is in (HighMemUsageThreshold, LowMemUsageThreshold) LowMemUsageThreshold int64 `json:"lowMemUsageThreshold"` HighMemUsageThreshold int64 `json:"highMemUsageThreshold"` MemLowWaterMarkBytes int64 `json:"memLowWaterMarkBytes"` MemHighWaterMarkBytes int64 `json:"memHighWaterMarkBytes"` } 熔断规则 # 熔断规则
Docker基本命令
·436 字
基本命令 # 模块 功能 命令 repo 登录仓库 docker login [private repo] -u username -p password image 打包镜像 docker build . -t name:tag –build-arg VERSION=v1 image 拉取镜像 docker pull [name:tag] image 列出所有镜像 docker images 或 docker image ls image 删除镜像 docker image rm [name:tag|image id] image 删除所有不使用的镜像 docker image prune [–force –all | -f -a] container 列出运行中的容器 docker container [list|ls] container 列出运行中的容器 docker ps container 列出所有容器 docker container list -a container 启动容器 docker run -p 8080:8080 –name test -it [name:tag | image id] container 启动容器 docker start [container name | container id] container 进入容器 docker exec [OPTIONS] CONTAINER COMMAND [ARG…] container 删除所有停止的容器 docker container prune -f Dockerfile 打包镜像 # Dockerfiles 项目参考
链表
·358 字
链表简述 # 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)
Zap日志包
·410 字
zap # go get -u go.uber.org/zap
Viper配置包
·66 字
viper # go get github.com/spf13/viper 封装加载配置方法 # package configs import "github.com/spf13/viper" func LoadConfig(filepath string, encoding string, config interface{}) error { vp := viper.New() vp.SetConfigFile(filepath) vp.SetConfigType(encoding) err := vp.ReadInConfig() if err != nil { return err } return vp.Unmarshal(config) } func ReadConfig(filepath string, encoding string) (*viper.Viper, error) { vp := viper.New() vp.SetConfigFile(filepath) vp.SetConfigType(encoding) err := vp.ReadInConfig() return vp, err }
Go Sync包
·492 字
WaitGroup # type WaitGroup func (wg *WaitGroup) Add(delta int) func (wg *WaitGroup) Done() func (wg *WaitGroup) Wait() package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup delta := 10 wg.Add(delta) for i := 1; i <= delta; i++ { go func(i int) { defer wg.Done() fmt.Println("handle ", i) }(i) } wg.Wait() fmt.Println("done") } Mutex # type Mutex func (m *Mutex) Lock() // 若锁正在被占用,则Lock()会被阻塞,直至锁被释放 func (m *Mutex) TryLock() bool // 尝试加锁,若锁被占用,则返回false,加锁失败 func (m *Mutex) Unlock() // 未被加锁的Mutex直接Unlock()则会fatal error: sync: unlock of unlocked mutex package main import ( "fmt" "sync" "time" ) func main() { var mu sync.Mutex m := make(map[string]int) for i := 1; i <= 10; i++ { go func(i int, m map[string]int) { mu.Lock() m["num"] += i mu.Unlock() }(i, m) } time.Sleep(5 * time.Second) fmt.Println(m) } RWMutex # type RWMutex func (rw *RWMutex) Lock() // 读写锁 func (rw *RWMutex) RLock() // 读锁 func (rw *RWMutex) RLocker() Locker func (rw *RWMutex) RUnlock() func (rw *RWMutex) TryLock() bool func (rw *RWMutex) TryRLock() bool func (rw *RWMutex) Unlock() 和 mutex 类似,只不过功能更多,可以只加读锁
Go Time包
·910 字
时间转换 # func Now() Time // Now returns the current local time. func Unix(sec int64, nsec int64) Time func UnixMicro(usec int64) Time // Go v1.17 func UnixMilli(msec int64) Time // Go v1.17 func (t Time) Unix() int64 func (t Time) UnixMicro() int64 // Go v1.17 func (t Time) UnixMilli() int64 // Go v1.17 func (t Time) UnixNano() int64 // Go v1.17 func ParseInLocation(layout, value string, loc *Location) (Time, error) func (t Time) Format(layout string) string 获取当前时间戳 # package main import ( "fmt" "time" ) func main() { t := time.Now() // 当前时间 fmt.Println(t.Unix()) // 秒时间戳 fmt.Println(t.UnixMilli()) // 毫秒时间戳,Go V1.17新增 fmt.Println(t.UnixMicro()) // 微秒时间戳,Go V1.17新增 fmt.Println(t.UnixNano()) // 纳秒时间戳 } 时间戳转 string # 常用 time layout 常量
Requests库
·128 字
简单Get请求 # #!/usr/bin/python3 import requests # get返回html文本 def getData(url): result = requests.get(url) print(result.json()) if __name__ == '__main__': url = "https://mock.apifox.cn/m2/2245336-0-default/64492343" getData(url) Post请求 # form表单请求 # #!/usr/bin/python3 import requests # post form表单请求 def postData(url): data = {"key1": "value1", "key2": "value2"} result = requests.post(url, data=data) print(result.json()) if __name__ == '__main__': url = "https://baidu.com" postData(url) json请求 # #!/usr/bin/python3 import requests # post form表单请求 def postJsonData(url): data = {"key1": "value1", "key2": "value2"} result = requests.post(url, json=data) print(result.json()) if __name__ == '__main__': url = "https://baidu.com" postJsonData(url)
Singleflight源码解读
·1211 字
singleflight应用在什么场景 # singleflight主要应用在高并发场景下,通过对相同请求进行阻塞,减少实际业务方法执行次数,减少依赖服务压力,实际应用场景主要为防止缓存击穿,在接收到请求时,查询redis缓存或查询DB返回结果之前,对后续相同请求进行阻塞,待查询结果返回后被阻塞的n个请求使用相同的返回结果。适用于只读场景。
Thrift