From 5916dc5ce0a8e5b71985c734b82f2a70a30c385a Mon Sep 17 00:00:00 2001 From: Trino Date: Fri, 16 Feb 2024 21:19:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?chore(lint):=E5=A2=9E=E5=8A=A0golanci-lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/golangci-lint.yaml | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/golangci-lint.yaml diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 0000000..53ba642 --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,55 @@ +name: golangci-lint +on: + push: + pull_request: + +permissions: + # Required: allow read access to the content for analysis. + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + pull-requests: read + # Optional: Allow write access to checks to allow the action to annotate code in the PR. + checks: write + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + # Require: The version of golangci-lint to use. + # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. + # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + version: v1.54 + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + # + # Note: By default, the `.golangci.yml` file should be at the root of the repository. + # The location of the configuration file can be changed by using `--config=` + # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true, then all caching functionality will be completely disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/.cache/go-build. + # skip-build-cache: true + + # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'. + # install-mode: "goinstall" \ No newline at end of file From 9f0a84dada6c5755901096d7060ecbdbffbb2a5e Mon Sep 17 00:00:00 2001 From: Trino Date: Fri, 16 Feb 2024 21:43:51 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E4=BB=A3=E7=A0=81=E4=B8=AD=E4=B8=8D=E7=AC=A6=E5=90=88?= =?UTF-8?q?lint=E8=A7=84=E8=8C=83=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/golangci-lint.yaml | 1 - .github/workflows/pages.yaml | 3 +-- .github/workflows/test.yaml | 1 - errs/error.go | 10 ++++++++++ storage/cli/cli.go | 3 ++- storage/core/ragdoll/data.go | 4 ---- storage/core/ragdoll/kv.go | 5 ----- storage/server/mw.go | 4 ++-- storage/server/server.go | 15 ++++++++++----- utils/file_test.go | 2 +- 10 files changed, 26 insertions(+), 22 deletions(-) diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index 53ba642..f1334e9 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -1,6 +1,5 @@ name: golangci-lint on: - push: pull_request: permissions: diff --git a/.github/workflows/pages.yaml b/.github/workflows/pages.yaml index 7f03943..d5b4352 100644 --- a/.github/workflows/pages.yaml +++ b/.github/workflows/pages.yaml @@ -1,10 +1,9 @@ name: Page on: - push: + pull_request: branches: - main # default branch - - doc/restart # test jobs: build: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index ee3bbef..9c99703 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,7 +1,6 @@ name: Go Test on: - push: pull_request: jobs: diff --git a/errs/error.go b/errs/error.go index 797524e..50c5a17 100644 --- a/errs/error.go +++ b/errs/error.go @@ -80,6 +80,8 @@ const ( CreateTempFileErrCode = 100033 CopyFileErrCode = 100034 CoreNotFoundErrCode = 100035 + ReadSocketErrCode = 100036 + WriteSocketErrCode = 100037 ) func NewUnknownErr() *KvErr { @@ -229,3 +231,11 @@ func NewCopyFileErr() *KvErr { func NewCoreNotFoundErr() *KvErr { return &KvErr{msg: "core not found", code: CoreNotFoundErrCode} } + +func NewReadSocketErr() *KvErr { + return &KvErr{msg: "read socket failed", code: ReadSocketErrCode} +} + +func NewWriteSocketErr() *KvErr { + return &KvErr{msg: "write socket failed", code: WriteSocketErrCode} +} diff --git a/storage/cli/cli.go b/storage/cli/cli.go index 40a4c7f..71708aa 100644 --- a/storage/cli/cli.go +++ b/storage/cli/cli.go @@ -132,7 +132,8 @@ func (wrapper *Wrapper) withAction() { log.Fatal(err) } }() - sig := make(chan os.Signal) + // bugfix: 使用缓冲通道避免执行信号处理程序(下面的for)之前有信号到达会被丢弃 + sig := make(chan os.Signal, 5) signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM) for range sig { log.Info("shutdown...") diff --git a/storage/core/ragdoll/data.go b/storage/core/ragdoll/data.go index 1ce0498..cbeff34 100644 --- a/storage/core/ragdoll/data.go +++ b/storage/core/ragdoll/data.go @@ -31,10 +31,6 @@ func (r *Record) signature() [16]byte { return md5.Sum(append(append(buffer, []byte(r.Key)...), r.Value...)) } -func (r *Record) checkIntegrity() bool { - return r.CheckSum == r.signature() -} - // Data 磁盘中的数据文件 type Data struct { Fd *os.File diff --git a/storage/core/ragdoll/kv.go b/storage/core/ragdoll/kv.go index 09573b3..f7a6833 100644 --- a/storage/core/ragdoll/kv.go +++ b/storage/core/ragdoll/kv.go @@ -1,9 +1,7 @@ package ragdoll import ( - "errors" "github.com/Trinoooo/eggie_kv/consts" - errs "github.com/Trinoooo/eggie_kv/errs" "github.com/Trinoooo/eggie_kv/storage/core/iface" "github.com/Trinoooo/eggie_kv/storage/core/ragdoll/wal" "github.com/spf13/viper" @@ -19,7 +17,6 @@ type KV struct { } func New(config *viper.Viper) (iface.ICore, error) { - var kvErr *errs.KvErr data, err := NewData("") if err != nil { return nil, err @@ -27,8 +24,6 @@ func New(config *viper.Viper) (iface.ICore, error) { wal, err := wal.Open("123", wal.NewOptions()) if err != nil { - if errors.As(err, &kvErr) { - } return nil, err } diff --git a/storage/server/mw.go b/storage/server/mw.go index 4e1368e..451c768 100644 --- a/storage/server/mw.go +++ b/storage/server/mw.go @@ -15,9 +15,9 @@ type MiddlewareFunc func(handleFn HandleFunc) HandleFunc func LogMw(handleFn HandleFunc) HandleFunc { return func(req *consts.KvRequest) (*consts.KvResponse, error) { - logs.Info(fmt.Sprintf("req: %#.all-contributorsrc", render.Render(req))) + logs.Info(fmt.Sprintf("req: %#v", render.Render(req))) resp, err := handleFn(req) - logs.Info(fmt.Sprintf("resp: %#.all-contributorsrc, errs: %#.all-contributorsrc", render.Render(resp), err)) + logs.Info(fmt.Sprintf("resp: %#v, errs: %#v", render.Render(resp), err)) return resp, err } } diff --git a/storage/server/server.go b/storage/server/server.go index dac2016..0ec7697 100644 --- a/storage/server/server.go +++ b/storage/server/server.go @@ -3,12 +3,12 @@ package server import ( "encoding/json" "errors" + "fmt" "github.com/Trinoooo/eggie_kv/consts" "github.com/Trinoooo/eggie_kv/errs" "github.com/Trinoooo/eggie_kv/storage/core" "github.com/Trinoooo/eggie_kv/storage/core/iface" "github.com/Trinoooo/eggie_kv/storage/logs" - log "github.com/sirupsen/logrus" "github.com/spf13/viper" "io" "net/http" @@ -49,14 +49,14 @@ func NewServer() (*Server, error) { func (srv *Server) Server(resp http.ResponseWriter, req *http.Request) { kvReq, err := parseKvReq(req) if err != nil { - log.Error("parse kvReq errs:", err) + logs.Error(fmt.Sprintf("parse kvReq errs: %#v", err)) _, _ = resp.Write(mustMarshalKvResp(newExceptionResp(err))) return } handler, ok := srv.operatorHandlers[kvReq.OperationType] if !ok { - log.Warn("unsupported operation type:", kvReq.OperationType) + logs.Warn(fmt.Sprintf("unsupported operation type: %#v", kvReq.OperationType)) _, _ = resp.Write(mustMarshalKvResp(newExceptionResp(errs.NewUnsupportedOperatorTypeErr()))) return } @@ -68,7 +68,7 @@ func (srv *Server) Server(resp http.ResponseWriter, req *http.Request) { kvResp, err := wrappedHandler(kvReq) if err != nil { - log.Error("execute handle failed:", err) + logs.Error(fmt.Sprintf("execute handle failed: %#v", err)) _, _ = resp.Write(mustMarshalKvResp(newExceptionResp(err))) return } @@ -115,6 +115,12 @@ func parseKvReq(req *http.Request) (*consts.KvRequest, error) { kvReq := &consts.KvRequest{} bodyBytes, err := io.ReadAll(req.Body) + if err != nil { + e := errs.NewReadSocketErr().WithErr(err) + logs.Error(e.Error()) + return nil, e + } + if err = json.Unmarshal(bodyBytes, kvReq); err != nil { e := errs.NewJsonUnmarshalErr().WithErr(err) logs.Error(e.Error()) @@ -127,7 +133,6 @@ func parseKvReq(req *http.Request) (*consts.KvRequest, error) { func mustMarshalKvResp(resp *consts.KvResponse) []byte { respBytes, err := json.Marshal(resp) if err != nil { - log.Error("json marshal errs:", err) panic(err) } return respBytes diff --git a/utils/file_test.go b/utils/file_test.go index 922c734..f85ce4e 100644 --- a/utils/file_test.go +++ b/utils/file_test.go @@ -28,7 +28,7 @@ func TestCheckAndCreateFile(t *testing.T) { }, { Description: "abs path & dir exist & not have dir permission", - Path: fmt.Sprintf("/tmp/eggie_kv/test_data/f4"), + Path: "/tmp/eggie_kv/test_data/f4", }, } From 7962974da7f6108c1a8009eeac2cfd64bc94d0b3 Mon Sep 17 00:00:00 2001 From: Trino Date: Fri, 16 Feb 2024 21:56:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E4=BB=A3=E7=A0=81=E4=B8=AD=E4=B8=8D=E7=AC=A6=E5=90=88?= =?UTF-8?q?lint=E8=A7=84=E8=8C=83=E7=9A=84=E9=83=A8=E5=88=86=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9page=20actions=E4=BD=BF=E5=85=B6=E5=8F=AA?= =?UTF-8?q?=E5=9C=A8main=E5=88=86=E6=94=AF=E6=9C=89=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E8=A7=A6=E5=8F=91=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pages.yaml | 2 +- storage/cli/cli.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pages.yaml b/.github/workflows/pages.yaml index d5b4352..f50797c 100644 --- a/.github/workflows/pages.yaml +++ b/.github/workflows/pages.yaml @@ -1,7 +1,7 @@ name: Page on: - pull_request: + push: branches: - main # default branch diff --git a/storage/cli/cli.go b/storage/cli/cli.go index 71708aa..c5116f4 100644 --- a/storage/cli/cli.go +++ b/storage/cli/cli.go @@ -134,7 +134,7 @@ func (wrapper *Wrapper) withAction() { }() // bugfix: 使用缓冲通道避免执行信号处理程序(下面的for)之前有信号到达会被丢弃 sig := make(chan os.Signal, 5) - signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM) + signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) for range sig { log.Info("shutdown...") }