From dd11eded5c63d02a257feab576ed78632b221ffb Mon Sep 17 00:00:00 2001 From: tidwall Date: Mon, 12 Sep 2022 17:06:27 -0700 Subject: [PATCH] Cleanup code for Go 1.19 --- cmd/tile38-benchmark/main.go | 2 +- cmd/tile38-luamemtest/main.go | 9 ++++----- cmd/tile38-server/main.go | 7 +++---- core/commands.go | 2 +- core/gen.sh | 2 +- go.mod | 2 +- go.sum | 3 ++- internal/deadline/deadline.go | 1 + internal/endpoint/http.go | 3 +-- internal/endpoint/kafka.go | 3 +-- internal/endpoint/mqtt.go | 4 ++-- internal/glob/match.go | 1 - internal/log/log_test.go | 6 +++--- internal/server/config.go | 15 ++++----------- internal/server/follow.go | 3 +-- internal/server/hooks.go | 4 +--- internal/server/metrics.go | 7 ++++--- internal/server/scanner.go | 2 +- internal/server/scanner_test.go | 3 +-- internal/server/stats_cpu.go | 2 +- internal/server/stats_cpu_darlin.go | 2 +- 21 files changed, 35 insertions(+), 48 deletions(-) diff --git a/cmd/tile38-benchmark/main.go b/cmd/tile38-benchmark/main.go index 45c41918..f0f0b931 100644 --- a/cmd/tile38-benchmark/main.go +++ b/cmd/tile38-benchmark/main.go @@ -221,7 +221,7 @@ func main() { } subtract = true for i := range testsArr { - if strings.ToLower(testsArr[i]) == strings.ToLower(test[1:]) { + if strings.EqualFold(testsArr[i], test[1:]) { testsArr = append(testsArr[:i], testsArr[i+1:]...) break } diff --git a/cmd/tile38-luamemtest/main.go b/cmd/tile38-luamemtest/main.go index 54c4d29c..1880c401 100644 --- a/cmd/tile38-luamemtest/main.go +++ b/cmd/tile38-luamemtest/main.go @@ -9,12 +9,11 @@ import ( "runtime" "runtime/debug" - "github.com/tidwall/resp" - "github.com/yuin/gopher-lua" "strings" -) -var errCmdNotSupported = errors.New("command not supported in scripts") + "github.com/tidwall/resp" + lua "github.com/yuin/gopher-lua" +) func Sha1Sum(s string) string { h := sha1.New() @@ -271,7 +270,7 @@ func testLua() { start = runMany(luaState, start, 100) printMemStats() - start = runMany(luaState, start, 1000) + _ = runMany(luaState, start, 1000) printMemStats() luaState.Close() diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index 5a583767..cbc3d241 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net" "net/http" _ "net/http/pprof" @@ -108,7 +107,7 @@ Developer Options: log.Fatal(err) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { log.Fatal(err) } @@ -294,7 +293,7 @@ Developer Options: var logw io.Writer = os.Stderr if quiet { - logw = ioutil.Discard + logw = io.Discard } log.SetOutput(logw) @@ -390,7 +389,7 @@ Developer Options: } defer pidcleanup() if pidfile != "" { - ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) + os.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666) } c := make(chan os.Signal, 1) diff --git a/core/commands.go b/core/commands.go index 5e98c021..f90c8b9d 100644 --- a/core/commands.go +++ b/core/commands.go @@ -1,4 +1,4 @@ -// +build ignore +//go:build ignore package core diff --git a/core/gen.sh b/core/gen.sh index 04c33b7d..4ff8c549 100755 --- a/core/gen.sh +++ b/core/gen.sh @@ -13,5 +13,5 @@ perl -pe ' ' commands.go > commands_gen.go # remove the ignore -sed -i -e 's/\/\/ +build ignore/\/\/ This file was autogenerated. DO NOT EDIT./g' commands_gen.go +sed -i -e 's/\/\/go:build ignore/\/\/ This file was autogenerated. DO NOT EDIT./g' commands_gen.go rm -rf commands_gen.go-e diff --git a/go.mod b/go.mod index 81c01ae4..b8ae3b9a 100644 --- a/go.mod +++ b/go.mod @@ -107,7 +107,7 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.6.6 // indirect google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect - google.golang.org/protobuf v1.26.0 // indirect + google.golang.org/protobuf v1.28.1 // indirect honnef.co/go/tools v0.0.1-2020.1.4 // indirect ) diff --git a/go.sum b/go.sum index d7dff071..f372d7b8 100644 --- a/go.sum +++ b/go.sum @@ -689,8 +689,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/deadline/deadline.go b/internal/deadline/deadline.go index db95f080..f6047ad2 100644 --- a/internal/deadline/deadline.go +++ b/internal/deadline/deadline.go @@ -14,6 +14,7 @@ func New(dl time.Time) *Deadline { } // Check the deadline and panic when reached +// //go:noinline func (dl *Deadline) Check() { if dl == nil || dl.unixNano == 0 { diff --git a/internal/endpoint/http.go b/internal/endpoint/http.go index cae7eb7f..52deba24 100644 --- a/internal/endpoint/http.go +++ b/internal/endpoint/http.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "time" ) @@ -54,7 +53,7 @@ func (conn *HTTPConn) Send(msg string) error { // close the connection to reuse it defer resp.Body.Close() // discard response - if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { + if _, err := io.Copy(io.Discard, resp.Body); err != nil { return err } // Only allow responses with status code 200, 201, and 202 diff --git a/internal/endpoint/kafka.go b/internal/endpoint/kafka.go index 2793e0da..ef083380 100644 --- a/internal/endpoint/kafka.go +++ b/internal/endpoint/kafka.go @@ -5,7 +5,6 @@ import ( "crypto/x509" "errors" "fmt" - "io/ioutil" "os" "sync" "time" @@ -192,7 +191,7 @@ func loadClientTLSCert(KeyFile, CertFile string) ([]tls.Certificate, error) { func loadRootTLSCert(CACertFile string) (x509.CertPool, error) { // Load CA cert - caCert, err := ioutil.ReadFile(CACertFile) + caCert, err := os.ReadFile(CACertFile) if err != nil { return x509.CertPool{}, err diff --git a/internal/endpoint/mqtt.go b/internal/endpoint/mqtt.go index b755e40c..5f80765c 100644 --- a/internal/endpoint/mqtt.go +++ b/internal/endpoint/mqtt.go @@ -4,8 +4,8 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "math/rand" + "os" "sync" "time" @@ -76,7 +76,7 @@ func (conn *MQTTConn) Send(msg string) error { } if conn.ep.MQTT.CACertFile != "" { // Load CA cert - caCert, err := ioutil.ReadFile(conn.ep.MQTT.CACertFile) + caCert, err := os.ReadFile(conn.ep.MQTT.CACertFile) if err != nil { return err } diff --git a/internal/glob/match.go b/internal/glob/match.go index 3c290ee5..61d65071 100644 --- a/internal/glob/match.go +++ b/internal/glob/match.go @@ -36,7 +36,6 @@ var ErrBadPattern = errors.New("syntax error in pattern") // // On Windows, escaping is disabled. Instead, '\\' is treated as // path separator. -// func wildcardMatch(pattern, name string) (matched bool, err error) { Pattern: for len(pattern) > 0 { diff --git a/internal/log/log_test.go b/internal/log/log_test.go index f1169615..6c86128d 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -2,7 +2,7 @@ package log import ( "bytes" - "io/ioutil" + "io" "strings" "testing" @@ -189,7 +189,7 @@ func TestLogJSON(t *testing.T) { func BenchmarkLogPrintf(t *testing.B) { LogJSON = false Level = 1 - SetOutput(ioutil.Discard) + SetOutput(io.Discard) t.ResetTimer() for i := 0; i < t.N; i++ { Printf("X %s", "Y") @@ -208,7 +208,7 @@ func BenchmarkLogJSONPrintf(t *testing.B) { logger := zap.New( zapcore.NewCore( enc, - zapcore.AddSync(ioutil.Discard), + zapcore.AddSync(io.Discard), zap.DebugLevel, )).Sugar() diff --git a/internal/server/config.go b/internal/server/config.go index 31aca1d0..b29cfc1d 100644 --- a/internal/server/config.go +++ b/internal/server/config.go @@ -2,7 +2,6 @@ package server import ( "encoding/json" - "io/ioutil" "os" "strconv" "strings" @@ -71,7 +70,7 @@ type Config struct { func loadConfig(path string) (*Config, error) { var json string - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { if !os.IsNotExist(err) { return nil, err @@ -217,7 +216,7 @@ func (config *Config) write(writeProperties bool) { panic(err) } data = append(data, '\n') - err = ioutil.WriteFile(config.path, data, 0600) + err = os.WriteFile(config.path, data, 0600) if err != nil { panic(err) } @@ -327,11 +326,11 @@ func (config *Config) setProperty(name, value string, fromLoad bool) error { config._logConfig = value } case ReplicaPriority: - replicaPriority, err := strconv.ParseUint(value, 10, 64) + replicaPriority, err := strconv.ParseInt(value, 10, 64) if err != nil || replicaPriority < 0 { invalid = true } else { - config._replicaPriority = int64(replicaPriority) + config._replicaPriority = replicaPriority } } @@ -522,9 +521,3 @@ func (config *Config) setReadOnly(v bool) { config._readOnly = v config.mu.Unlock() } -func (config *Config) logConfig() string { - config.mu.RLock() - v := config._logConfig - config.mu.RUnlock() - return v -} diff --git a/internal/server/follow.go b/internal/server/follow.go index 7d9f0b16..8367e2fd 100644 --- a/internal/server/follow.go +++ b/internal/server/follow.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "strconv" "strings" "time" @@ -272,7 +271,7 @@ func (s *Server) followStep(host string, port int, followc int) error { s.mu.Unlock() log.Info("caught up") } - nullw := ioutil.Discard + nullw := io.Discard for { v, telnet, _, err := conn.rd.ReadMultiBulk() if err != nil { diff --git a/internal/server/hooks.go b/internal/server/hooks.go index 4e490d77..bebcaef2 100644 --- a/internal/server/hooks.go +++ b/internal/server/hooks.go @@ -113,9 +113,7 @@ func (s *Server) cmdSetHook(msg *Message) ( cmsg := &Message{} *cmsg = *msg cmsg.Args = make([]string, len(commandvs)) - for i := 0; i < len(commandvs); i++ { - cmsg.Args[i] = commandvs[i] - } + copy(cmsg.Args, commandvs) metas := make([]FenceMeta, 0, len(metaMap)) for key, val := range metaMap { metas = append(metas, FenceMeta{key, val}) diff --git a/internal/server/metrics.go b/internal/server/metrics.go index aa7702d2..01049481 100644 --- a/internal/server/metrics.go +++ b/internal/server/metrics.go @@ -7,6 +7,7 @@ import ( "github.com/tidwall/tile38/core" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -65,9 +66,9 @@ func (s *Server) MetricsHandler(w http.ResponseWriter, r *http.Request) { reg := prometheus.NewRegistry() reg.MustRegister( - prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}), - prometheus.NewGoCollector(), - prometheus.NewBuildInfoCollector(), + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + collectors.NewGoCollector(), + collectors.NewBuildInfoCollector(), cmdDurations, s, ) diff --git a/internal/server/scanner.go b/internal/server/scanner.go index 3053ca93..02a771af 100644 --- a/internal/server/scanner.go +++ b/internal/server/scanner.go @@ -380,7 +380,7 @@ func (sw *scanWriter) testObject(id string, o geojson.Object, fields []float64) return ok, true, nf } -//id string, o geojson.Object, fields []float64, noLock bool +// id string, o geojson.Object, fields []float64, noLock bool func (sw *scanWriter) writeObject(opts ScanWriterParams) bool { if !opts.noLock { sw.mu.Lock() diff --git a/internal/server/scanner_test.go b/internal/server/scanner_test.go index 31f186a2..e092cd8a 100644 --- a/internal/server/scanner_test.go +++ b/internal/server/scanner_test.go @@ -19,14 +19,13 @@ func PO(x, y float64) *geojson.Point { return geojson.NewPoint(geometry.Point{X: x, Y: y}) } - func BenchmarkFieldMatch(t *testing.B) { rand.Seed(time.Now().UnixNano()) items := make([]testPointItem, t.N) for i := 0; i < t.N; i++ { items[i] = testPointItem{ PO(rand.Float64()*360-180, rand.Float64()*180-90), - []float64{rand.Float64()*9+1, math.Round(rand.Float64()*30) + 1}, + []float64{rand.Float64()*9 + 1, math.Round(rand.Float64()*30) + 1}, } } sw := &scanWriter{ diff --git a/internal/server/stats_cpu.go b/internal/server/stats_cpu.go index fdcf9537..c3f23ccf 100644 --- a/internal/server/stats_cpu.go +++ b/internal/server/stats_cpu.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin +//go:build !linux && !darwin package server diff --git a/internal/server/stats_cpu_darlin.go b/internal/server/stats_cpu_darlin.go index a185bb46..de58c1fb 100644 --- a/internal/server/stats_cpu_darlin.go +++ b/internal/server/stats_cpu_darlin.go @@ -1,4 +1,4 @@ -// +build linux darwin +//go:build linux || darwin package server