diff --git a/backend/backend.go b/backend/backend.go index 7669ba1..a9e15a4 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -318,7 +318,7 @@ func (ib *Backend) Close() { func (ib *Backend) GetHealth() interface{} { health := struct { Name string `json:"name"` - Url string `json:"url"` // nolint:golint + Url string `json:"url"` //nolint:all Active bool `json:"active"` Backlog bool `json:"backlog"` Rewriting bool `json:"rewriting"` diff --git a/backend/circle.go b/backend/circle.go index aae0da8..b359b84 100644 --- a/backend/circle.go +++ b/backend/circle.go @@ -12,7 +12,7 @@ import ( ) type Circle struct { - CircleId int // nolint:golint + CircleId int //nolint:all 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:all ic = &Circle{ CircleId: circleId, Name: cfg.Name, @@ -64,7 +64,7 @@ func (ic *Circle) GetHealth() interface{} { } wg.Wait() circle := struct { - Id int `json:"id"` // nolint:golint + Id int `json:"id"` //nolint:all Name string `json:"name"` Active bool `json:"active"` WriteOnly bool `json:"write_only"` diff --git a/backend/config.go b/backend/config.go index 841ecaf..fe0e4f4 100644 --- a/backend/config.go +++ b/backend/config.go @@ -25,14 +25,14 @@ var ( ErrEmptyBackends = errors.New("backends cannot be empty") ErrEmptyBackendName = errors.New("backend name cannot be empty") ErrDuplicatedBackendName = errors.New("backend name duplicated") - ErrEmptyBackendUrl = errors.New("backend url cannot be empty") // nolint:golint + ErrEmptyBackendUrl = errors.New("backend url cannot be empty") //nolint:all ErrEmptyBackendToken = errors.New("backend token cannot be empty") ErrInvalidDBRPMapping = errors.New("invalid dbrp mapping") ) -type BackendConfig struct { // nolint:golint +type BackendConfig struct { //nolint:all Name string `mapstructure:"name"` - Url string `mapstructure:"url"` // nolint:golint + Url string `mapstructure:"url"` //nolint:all Token string `mapstructure:"token"` WriteOnly bool `mapstructure:"write_only"` } diff --git a/backend/http.go b/backend/http.go index a3efb64..e653939 100644 --- a/backend/http.go +++ b/backend/http.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -46,11 +45,11 @@ type QueryResult struct { Err error } -type HttpBackend struct { // nolint:golint +type HttpBackend struct { //nolint:all client *http.Client transport *http.Transport Name string - Url string // nolint:golint + Url string //nolint:all token string interval int running atomic.Value @@ -60,7 +59,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:all hb = NewSimpleHttpBackend(cfg) hb.client = NewClient(strings.HasPrefix(cfg.Url, "https"), pxcfg.WriteTimeout) hb.interval = pxcfg.CheckInterval @@ -68,7 +67,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:all hb = &HttpBackend{ transport: NewTransport(strings.HasPrefix(cfg.Url, "https")), Name: cfg.Name, @@ -104,7 +103,7 @@ func NewTransport(tlsSkip bool) *http.Transport { 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 } @@ -194,6 +193,10 @@ func (hb *HttpBackend) WriteStream(org, bucket string, stream io.Reader, compres q.Set("org", org) q.Set("bucket", bucket) req, err := http.NewRequest("POST", hb.Url+"/api/v2/write?"+q.Encode(), stream) + if err != nil { + log.Print("new request error: ", err) + return nil + } hb.SetAuthorization(req) if compressed { req.Header.Add("Content-Encoding", "gzip") @@ -212,7 +215,7 @@ func (hb *HttpBackend) WriteStream(org, bucket string, stream io.Reader, compres } 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 @@ -264,7 +267,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 @@ -316,7 +319,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 e431825..db1f229 100644 --- a/backend/influxql.go +++ b/backend/influxql.go @@ -128,7 +128,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 @@ -300,7 +300,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 } @@ -326,7 +326,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 0695524..01ba92d 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 0747ac6..d3ca258 100644 --- a/backend/proxy.go +++ b/backend/proxy.go @@ -37,7 +37,7 @@ func NewProxy(cfg *ProxyConfig) (ip *Proxy) { for key, value := range cfg.DBRP.Mapping { ip.dbrps[key] = strings.Split(value, cfg.DBRP.Separator) } - rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) return } diff --git a/service/http.go b/service/http.go index e216ed4..9af8f9c 100644 --- a/service/http.go +++ b/service/http.go @@ -9,7 +9,7 @@ import ( "compress/gzip" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "mime" "net/http" @@ -34,7 +34,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:all ip *backend.Proxy token string writeTracing bool @@ -42,7 +42,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:all ip := backend.NewProxy(cfg) hs = &HttpService{ ip: ip, @@ -68,7 +68,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) } @@ -112,7 +112,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 @@ -139,7 +139,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, org, qr) if err != nil { log.Printf("flux query error: %s, query: %s, spec: %s, org: %s, client: %s", err, qr.Query, qr.Spec, org, req.RemoteAddr) @@ -227,7 +227,7 @@ func (hs *HttpService) handlerWrite(org, bucket, precision string, w http.Respon 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