-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-organize files & switch to go-logr/logr
- Loading branch information
Showing
10 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,14 @@ | ||
|
||
GOFILES=$(wildcard *.go) | ||
BACKENDS=$(wildcard ./backend/*.go) | ||
EXAMPLE=./example/main.go | ||
GOBIN=./bin | ||
|
||
all: build test | ||
|
||
.PHONY: build | ||
build: | ||
@echo " > formating file ..." | ||
go fmt $(GOFILES) | ||
go fmt $(BACKENDS) | ||
golangci-lint run $(GOFILES) | ||
golangci-lint run $(BACKENDS) | ||
@rm -rf $(GOBIN) | ||
@-mkdir $(GOBIN) | ||
go build -o $(patsubst %.go, $(GOBIN)/%, $(notdir $(EXAMPLE))) $(EXAMPLE) | ||
@scripts/go-build | ||
|
||
.PHONY: test | ||
test: | ||
@echo " > testing file ..." | ||
|
||
.PHONY:clean | ||
.PHONY: clean | ||
clean: | ||
@echo " > cleaning file ..." | ||
rm -rf $(GOBIN) | ||
rm -rf ./bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
# stderr 解决竞争的方案 | ||
# logr 后端设计 | ||
|
||
logr 在设计的时候充分考虑了扩展性,可以添加多种不同的后端方案,方便日志打印和日志归集。只要实现了 [`types.go`](../types/types.go) 中定义的 [`EntryWriter`](../types/types.go#L15) 接口,就可以直接切换后端,而不必担心日志前端的兼容问题。 | ||
|
||
这里,我们默认提供了 `stderr` 的[后端实现](./stderr.go)。 | ||
|
||
## stderr 解决竞争的方案 | ||
|
||
log 前端部分采用只读类型解决了竞争问题,后端的竞争问题由后端自行解决。这里的解决思路同样是两个:只读、加锁(包括 chan 也是用到了锁)。 | ||
|
||
我们注意到,可能存在的竞争发生在调用 os.Stderr.WriteString 的时候,由于这不是一个原子操作,也就存在着一条日志没打完,另一条日志混入的情况。只读的解决办法看样子不行,我们这里考虑加互斥锁、或者使用 channel,下面比较一下两种方案的优势和不足: | ||
|
||
使用 channel 的好处在于直观,所有的 log 请求被组织成了一个队列的形式,所有的日志汇集到了一个 goroutine 中进行处理;可能存在的问题是,当日志请求过多的时候,这个负责处理日志的 goroutine 可能会成为性能瓶颈。 | ||
|
||
加锁的话,日志请求在原来的 goroutine 中完成,避免了单个 goroutine 的性能我呢提,但是会影响当前 goroutine 的性能。 | ||
加锁的话,日志请求在原来的 goroutine 中完成,避免了单个 goroutine 的性能问题,但是会影响当前 goroutine 的性能。 | ||
|
||
我们假定日志组织良好,不会达到 goroutine 性能瓶颈。这样,采用 channel 的方式排队处理请求,把日志处理从普通 goroutine 中提取出来,由专门的 goroutine 来处理,以期提高性能。 | ||
|
||
### 思考1 | ||
|
||
如果使用的是无缓冲chan的话,日志库性能并不高,因为阻塞队列必须每产生一条日志就打印下来,这其实是一个同步操作。带缓冲的chan能够提高性能。(这里其实是假设所有的 goroutine 是在一个线程内部了,被分配到一个核上执行) | ||
|
||
ps: goroutine 是否能够充分利用多核CPU,把打印日志的 goroutine 单独放到另一个核中?这样的话就不存在阻塞问题了,性能会很好。 | ||
|
||
### 思考2 | ||
|
||
单独启动一个 goroutine 负责日志后端打印的话,存在一个问题:当主程序执行完一条打印日志命令后很快结束程序的时候,所有 goroutine 都会被杀死,但是这时候可能日志还没打印完毕,会有丢失日志的情况出现。我们需要一个同步的操作来等待日志打印完毕再结束进程。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/go-coder/log | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/go-coder/logr v0.1.0 | ||
github.com/go-logr/logr v0.1.0 | ||
github.com/spf13/pflag v1.0.3 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/go-coder/logr v0.1.0 h1:cKKbRhVJXz38rq0AG/QVIM6Atx14po8nL8It6wgi7+w= | ||
github.com/go-coder/logr v0.1.0/go.mod h1:Wb42XhUxL//12vqkxbdiqPP04fqnODPsXhd87jSiKqw= | ||
github.com/go-logr/logr v0.1.0 h1:M1Tv3VzNlEHg6uyACnRdtrploV2P7wZqH8BoQMtz0cg= | ||
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= | ||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= | ||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
# Use the Unofficial Bash Strict Mode (Unless You Looove Debugging) | Aaron Maxwell | ||
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
|
||
main() { | ||
echo " > formating file ..." | ||
for dir in . ./types ./frontend ./backend ./example; do | ||
go fmt $dir/... | ||
golangci-lint run $dir/... | ||
done | ||
echo " > building file ..." | ||
go build -o ./bin/main ./example/main.go | ||
} | ||
|
||
main ${@:-} |