Skip to content

Commit

Permalink
use revive instead of golint and fix lint warning
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshiwen committed Aug 5, 2024
1 parent 99a9d80 commit 8791a8d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 56 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ run:
allow-parallel-runners: true
linters:
enable:
- golint
- revive
disable:
- errcheck
- typecheck
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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"`
Expand Down
6 changes: 3 additions & 3 deletions backend/circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
)

type Circle struct {
CircleId int // nolint:golint
CircleId int // nolint:revive
Name string
Backends []*Backend
router *consistent.Consistent
routerCache sync.Map
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,
Expand Down Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
23 changes: 13 additions & 10 deletions backend/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -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
Expand All @@ -59,15 +58,15 @@ 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
go hb.CheckActive()
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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions backend/influxql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
12 changes: 5 additions & 7 deletions backend/lineproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,20 @@ 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 {
return []byte(strconv.FormatInt(n, 10))
}

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')
Expand Down
2 changes: 1 addition & 1 deletion backend/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
34 changes: 17 additions & 17 deletions service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"mime"
"net/http"
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit 8791a8d

Please sign in to comment.