From 8791a8d51ca944100d05dd5556e7188d38a70d12 Mon Sep 17 00:00:00 2001 From: chengshiwen Date: Mon, 5 Aug 2024 23:30:45 +0800 Subject: [PATCH] use revive instead of golint and fix lint warning --- .golangci.yml | 3 +-- Makefile | 2 +- backend/backend.go | 6 +++--- backend/circle.go | 6 +++--- backend/config.go | 4 ++-- backend/http.go | 23 +++++++++++++---------- backend/influxql.go | 6 +++--- backend/lineproto.go | 12 +++++------- backend/proxy.go | 2 +- service/http.go | 34 +++++++++++++++++----------------- transfer/transfer.go | 14 +++++++------- 11 files changed, 56 insertions(+), 56 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 87b4f05..76b1f6c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,7 +2,6 @@ run: allow-parallel-runners: true linters: enable: - - golint + - revive disable: - errcheck - - typecheck diff --git a/Makefile b/Makefile index 595c3ee..5f02b84 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ run: go run main.go lint: - golangci-lint run --enable=golint --disable=errcheck --disable=typecheck && goimports -l -w . && go fmt ./... && go vet ./... + golangci-lint run --config .golangci.yml && goimports -l -w . && go fmt ./... && go vet ./... down: go list ./... && go mod verify diff --git a/backend/backend.go b/backend/backend.go index 3da6dcd..e3be95e 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -263,10 +263,10 @@ func (ib *Backend) Rewrite() (err error) { case nil: case ErrBadRequest: log.Printf("bad request, drop all data") - err = nil + // err = nil case ErrNotFound: log.Printf("bad backend, drop all data") - err = nil + // err = nil default: log.Printf("rewrite http error, url: %s, db: %s, rp: %s, plen: %d", ib.Url, db, rp, len(p[1])) @@ -296,7 +296,7 @@ func (ib *Backend) Close() { func (ib *Backend) GetHealth(ic *Circle, withStats bool) interface{} { health := struct { Name string `json:"name"` - Url string `json:"url"` // nolint:golint + Url string `json:"url"` // nolint:revive Active bool `json:"active"` Backlog bool `json:"backlog"` Rewriting bool `json:"rewriting"` diff --git a/backend/circle.go b/backend/circle.go index b05c3fd..f66dbec 100644 --- a/backend/circle.go +++ b/backend/circle.go @@ -12,7 +12,7 @@ import ( ) type Circle struct { - CircleId int // nolint:golint + CircleId int // nolint:revive Name string Backends []*Backend router *consistent.Consistent @@ -20,7 +20,7 @@ type Circle struct { mapToBackend map[string]*Backend } -func NewCircle(cfg *CircleConfig, pxcfg *ProxyConfig, circleId int) (ic *Circle) { // nolint:golint +func NewCircle(cfg *CircleConfig, pxcfg *ProxyConfig, circleId int) (ic *Circle) { // nolint:revive ic = &Circle{ CircleId: circleId, Name: cfg.Name, @@ -81,7 +81,7 @@ func (ic *Circle) GetHealth(stats bool) interface{} { } wg.Wait() circle := struct { - Id int `json:"id"` // nolint:golint + Id int `json:"id"` // nolint:revive Name string `json:"name"` Active bool `json:"active"` WriteOnly bool `json:"write_only"` diff --git a/backend/config.go b/backend/config.go index 7a59af8..009c472 100644 --- a/backend/config.go +++ b/backend/config.go @@ -27,9 +27,9 @@ var ( ErrInvalidHashKey = errors.New("invalid hash_key, require idx, exi, name or url") ) -type BackendConfig struct { // nolint:golint +type BackendConfig struct { // nolint:revive Name string `mapstructure:"name"` - Url string `mapstructure:"url"` // nolint:golint + Url string `mapstructure:"url"` // nolint:revive Username string `mapstructure:"username"` Password string `mapstructure:"password"` AuthEncrypt bool `mapstructure:"auth_encrypt"` diff --git a/backend/http.go b/backend/http.go index 801c910..b24b746 100644 --- a/backend/http.go +++ b/backend/http.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -43,11 +42,11 @@ type QueryResult struct { Err error } -type HttpBackend struct { // nolint:golint +type HttpBackend struct { // nolint:revive client *http.Client transport *http.Transport Name string - Url string // nolint:golint + Url string // nolint:revive username string password string authEncrypt bool @@ -59,7 +58,7 @@ type HttpBackend struct { // nolint:golint writeOnly bool } -func NewHttpBackend(cfg *BackendConfig, pxcfg *ProxyConfig) (hb *HttpBackend) { // nolint:golint +func NewHttpBackend(cfg *BackendConfig, pxcfg *ProxyConfig) (hb *HttpBackend) { // nolint:revive hb = NewSimpleHttpBackend(cfg) hb.client = NewClient(strings.HasPrefix(cfg.Url, "https"), pxcfg.WriteTimeout) hb.interval = pxcfg.CheckInterval @@ -67,7 +66,7 @@ func NewHttpBackend(cfg *BackendConfig, pxcfg *ProxyConfig) (hb *HttpBackend) { return } -func NewSimpleHttpBackend(cfg *BackendConfig) (hb *HttpBackend) { // nolint:golint +func NewSimpleHttpBackend(cfg *BackendConfig) (hb *HttpBackend) { // nolint:revive hb = &HttpBackend{ transport: NewTransport(strings.HasPrefix(cfg.Url, "https")), Name: cfg.Name, @@ -119,7 +118,7 @@ func NewQueryRequest(method, db, q, epoch string) *http.Request { func CloneQueryRequest(r *http.Request) *http.Request { cr := r.Clone(r.Context()) - cr.Body = ioutil.NopCloser(&bytes.Buffer{}) + cr.Body = io.NopCloser(&bytes.Buffer{}) return cr } @@ -227,6 +226,10 @@ func (hb *HttpBackend) WriteStream(db, rp string, stream io.Reader, compressed b q.Set("db", db) q.Set("rp", rp) req, err := http.NewRequest("POST", hb.Url+"/write?"+q.Encode(), stream) + if err != nil { + log.Print("new request error: ", err) + return nil + } if hb.username != "" || hb.password != "" { hb.SetBasicAuth(req) } @@ -247,7 +250,7 @@ func (hb *HttpBackend) WriteStream(db, rp string, stream io.Reader, compressed b } log.Printf("write status code: %d, from: %s", resp.StatusCode, hb.Url) - respbuf, err := ioutil.ReadAll(resp.Body) + respbuf, err := io.ReadAll(resp.Body) if err != nil { log.Print("readall error: ", err) return @@ -297,7 +300,7 @@ func (hb *HttpBackend) ReadProm(req *http.Request, w http.ResponseWriter) (err e CopyHeader(w.Header(), resp.Header) - p, err := ioutil.ReadAll(resp.Body) + p, err := io.ReadAll(resp.Body) if err != nil { log.Printf("prometheus read body error: %s", err) return @@ -328,7 +331,7 @@ func (hb *HttpBackend) QueryFlux(req *http.Request, w http.ResponseWriter) (err CopyHeader(w.Header(), resp.Header) - p, err := ioutil.ReadAll(resp.Body) + p, err := io.ReadAll(resp.Body) if err != nil { log.Printf("flux read body error: %s", err) return @@ -382,7 +385,7 @@ func (hb *HttpBackend) Query(req *http.Request, w http.ResponseWriter, decompres respBody = b } - qr.Body, qr.Err = ioutil.ReadAll(respBody) + qr.Body, qr.Err = io.ReadAll(respBody) if qr.Err != nil { log.Printf("read body error: %s, the query is %s", qr.Err, q) return diff --git a/backend/influxql.go b/backend/influxql.go index f68d700..4d6bc38 100644 --- a/backend/influxql.go +++ b/backend/influxql.go @@ -137,7 +137,7 @@ func ScanToken(data []byte, atEOF bool) (advance int, token []byte, err error) { } start := 0 - for ; start < len(data) && data[start] == ' '; start++ { + for ; start < len(data) && data[start] == ' '; start++ { //revive:disable-line:empty-block } if start == len(data) { return 0, nil, nil @@ -309,7 +309,7 @@ func getDatabase(tokens []string, keyword string) (db string) { return } -func getRetentionPolicy(tokens []string, keyword string) (rp string) { +func getRetentionPolicy(tokens []string, _ string) (rp string) { if len(tokens) == 0 { return } @@ -335,7 +335,7 @@ func getRetentionPolicy(tokens []string, keyword string) (rp string) { return } -func getMeasurement(tokens []string, keyword string) (mm string) { +func getMeasurement(tokens []string, _ string) (mm string) { if len(tokens) == 0 { return } diff --git a/backend/lineproto.go b/backend/lineproto.go index 9dcfbfc..888ff6c 100644 --- a/backend/lineproto.go +++ b/backend/lineproto.go @@ -62,14 +62,12 @@ func AppendNano(line []byte, precision string) []byte { return append(line, '0', '0', '0', '0', '0', '0') } else if precision == "s" { return append(line, '0', '0', '0', '0', '0', '0', '0', '0', '0') - } else { - mul := models.GetPrecisionMultiplier(precision) - nano := BytesToInt64(line[pos+1:]) * mul - return append(line[:pos+1], Int64ToBytes(nano)...) } - } else { - return append(line, []byte(" "+strconv.FormatInt(time.Now().UnixNano(), 10))...) + mul := models.GetPrecisionMultiplier(precision) + nano := BytesToInt64(line[pos+1:]) * mul + return append(line[:pos+1], Int64ToBytes(nano)...) } + return append(line, []byte(" "+strconv.FormatInt(time.Now().UnixNano(), 10))...) } func Int64ToBytes(n int64) []byte { @@ -77,7 +75,7 @@ func Int64ToBytes(n int64) []byte { } func BytesToInt64(buf []byte) int64 { - var res int64 = 0 + var res int64 var length = len(buf) for i := 0; i < length; i++ { res = res*10 + int64(buf[i]-'0') diff --git a/backend/proxy.go b/backend/proxy.go index d91a1bd..61b879a 100644 --- a/backend/proxy.go +++ b/backend/proxy.go @@ -38,7 +38,7 @@ func NewProxy(cfg *ProxyConfig) (ip *Proxy) { for _, db := range cfg.DBList { ip.dbSet.Add(db) } - rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) return } diff --git a/service/http.go b/service/http.go index 097b47a..e287ad1 100644 --- a/service/http.go +++ b/service/http.go @@ -10,7 +10,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "log" "mime" "net/http" @@ -50,7 +50,7 @@ func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { mux.ServeMux.ServeHTTP(w, r) } -type HttpService struct { // nolint:golint +type HttpService struct { // nolint:revive ip *backend.Proxy tx *transfer.Transfer username string @@ -61,7 +61,7 @@ type HttpService struct { // nolint:golint pprofEnabled bool } -func NewHttpService(cfg *backend.ProxyConfig) (hs *HttpService) { // nolint:golint +func NewHttpService(cfg *backend.ProxyConfig) (hs *HttpService) { // nolint:revive ip := backend.NewProxy(cfg) hs = &HttpService{ ip: ip, @@ -100,7 +100,7 @@ func (hs *HttpService) Register(mux *ServeMux) { } } -func (hs *HttpService) HandlerPing(w http.ResponseWriter, req *http.Request) { +func (hs *HttpService) HandlerPing(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) } @@ -137,7 +137,7 @@ func (hs *HttpService) HandlerQueryV2(w http.ResponseWriter, req *http.Request) hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return } - rbody, err := ioutil.ReadAll(req.Body) + rbody, err := io.ReadAll(req.Body) if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -164,7 +164,7 @@ func (hs *HttpService) HandlerQueryV2(w http.ResponseWriter, req *http.Request) return } - req.Body = ioutil.NopCloser(bytes.NewBuffer(rbody)) + req.Body = io.NopCloser(bytes.NewBuffer(rbody)) err = hs.ip.QueryFlux(w, req, qr) if err != nil { log.Printf("flux query error: %s, query: %s, spec: %s, client: %s", err, qr.Query, qr.Spec, req.RemoteAddr) @@ -245,7 +245,7 @@ func (hs *HttpService) handlerWrite(db, rp, precision string, w http.ResponseWri defer b.Close() body = b } - p, err := ioutil.ReadAll(body) + p, err := io.ReadAll(body) if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -328,7 +328,7 @@ func (hs *HttpService) HandlerRebalance(w http.ResponseWriter, req *http.Request return } - circleId, err := hs.formCircleId(req, "circle_id") // nolint:golint + circleId, err := hs.formCircleId(req, "circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -382,12 +382,12 @@ func (hs *HttpService) HandlerRecovery(w http.ResponseWriter, req *http.Request) return } - fromCircleId, err := hs.formCircleId(req, "from_circle_id") // nolint:golint + fromCircleId, err := hs.formCircleId(req, "from_circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return } - toCircleId, err := hs.formCircleId(req, "to_circle_id") // nolint:golint + toCircleId, err := hs.formCircleId(req, "to_circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -456,7 +456,7 @@ func (hs *HttpService) HandlerCleanup(w http.ResponseWriter, req *http.Request) return } - circleId, err := hs.formCircleId(req, "circle_id") // nolint:golint + circleId, err := hs.formCircleId(req, "circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -510,7 +510,7 @@ func (hs *HttpService) HandlerTransferState(w http.ResponseWriter, req *http.Req state["resyncing"] = resyncing } if req.FormValue("circle_id") != "" || req.FormValue("transferring") != "" { - circleId, err := hs.formCircleId(req, "circle_id") // nolint:golint + circleId, err := hs.formCircleId(req, "circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -543,7 +543,7 @@ func (hs *HttpService) HandlerTransferStats(w http.ResponseWriter, req *http.Req return } - circleId, err := hs.formCircleId(req, "circle_id") // nolint:golint + circleId, err := hs.formCircleId(req, "circle_id") // nolint:revive if err != nil { hs.WriteError(w, req, http.StatusBadRequest, err.Error()) return @@ -568,7 +568,7 @@ func (hs *HttpService) HandlerPromRead(w http.ResponseWriter, req *http.Request) return } - compressed, err := ioutil.ReadAll(req.Body) + compressed, err := io.ReadAll(req.Body) if err != nil { hs.WriteError(w, req, http.StatusInternalServerError, err.Error()) return @@ -604,7 +604,7 @@ func (hs *HttpService) HandlerPromRead(w http.ResponseWriter, req *http.Request) hs.WriteError(w, req, http.StatusBadRequest, err.Error()) } - req.Body = ioutil.NopCloser(bytes.NewBuffer(compressed)) + req.Body = io.NopCloser(bytes.NewBuffer(compressed)) err = hs.ip.ReadProm(w, req, db, metric) if err != nil { log.Printf("prometheus read error: %s, query: %s %s %v, client: %s", err, req.Method, db, q, req.RemoteAddr) @@ -836,8 +836,8 @@ func (hs *HttpService) formTick(req *http.Request) (int64, error) { return tick, nil } -func (hs *HttpService) formCircleId(req *http.Request, key string) (int, error) { // nolint:golint - circleId, err := strconv.Atoi(req.FormValue(key)) // nolint:golint +func (hs *HttpService) formCircleId(req *http.Request, key string) (int, error) { // nolint:revive + circleId, err := strconv.Atoi(req.FormValue(key)) // nolint:revive if err != nil || circleId < 0 || circleId >= len(hs.ip.Circles) { return circleId, fmt.Errorf("invalid %s", key) } diff --git a/transfer/transfer.go b/transfer/transfer.go index a7d82f2..9f9af96 100644 --- a/transfer/transfer.go +++ b/transfer/transfer.go @@ -397,7 +397,7 @@ func (tx *Transfer) runTransfer(cs *CircleState, be *backend.Backend, dbs []stri } } -func (tx *Transfer) Rebalance(circleId int, backends []*backend.Backend, dbs []string) { // nolint:golint +func (tx *Transfer) Rebalance(circleId int, backends []*backend.Backend, dbs []string) { // nolint:revive tx.setLogOutput("rebalance.log") dbs, err := tx.createDatabases(dbs) if err != nil || len(dbs) == 0 { @@ -424,7 +424,7 @@ func (tx *Transfer) Rebalance(circleId int, backends []*backend.Backend, dbs []s tlog.Printf("rebalance done: circle %d", circleId) } -func (tx *Transfer) runRebalance(cs *CircleState, be *backend.Backend, db string, meas string, args []interface{}) (require bool) { +func (tx *Transfer) runRebalance(cs *CircleState, be *backend.Backend, db string, meas string, _ []interface{}) (require bool) { key := backend.GetKey(db, meas) dst := cs.GetBackend(key) require = dst.Url != be.Url @@ -434,7 +434,7 @@ func (tx *Transfer) runRebalance(cs *CircleState, be *backend.Backend, db string return } -func (tx *Transfer) Recovery(fromCircleId, toCircleId int, backendUrls []string, dbs []string) { // nolint:golint +func (tx *Transfer) Recovery(fromCircleId, toCircleId int, backendUrls []string, dbs []string) { // nolint:revive tx.setLogOutput("recovery.log") dbs, err := tx.createDatabases(dbs) if err != nil || len(dbs) == 0 { @@ -453,7 +453,7 @@ func (tx *Transfer) Recovery(fromCircleId, toCircleId int, backendUrls []string, tx.broadcastTransferring(tcs, true) defer tx.broadcastTransferring(tcs, false) - backendUrlSet := util.NewSet() // nolint:golint + backendUrlSet := util.NewSet() // nolint:revive if len(backendUrls) != 0 { for _, u := range backendUrls { backendUrlSet.Add(u) @@ -474,7 +474,7 @@ func (tx *Transfer) Recovery(fromCircleId, toCircleId int, backendUrls []string, func (tx *Transfer) runRecovery(fcs *CircleState, be *backend.Backend, db string, meas string, args []interface{}) (require bool) { tcs := args[0].(*CircleState) - backendUrlSet := args[1].(util.Set) // nolint:golint + backendUrlSet := args[1].(util.Set) // nolint:revive key := backend.GetKey(db, meas) dst := tcs.GetBackend(key) require = backendUrlSet[dst.Url] @@ -531,7 +531,7 @@ func (tx *Transfer) runResync(cs *CircleState, be *backend.Backend, db string, m return } -func (tx *Transfer) Cleanup(circleId int) { // nolint:golint +func (tx *Transfer) Cleanup(circleId int) { // nolint:revive tx.setLogOutput("cleanup.log") var err error tx.pool, err = ants.NewPool(tx.Worker) @@ -558,7 +558,7 @@ func (tx *Transfer) Cleanup(circleId int) { // nolint:golint tlog.Printf("cleanup done: circle %d", circleId) } -func (tx *Transfer) runCleanup(cs *CircleState, be *backend.Backend, db string, meas string, args []interface{}) (require bool) { +func (tx *Transfer) runCleanup(cs *CircleState, be *backend.Backend, db string, meas string, _ []interface{}) (require bool) { key := backend.GetKey(db, meas) dst := cs.GetBackend(key) require = dst.Url != be.Url