Skip to content

Commit

Permalink
Merge pull request #17 from Trinoooo/chore/golint
Browse files Browse the repository at this point in the history
chore(lint):增加golanci-lint
  • Loading branch information
Trinoooo authored Feb 17, 2024
2 parents 075c24a + 7962974 commit 1bbb7df
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 21 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: golangci-lint
on:
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"
1 change: 0 additions & 1 deletion .github/workflows/pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- main # default branch
- doc/restart # test

jobs:
build:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Go Test

on:
push:
pull_request:

jobs:
Expand Down
10 changes: 10 additions & 0 deletions errs/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const (
CreateTempFileErrCode = 100033
CopyFileErrCode = 100034
CoreNotFoundErrCode = 100035
ReadSocketErrCode = 100036
WriteSocketErrCode = 100037
)

func NewUnknownErr() *KvErr {
Expand Down Expand Up @@ -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}
}
5 changes: 3 additions & 2 deletions storage/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ func (wrapper *Wrapper) withAction() {
log.Fatal(err)
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
// bugfix: 使用缓冲通道避免执行信号处理程序(下面的for)之前有信号到达会被丢弃
sig := make(chan os.Signal, 5)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for range sig {
log.Info("shutdown...")
}
Expand Down
4 changes: 0 additions & 4 deletions storage/core/ragdoll/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions storage/core/ragdoll/kv.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,16 +17,13 @@ type KV struct {
}

func New(config *viper.Viper) (iface.ICore, error) {
var kvErr *errs.KvErr
data, err := NewData("")
if err != nil {
return nil, err
}

wal, err := wal.Open("123", wal.NewOptions())
if err != nil {
if errors.As(err, &kvErr) {
}
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions storage/server/mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
15 changes: 10 additions & 5 deletions storage/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down

0 comments on commit 1bbb7df

Please sign in to comment.