diff --git a/core/bloom/bloom.go b/core/bloom/bloom.go index 22793878..f6d437ad 100644 --- a/core/bloom/bloom.go +++ b/core/bloom/bloom.go @@ -37,7 +37,6 @@ type ( BloomFilter struct { bits uint - maps uint bitSet BitSetProvider } ) diff --git a/core/codec/gzip.go b/core/codec/gzip.go index d93cf072..0486feac 100644 --- a/core/codec/gzip.go +++ b/core/codec/gzip.go @@ -6,6 +6,8 @@ import ( "io" ) +const unzipLimit = 100 * 1024 * 1024 // 100MB + func Gzip(bs []byte) []byte { var b bytes.Buffer @@ -24,8 +26,7 @@ func Gunzip(bs []byte) ([]byte, error) { defer r.Close() var c bytes.Buffer - _, err = io.Copy(&c, r) - if err != nil { + if _, err = io.Copy(&c, io.LimitReader(r, unzipLimit)); err != nil { return nil, err } diff --git a/core/codec/rsa_test.go b/core/codec/rsa_test.go index dbe9fd1d..8225a7c2 100644 --- a/core/codec/rsa_test.go +++ b/core/codec/rsa_test.go @@ -30,8 +30,7 @@ FstHGSkUYFLe+nl1dEKHbD+/Zt95L757J3xGTrwoTc7KCTxbrgn+stn0w52BNjj/ kIE2ko4lbh/v8Fl14AyVR9msfKtKOnhe5FCT72mdtApr+qvzcC3q9hfXwkyQU32p v7q5UimZ205iKSBmgQIDAQAB -----END PUBLIC KEY-----` - testBody = `this is the content` - encryptedBody = `49e7bc15640e5d927fd3f129b749536d0755baf03a0f35fc914ff1b7b8ce659e5fe3a598442eb908c5995e28bacd3d76e4420bb05b6bfc177040f66c6976f680f7123505d626ab96a9db1151f45c93bc0262db9087b9fb6801715f76f902e644a20029262858f05b0d10540842204346ac1d6d8f29cc5d47dab79af75d922ef2` + testBody = `this is the content` ) func TestCryption(t *testing.T) { diff --git a/core/collection/cache.go b/core/collection/cache.go index 1acf7a01..e95a844e 100644 --- a/core/collection/cache.go +++ b/core/collection/cache.go @@ -29,7 +29,6 @@ type ( name string lock sync.Mutex data map[string]interface{} - evicts *list.List expire time.Duration timingWheel *TimingWheel lruCache lru diff --git a/core/collection/rollingwindow_test.go b/core/collection/rollingwindow_test.go index 1cdd0fbb..9bb88845 100644 --- a/core/collection/rollingwindow_test.go +++ b/core/collection/rollingwindow_test.go @@ -88,7 +88,7 @@ func TestRollingWindowReduce(t *testing.T) { for _, test := range tests { t.Run(stringx.Rand(), func(t *testing.T) { r := test.win - for x := 0; x < size; x = x + 1 { + for x := 0; x < size; x++ { for i := 0; i <= x; i++ { r.Add(float64(i)) } diff --git a/core/contextx/deadline_test.go b/core/contextx/deadline_test.go index c20a374a..d33adfba 100644 --- a/core/contextx/deadline_test.go +++ b/core/contextx/deadline_test.go @@ -10,8 +10,10 @@ import ( func TestShrinkDeadlineLess(t *testing.T) { deadline := time.Now().Add(time.Second) - ctx, _ := context.WithDeadline(context.Background(), deadline) - ctx, _ = ShrinkDeadline(ctx, time.Minute) + ctx, cancel := context.WithDeadline(context.Background(), deadline) + defer cancel() + ctx, cancel = ShrinkDeadline(ctx, time.Minute) + defer cancel() dl, ok := ctx.Deadline() assert.True(t, ok) assert.Equal(t, deadline, dl) @@ -19,8 +21,10 @@ func TestShrinkDeadlineLess(t *testing.T) { func TestShrinkDeadlineMore(t *testing.T) { deadline := time.Now().Add(time.Minute) - ctx, _ := context.WithDeadline(context.Background(), deadline) - ctx, _ = ShrinkDeadline(ctx, time.Second) + ctx, cancel := context.WithDeadline(context.Background(), deadline) + defer cancel() + ctx, cancel = ShrinkDeadline(ctx, time.Second) + defer cancel() dl, ok := ctx.Deadline() assert.True(t, ok) assert.True(t, dl.Before(deadline)) diff --git a/core/contextx/valueonlycontext_test.go b/core/contextx/valueonlycontext_test.go index d9732e5d..a2fecc05 100644 --- a/core/contextx/valueonlycontext_test.go +++ b/core/contextx/valueonlycontext_test.go @@ -12,7 +12,8 @@ func TestContextCancel(t *testing.T) { c := context.WithValue(context.Background(), "key", "value") c1, cancel := context.WithCancel(c) o := ValueOnlyFrom(c1) - c2, _ := context.WithCancel(o) + c2, cancel2 := context.WithCancel(o) + defer cancel2() contexts := []context.Context{c1, c2} for _, c := range contexts { @@ -35,7 +36,8 @@ func TestContextCancel(t *testing.T) { } func TestContextDeadline(t *testing.T) { - c, _ := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + c, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + cancel() o := ValueOnlyFrom(c) select { case <-time.After(100 * time.Millisecond): diff --git a/core/discov/clients.go b/core/discov/clients.go index 82242b91..c97555da 100644 --- a/core/discov/clients.go +++ b/core/discov/clients.go @@ -8,7 +8,7 @@ import ( ) const ( - indexOfKey = iota + _ = iota indexOfId ) diff --git a/core/mathx/int_test.go b/core/mathx/int_test.go index e8312ae3..a5e1e76f 100644 --- a/core/mathx/int_test.go +++ b/core/mathx/int_test.go @@ -31,6 +31,7 @@ func TestMaxInt(t *testing.T) { } for _, each := range cases { + each := each t.Run(stringx.Rand(), func(t *testing.T) { actual := MaxInt(each.a, each.b) assert.Equal(t, each.expect, actual) diff --git a/core/proc/profile.go b/core/proc/profile.go index 062fa202..a1483e71 100644 --- a/core/proc/profile.go +++ b/core/proc/profile.go @@ -26,10 +26,6 @@ var started uint32 // Profile represents an active profiling session. type Profile struct { - // path holds the base path where various profiling files are written. - // If blank, the base path will be generated by ioutil.TempDir. - path string - // closers holds cleanup functions that run after each profile closers []func() diff --git a/core/service/servicegroup_test.go b/core/service/servicegroup_test.go index 00defaa1..7f2165dd 100644 --- a/core/service/servicegroup_test.go +++ b/core/service/servicegroup_test.go @@ -27,7 +27,7 @@ func newMockedService(multiplier int) *mockedService { func (s *mockedService) Start() { mutex.Lock() - number = number * s.multiplier + number *= s.multiplier mutex.Unlock() done <- struct{}{} <-s.quit diff --git a/core/syncx/cond_test.go b/core/syncx/cond_test.go index 04ee90d1..c7e7c283 100644 --- a/core/syncx/cond_test.go +++ b/core/syncx/cond_test.go @@ -67,7 +67,3 @@ func TestSignalNoWait(t *testing.T) { func sleep(millisecond int) { time.Sleep(time.Duration(millisecond) * time.Millisecond) } - -func currentTimeMillis() int64 { - return time.Now().UnixNano() / int64(time.Millisecond) -} diff --git a/core/syncx/sharedcalls_test.go b/core/syncx/sharedcalls_test.go index 63772c33..d6b52fb9 100644 --- a/core/syncx/sharedcalls_test.go +++ b/core/syncx/sharedcalls_test.go @@ -95,7 +95,8 @@ func TestExclusiveCallDoDiffDupSuppress(t *testing.T) { close(broadcast) wg.Wait() - if got := atomic.LoadInt32(&calls); got != 5 { // five letters + if got := atomic.LoadInt32(&calls); got != 5 { + // five letters t.Errorf("number of calls = %d; want 5", got) } } diff --git a/core/timex/relativetime_test.go b/core/timex/relativetime_test.go index fceb387b..386f6d2f 100644 --- a/core/timex/relativetime_test.go +++ b/core/timex/relativetime_test.go @@ -16,7 +16,7 @@ func TestRelativeTime(t *testing.T) { } func TestRelativeTime_Time(t *testing.T) { - diff := Time().Sub(time.Now()) + diff := time.Until(Time()) if diff > 0 { assert.True(t, diff < time.Second) } else { diff --git a/core/timex/ticker_test.go b/core/timex/ticker_test.go index 5f10413e..3acaf4e0 100644 --- a/core/timex/ticker_test.go +++ b/core/timex/ticker_test.go @@ -27,12 +27,9 @@ func TestFakeTicker(t *testing.T) { var count int32 go func() { - for { - select { - case <-ticker.Chan(): - if atomic.AddInt32(&count, 1) == total { - ticker.Done() - } + for range ticker.Chan() { + if atomic.AddInt32(&count, 1) == total { + ticker.Done() } } }() diff --git a/core/trace/span.go b/core/trace/span.go index 014ac29e..7c73f39c 100644 --- a/core/trace/span.go +++ b/core/trace/span.go @@ -17,7 +17,6 @@ const ( clientFlag = "client" serverFlag = "server" spanSepRune = '.' - timeFormat = "2006-01-02 15:04:05.000" ) var spanSep = string([]byte{spanSepRune}) @@ -37,9 +36,7 @@ func newServerSpan(carrier Carrier, serviceName, operationName string) tracespec return carrier.Get(traceIdKey) } return "" - }, func() string { - return stringx.RandId() - }) + }, stringx.RandId) spanId := stringx.TakeWithPriority(func() string { if carrier != nil { return carrier.Get(spanIdKey) diff --git a/core/utils/version_test.go b/core/utils/version_test.go index 01b3ef63..676b88c2 100644 --- a/core/utils/version_test.go +++ b/core/utils/version_test.go @@ -30,6 +30,7 @@ func TestCompareVersions(t *testing.T) { } for _, each := range cases { + each := each t.Run(each.ver1, func(t *testing.T) { actual := CompareVersions(each.ver1, each.operator, each.ver2) assert.Equal(t, each.out, actual, fmt.Sprintf("%s vs %s", each.ver1, each.ver2)) diff --git a/example/etcd/demo/etcdmon.go b/example/etcd/demo/etcdmon.go index 40cbe48a..3895b458 100644 --- a/example/etcd/demo/etcdmon.go +++ b/example/etcd/demo/etcdmon.go @@ -128,41 +128,38 @@ func main() { ticker := time.NewTicker(time.Minute) defer ticker.Stop() - for { - select { - case <-ticker.C: - expect, err := loadAll(registry.Client().(*clientv3.Client)) - if err != nil { - fmt.Println("[ETCD-test] can't load current keys") - continue - } + for range ticker.C { + expect, err := loadAll(registry.Client().(*clientv3.Client)) + if err != nil { + fmt.Println("[ETCD-test] can't load current keys") + continue + } - check := func() bool { - var match bool - barrier.Guard(func() { - match = compare(expect, vals) - }) - if match { - logx.Info("match") - } - return match + check := func() bool { + var match bool + barrier.Guard(func() { + match = compare(expect, vals) + }) + if match { + logx.Info("match") } + return match + } + if check() { + continue + } + + time.AfterFunc(time.Second*5, func() { if check() { - continue + return } - time.AfterFunc(time.Second*5, func() { - if check() { - return - } - - var builder strings.Builder - builder.WriteString(fmt.Sprintf("expect:\n%s\n", serializeMap(expect, "\t"))) - barrier.Guard(func() { - builder.WriteString(fmt.Sprintf("actual:\n%s\n", serializeMap(vals, "\t"))) - }) - fmt.Println(builder.String()) + var builder strings.Builder + builder.WriteString(fmt.Sprintf("expect:\n%s\n", serializeMap(expect, "\t"))) + barrier.Guard(func() { + builder.WriteString(fmt.Sprintf("actual:\n%s\n", serializeMap(vals, "\t"))) }) - } + fmt.Println(builder.String()) + }) } } diff --git a/example/mapreduce/deadlock/main.go b/example/mapreduce/deadlock/main.go index 60d3fd13..422014ad 100644 --- a/example/mapreduce/deadlock/main.go +++ b/example/mapreduce/deadlock/main.go @@ -26,10 +26,6 @@ func main() { } writer.Write(user) }, func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) { - var users []*User - for p := range pipe { - users = append(users, p.(*User)) - } // missing writer.Write(...), should not panic }) if err != nil { diff --git a/example/periodicalexecutor/pe.go b/example/periodicalexecutor/pe.go index cbd720dc..e32aef72 100644 --- a/example/periodicalexecutor/pe.go +++ b/example/periodicalexecutor/pe.go @@ -12,7 +12,11 @@ func main() { fmt.Println(len(items)) }, executors.WithBulkTasks(10)) for { - executor.Add(1) + if err := executor.Add(1); err != nil { + fmt.Println(err) + return + } + time.Sleep(time.Millisecond * 90) } } diff --git a/example/stat/main.go b/example/stat/main.go index 8eb449b5..8b869f88 100644 --- a/example/stat/main.go +++ b/example/stat/main.go @@ -24,11 +24,8 @@ func main() { ticker := time.NewTicker(time.Second * 5) defer ticker.Stop() - for { - select { - case <-ticker.C: - percent := stat.CpuUsage() - fmt.Println("cpu:", percent) - } + for range ticker.C { + percent := stat.CpuUsage() + fmt.Println("cpu:", percent) } } diff --git a/rest/handler/cryptionhandler.go b/rest/handler/cryptionhandler.go index e83132c1..efa73bcf 100644 --- a/rest/handler/cryptionhandler.go +++ b/rest/handler/cryptionhandler.go @@ -45,7 +45,7 @@ func decryptBody(key []byte, r *http.Request) error { var content []byte var err error if r.ContentLength > 0 { - content = make([]byte, r.ContentLength, r.ContentLength) + content = make([]byte, r.ContentLength) _, err = io.ReadFull(r.Body, content) } else { content, err = ioutil.ReadAll(io.LimitReader(r.Body, maxBytes)) diff --git a/tools/goctl/k8s/k8s.go b/tools/goctl/k8s/k8s.go index 006ee134..080f5965 100644 --- a/tools/goctl/k8s/k8s.go +++ b/tools/goctl/k8s/k8s.go @@ -18,8 +18,6 @@ const ( ServiceTypeRmq ServiceType = "rmq" ServiceTypeSync ServiceType = "sync" envDev = "dev" - envPre = "pre" - envPro = "pro" ) type ( diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index 4dab8296..2aaea49b 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -9,7 +9,7 @@ import ( ) const ( - none = iota + _ = iota primary unique normal diff --git a/tools/goctl/util/stringx/string.go b/tools/goctl/util/stringx/string.go index 4ea890be..cb8e204d 100644 --- a/tools/goctl/util/stringx/string.go +++ b/tools/goctl/util/stringx/string.go @@ -53,9 +53,7 @@ func (s String) ToCamel() string { // camel->snake func (s String) ToSnake() string { - list := s.splitBy(func(r rune) bool { - return unicode.IsUpper(r) - }, false) + list := s.splitBy(unicode.IsUpper, false) var target []string for _, item := range list { target = append(target, From(item).Lower()) diff --git a/zrpc/internal/resolver/directbuilder_test.go b/zrpc/internal/resolver/directbuilder_test.go index 2aebc8e4..175599e7 100644 --- a/zrpc/internal/resolver/directbuilder_test.go +++ b/zrpc/internal/resolver/directbuilder_test.go @@ -23,6 +23,7 @@ func TestDirectBuilder_Build(t *testing.T) { } for _, test := range tests { + test := test t.Run(strconv.Itoa(test), func(t *testing.T) { var servers []string for i := 0; i < test; i++ { diff --git a/zrpc/internal/resolver/subset_test.go b/zrpc/internal/resolver/subset_test.go index 16ca0a67..cb664cd6 100644 --- a/zrpc/internal/resolver/subset_test.go +++ b/zrpc/internal/resolver/subset_test.go @@ -27,6 +27,7 @@ func TestSubset(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { var vals []string for i := 0; i < test.set; i++ {