Skip to content

Commit

Permalink
Merge pull request ByteStorage#170 from saeid-a/feat/add-lint-check
Browse files Browse the repository at this point in the history
add `gofmt` to GitHub actions (ByteStorage#161)
  • Loading branch information
qishenonly authored Jul 9, 2023
2 parents 462ff34 + dcb6787 commit 84911f8
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 39 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- name: Check Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi

- name: Lint
uses: golangci/golangci-lint-action@v3.6.0
with:
args: --verbose
version: v1.53

- name: Build
run: go build -v ./...

Expand Down
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
run:
timeout: 15m
skip-files:
# Skip autogenerated files.
- ^.*\.(pb|y)\.go$

output:
sort-results: true

linters:
disable-all: true
enable:
- govet
- errcheck
- gosimple
- ineffassign


issues:
max-same-issues: 0
exclude-rules:
- linters:
- gocritic
text: "appendAssign"
- path: _test.go
linters:
- errcheck
- ineffassign

linters-settings:
goimports:
local-prefixes: github.com/prometheus/prometheus
ineffassign:
rules:
-

gofumpt:
extra-rules: true
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
- name: unused-parameter
severity: warning
disabled: true
1 change: 0 additions & 1 deletion engine/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,3 @@ func parseLogRecordKeyAndSeq(key []byte) ([]byte, uint64) {

return realKey, seqNo
}

2 changes: 1 addition & 1 deletion engine/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetValue() []byte {
for i := 0; i < 512; i++ {
str.WriteByte(alphabet[rand.Int()%36])
}
return []byte(str.String())
return str.Bytes()
}

func init() {
Expand Down
6 changes: 1 addition & 5 deletions engine/grpc/service/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ func (s *Service) IsGrpcServerRunning() bool {
return false
}
err = conn.Close()
if err != nil {
return false
}
return true
return err == nil
}

// StartServer starts a grpc server
func (s *Service) StartServer() {
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
panic("tcp listen error: " + err.Error())
return
}
server := grpc.NewServer()
dbs.RegisterFlyDBServiceServer(server, s)
Expand Down
6 changes: 2 additions & 4 deletions engine/index/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ func (bt *BTree) Delete(key []byte) bool {

it := &Item{key: key}
oldItem := bt.tree.Delete(it)
if oldItem == nil {
return false
}
return true

return oldItem != nil
}

func (bt *BTree) Size() int {
Expand Down
2 changes: 1 addition & 1 deletion engine/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (it *Iterator) skipToNext() {
key := it.indexIter.Key()

// Check if the key has the desired prefix
if prefixLen <= len(key) && bytes.Compare(it.options.Prefix, key[:prefixLen]) == 0 {
if prefixLen <= len(key) && bytes.Equal(it.options.Prefix, key[:prefixLen]) {
break
}
}
Expand Down
30 changes: 12 additions & 18 deletions lib/raft/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,26 @@ import (

func (m *Master) ListenSlave() {
timeTick := time.NewTicker(200 * time.Millisecond)
for {
select {
case <-timeTick.C:
for k, t := range m.Heartbeat {
if t.Add(5*time.Second).Unix() < time.Now().Unix() {
delete(m.Heartbeat, k)
}
for range timeTick.C {
for k, t := range m.Heartbeat {
if t.Add(5*time.Second).Unix() < time.Now().Unix() {
delete(m.Heartbeat, k)
}
}
}
}

func (m *Master) WaitForLeader() {
timeTick := time.NewTicker(100 * time.Millisecond)
for {
for range timeTick.C {
ch := m.Raft.LeaderCh()
select {
case <-timeTick.C:
ch := m.Raft.LeaderCh()
select {
case isLeader := <-ch:
if isLeader {
return
}
default:
continue
case isLeader := <-ch:
if isLeader {
return
}
default:
continue
}
}
}
Expand All @@ -63,7 +57,7 @@ func (m *Master) StartGrpcServer() {
}
}()
// graceful shutdown
sig := make(chan os.Signal)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL)

<-sig
Expand Down
14 changes: 7 additions & 7 deletions lib/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ func NewRaftCluster(masterList []string, slaveList []string) *Cluster {
}

func (c *Cluster) startMasters() {
for _, m := range c.Master {
m.c = c
for idx := range c.Master {
c.Master[idx].c = c
//start grpc server
m.StartGrpcServer()
c.Master[idx].StartGrpcServer()
//start raft
m.NewRaft()
c.Master[idx].NewRaft()
//wait for leader
m.WaitForLeader()
c.Master[idx].WaitForLeader()
//add slave or delete slave
go m.ListenSlave()
go c.Master[idx].ListenSlave()
//listen user request, by wal
go m.ListenRequest()
go c.Master[idx].ListenRequest()
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/raft/slave.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *Slave) StartGrpcServer() {
}
}()
// graceful shutdown
sig := make(chan os.Signal)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL)

<-sig
Expand Down
3 changes: 3 additions & 0 deletions lib/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func New() (*Wal, error) {
return &Wal{}, err
}
index, err := log.LastIndex()
if err != nil {
return &Wal{}, err
}
if index == 0 {
err := log.Write(1, []byte("--------------------"))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion protocol/tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config struct {
func ListenAndServeBySignal(cfg *Config, handler tcpIF.Handler) error {
closeChan := make(chan struct{})
// listen system-level signal
signalChan := make(chan os.Signal)
signalChan := make(chan os.Signal, 1)
// syscall.SIGHUP: terminal closed
// syscall.SIGINT: ctrl + c
// syscall.SIGTERM: kill
Expand Down

0 comments on commit 84911f8

Please sign in to comment.