Below you will find pages that utilize the taxonomy term “go”
Posts
Debuging Session
Today we encountered an intriguing production resource leak that deserved to be shared.
The Observation: In our production server, the CPU and Memory usage continued to increase while the new request came in. In the development server where no requests were served, the resource usage is stable. The server code was written in Go.
Two suspicions were drawn from the observation right away.
The problematic code resides in the hot path, ie.
Posts
查看Pod重启的原因
在Kubernetes中,有时候Pod会异常重启。事后发现的时候错误原因在控制台面板里已经看不到了。
实际上Kubernetes提供了相关的工具。我们可以在不可恢复的异常发生时拦截异常,将其写入/dev/termination-log。
package main import ( "fmt" "os" ) func main() { defer func() { if r := recover(); r != nil { _ = os.WriteFile( "/dev/termination-log", []byte(fmt.Sprintf("panic: %s", r)), os.ModePerm, ) panic(r) } }() // ... } 查看的时候,我们可以在edit pod时在lastState当中找到错误信息:
apiVersion:v1kind:Pod...lastState:terminated:containerID:...exitCode:0finishedAt:...message:| panic: goroutine 1 [running]:main.main.func1()/Users/donew/src/kitty/main.go:18+0x131panic(0x5192e20,0x5640e10)/usr/local/Cellar/go/1.16/libexec/src/runtime/panic.go:965+0x1b9main.main()/Users/donew/src/kitty/main.go:21+0x5b... 也可以直接用kubectl 查看。
kubectl get pod termination-demo -o go-template="{{range .status.containerStatuses}}{{.lastState.terminated.message}}{{end}}" 写入的路径可以在terminationMessagePath中更改。
除此之外,如果项目不方便拦截错误,还可以将terminationMessagePolicy设置为FallbackToLogsOnError。 此时使用容器日志输出的最后一块作为终止消息。 日志输出限制为 2048 字节或 80 行,以较小者为准。
实际操作中,FallbackToLogsOnError的长度限制导致有时候会截取太少,丢掉重要信息。
这里提供一个简单的最佳实践:将正常的日志打印到stdout中,将致命错误打印到stderr(Go里的panic默认就是stderr,不用拦截了),然后将terminationMessagePath设置为/dev/stderr,即可精确的获取Pod重启原因。
参考阅读:https://kubernetes.io/zh/docs/tasks/debug-application-cluster/determine-reason-pod-failure/
Posts
An Elegant Way to Bootstrap Go App
The twelve-factor methodology has proven its worth over the years. Since its invention many fields in technology have changed, many among them are shining and exciting. In the age of Kubernetes, service mesh and serverless architectures, the twelve-factor methodology has not faded away, but rather has happened to be a good fit for nearly all of those powerful platforms.
Scaffolding a twelve-factor go app may not be a difficult task for experienced engineers, but certainly presents some challenges to juniors.
Posts
使用Hyperf插入100万行数据到MongoDB,能行吗
最近用go搞了一个swoole的边车。是真的边车,用swoole process启动的。挂载到swoole server上跑,swoole起它起,swoole停它停,中间如果go挂了swoole还负责给拉起来。消息投递也照搬swoole task走IPC,从web worker上直接投递,等结果出来再返还web worker。
Posts
When Do We Need PHP
When we started huijiwiki.com, PHP was the language of the choice. But it was not much a choice. MediaWiki was coded in PHP. It provided us a solid starting point to expand our idea.
Since then I have touched many languages and am moderately proficient in some of them. Among them, Go is my current favorite. Rust is the language I want to explore more.
Go as well as Rust has a very different nature compared to PHP.
Posts
Options Take a Toll
Vue community exploded due to changes in Vue 3. Some comments are full of gunpowder with regards to the recent functional API RFC. Let me show my opinions up front:
A. OSS authors clearly deserve more respect.
B. I think functional API generally are more expressive compared to the old one.
However I would like to provide a counter argument to a very point the defender of RFC made.