Skip to content

Commit

Permalink
update linter checks with newer linters
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed May 26, 2022
1 parent b936d8e commit dce1854
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ jobs:
env:
GO111MODULE: on
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2
$(go env GOPATH)/bin/golangci-lint run --timeout=5m --config ./.golangci.yml
go test -v -race ./...
12 changes: 11 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ linters:
- goimports
- misspell
- govet
- golint
- revive
- ineffassign
- gosimple
- deadcode
- structcheck
- gomodguard
- gofmt
- unused
- structcheck
- unconvert
- varcheck
- gocritic
- gofumpt
- tenv
- durationcheck

issues:
exclude-use-default: false
Expand Down
2 changes: 1 addition & 1 deletion cache-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func NewRecorder() *ResponseRecorder {
pw: pw,
pr: pr,
}

}

// Header needed for implementing "net/http".ResponseWriter
Expand Down Expand Up @@ -171,6 +170,7 @@ func (t *multiWriter) Write(p []byte) (n int, err error) {
}
return len(p), nil
}

func cacheMultiWriter(w1 io.Writer, w2 *io.PipeWriter) io.Writer {
return &multiWriter{backendWriter: w1, cacheWriter: w2}
}
28 changes: 18 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ type S3CacheClient struct {

func (c *S3CacheClient) setOffline() {
atomic.StoreInt32(&c.up, 0)

}

func (c *S3CacheClient) isOnline() bool {
return atomic.LoadInt32(&c.up) == 1
}

func (c *S3CacheClient) isCacheable(method string) bool {
for _, m := range c.methods {
if method == m {
Expand All @@ -102,7 +103,7 @@ func (c *S3CacheClient) healthCheck() {
logMsg(logMessage{Endpoint: c.endpoint, Error: err})
}
c.setOffline()
time.Sleep(time.Duration(c.healthCheckDuration) * time.Second)
time.Sleep(c.healthCheckDuration)
continue
}

Expand All @@ -121,7 +122,7 @@ func (c *S3CacheClient) healthCheck() {
}

}
time.Sleep(time.Duration(c.healthCheckDuration) * time.Second)
time.Sleep(c.healthCheckDuration)
}
}

Expand Down Expand Up @@ -213,18 +214,21 @@ func parseCacheControlHeaders(header http.Header) *cacheControl {
}
return &c
}

func (c *cacheControl) revalidate() bool {
if c == nil {
return true
}
return c.noCache || c.mustRevalidate
}

func (c *cacheControl) neverCache() bool {
if c == nil {
return false
}
return c.private || c.noStore
}

func (c *cacheControl) fresh(modTime time.Time) bool {
if c == nil {
return false
Expand Down Expand Up @@ -328,7 +332,7 @@ func getCacheResponseHeaders(oi minio.ObjectInfo) (ch cacheHeader) {
return ch
}

//Expires returns expires header from cached response
// Expires returns expires header from cached response
func (c cacheHeader) Expires() time.Time {
if v := c.Header.Get("Expires"); v != "" {
if t, e := time.Parse(http.TimeFormat, v); e == nil {
Expand All @@ -338,12 +342,12 @@ func (c cacheHeader) Expires() time.Time {
return time.Time{}
}

//ETag returns ETag from cached response
// ETag returns ETag from cached response
func (c cacheHeader) ETag() string {
return c.Header.Get("Etag")
}

//LastModified returns last modified header from cached response
// LastModified returns last modified header from cached response
func (c cacheHeader) LastModified() time.Time {
if v := c.Header.Get("Last-Modified"); v != "" {
if t, e := time.Parse(http.TimeFormat, v); e == nil {
Expand Down Expand Up @@ -505,6 +509,7 @@ func (h *HTTPRangeSpec) String(resourceSize int64) string {
}
return fmt.Sprintf("%d-%d", off, off+length-1)
}

func (c cacheHeader) Range() (rs *HTTPRangeSpec) {
if rangeHeader, ok := c.Header["Range"]; ok {
if len(rangeHeader) != 0 {
Expand All @@ -527,6 +532,7 @@ func getPutMetadata(h http.Header) map[string]string {
}
return metadata
}

func isFresh(cacheCC, reqCC *cacheControl, lastModified time.Time) bool {
if cacheCC == nil && reqCC == nil {
return false
Expand Down Expand Up @@ -753,14 +759,16 @@ func newCacheConfig() *cacheConfig {
console.Fatalln(fmt.Errorf("Unable to parse SIDEKICK_CACHE_HEALTH_DURATION %s should be an integer", durationStr))
}
}
return &cacheConfig{endpoint: cURL,
return &cacheConfig{
endpoint: cURL,
accessKey: accessKey,
secretKey: secretKey,
bucket: bucket,
minSize: minSize,
duration: time.Duration(duration) * time.Second,
}
}

func newCacheClient(ctx *cli.Context, cfg *cacheConfig) *S3CacheClient {
if cfg == nil {
return nil
Expand Down Expand Up @@ -844,14 +852,14 @@ func newCacheClient(ctx *cli.Context, cfg *cacheConfig) *S3CacheClient {
return s3Clnt
}

func sortURLParams(URL *url.URL) {
params := URL.Query()
func sortURLParams(u *url.URL) {
params := u.Query()
for _, param := range params {
sort.Slice(param, func(i, j int) bool {
return param[i] < param[j]
})
}
URL.RawQuery = params.Encode()
u.RawQuery = params.Encode()
}

func generateKey(u *url.URL, host string) string {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ require (
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,8 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
11 changes: 7 additions & 4 deletions http-tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (r *recordRequest) Read(p []byte) (n int, err error) {
}
return n, err
}

func (r *recordRequest) Size() int {
sz := r.bytesRead
for k, v := range r.headers {
Expand Down Expand Up @@ -171,6 +172,7 @@ func (r *recordRequest) Data() []byte {
// ... otherwise we return <BODY> placeholder
return BodyPlaceHolder
}

func httpInternalTrace(req *http.Request, resp *http.Response, reqTime, respTime time.Time, backend *Backend) {
ti := InternalTrace(req, resp, reqTime, respTime)
doTrace(ti, backend)
Expand Down Expand Up @@ -370,7 +372,7 @@ type shortTraceMsg struct {
}

func (s shortTraceMsg) String() string {
var b = &strings.Builder{}
b := &strings.Builder{}
fmt.Fprintf(b, " %5s: ", TraceMsgType)
fmt.Fprintf(b, "%s ", s.Time.Format(timeFormat))
statusStr := console.Colorize("RespStatus", fmt.Sprintf("%d %s", s.StatusCode, s.StatusMsg))
Expand Down Expand Up @@ -418,9 +420,10 @@ func shortTrace(t TraceInfo) shortTraceMsg {
s.Method = t.ReqInfo.Method
return s
}

func (trc TraceInfo) String() string {
var nodeNameStr string
var b = &strings.Builder{}
b := &strings.Builder{}

if trc.NodeName != "" {
nodeNameStr = fmt.Sprintf("%s: %s ", console.Colorize("TraceMsgType", DebugMsgType), trc.NodeName)
Expand All @@ -446,7 +449,7 @@ func (trc TraceInfo) String() string {
fmt.Sprintf("%s: ", k))+console.Colorize("HeaderValue", fmt.Sprintf("%s\n", strings.Join(v, ""))))
}

fmt.Fprintf(b, "%s%s", nodeNameStr, console.Colorize("Body", fmt.Sprintf("%s\n", string(ri.Body))))
fmt.Fprintf(b, "%s%s", nodeNameStr, console.Colorize("Body", fmt.Sprintf("%s\n", ri.Body)))
fmt.Fprintf(b, "%s%s", nodeNameStr, console.Colorize("Response", "[RESPONSE] "))
fmt.Fprintf(b, "[%s] ", rs.Time.Format(timeFormat))
fmt.Fprint(b, console.Colorize("Stat", fmt.Sprintf("[ Duration %2s ↑ %s ↓ %s ]\n", trc.CallStats.Latency.Round(time.Microsecond), humanize.IBytes(uint64(trc.CallStats.Rx)), humanize.IBytes(uint64(trc.CallStats.Tx)))))
Expand All @@ -461,7 +464,7 @@ func (trc TraceInfo) String() string {
fmt.Fprintf(b, "%s%s", nodeNameStr, console.Colorize("RespHeaderKey",
fmt.Sprintf("%s: ", k))+console.Colorize("HeaderValue", fmt.Sprintf("%s\n", strings.Join(v, ""))))
}
fmt.Fprintf(b, "%s%s\n", nodeNameStr, console.Colorize("Body", string(rs.Body)))
fmt.Fprintf(b, "%s%s\n", nodeNameStr, console.Colorize("Body", rs.Body))
fmt.Fprint(b, nodeNameStr)
return b.String()
}
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (b *Backend) updateDowntime(downtime time.Duration) {
b.Stats.Lock()
defer b.Stats.Unlock()
b.Stats.LastDowntime = downtime
b.Stats.CumDowntime = b.Stats.CumDowntime + downtime
b.Stats.CumDowntime += downtime
}

// updateCallStats updates the cumulative stats for each call to backend
Expand Down Expand Up @@ -424,7 +424,7 @@ func getCertPool(cacert string) *x509.CertPool {
if err != nil {
console.Fatalln(fmt.Errorf("unable to load CA certificate: %s", err))
}
ok := pool.AppendCertsFromPEM([]byte(caPEM))
ok := pool.AppendCertsFromPEM(caPEM)
if !ok {
console.Fatalln(fmt.Errorf("unable to load CA certificate: %s is not valid certificate", cacert))
}
Expand All @@ -447,7 +447,7 @@ func getCertKeyPair(cert, key string) []tls.Certificate {
if err != nil {
console.Fatalln(fmt.Errorf("unable to load key: %s", err))
}
keyPair, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
keyPair, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
console.Fatalln(fmt.Errorf("%s", err))
}
Expand Down Expand Up @@ -631,7 +631,7 @@ func configureSite(ctx *cli.Context, siteNum int, siteStrs []string, healthCheck
Transport: transport,
}

stats := BackendStats{MinLatency: time.Duration(24 * time.Hour), MaxLatency: time.Duration(0)}
stats := BackendStats{MinLatency: 24 * time.Hour, MaxLatency: 0}
backend := &Backend{siteNum, endpoint, proxy, &http.Client{
Transport: proxy.Transport,
}, 0, healthCheckPath, healthCheckPort, healthCheckDuration, &stats, newCacheClient(ctx, cacheCfg)}
Expand Down
2 changes: 1 addition & 1 deletion main_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

// Copyright (c) 2021 MinIO, Inc.
Expand Down Expand Up @@ -45,7 +46,6 @@ func setTCPParameters(network, address string, c syscall.RawConn) error {
// Enable TCP quick ACK, John Nagle says
// "Set TCP_QUICKACK. If you find a case where that makes things worse, let me know."
_ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_QUICKACK, 1)

})
return nil
}
1 change: 1 addition & 0 deletions main_others.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !linux
// +build !linux

// Copyright (c) 2021 MinIO, Inc.
Expand Down
1 change: 0 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestGetHealthCheckURL_Valid(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// ----- call -----------------------------------------------------
healthCheckURL, err := getHealthCheckURL(tc.endpoint, tc.healthCheckPath, tc.healthCheckPort)

// ----- verify ---------------------------------------------------
if err != nil {
t.Errorf("Expected no error, got %q", err)
Expand Down
9 changes: 0 additions & 9 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (c *sidekickCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect is called by the Prometheus registry when collecting metrics.
func (c *sidekickCollector) Collect(ch chan<- prometheus.Metric) {

for _, c := range globalConnStats {
if c == nil {
continue
Expand Down Expand Up @@ -94,7 +93,6 @@ func (c *sidekickCollector) Collect(ch chan<- prometheus.Metric) {
c.endpoint,
)
}

}

func metricsHandler() (http.Handler, error) {
Expand All @@ -117,7 +115,6 @@ func metricsHandler() (http.Handler, error) {
ErrorHandling: promhttp.ContinueOnError,
}),
), nil

}

// ConnStats - statistics on backend
Expand All @@ -129,7 +126,6 @@ type ConnStats struct {
totalFailedCalls atomic.Uint64
minLatency atomic.Duration
maxLatency atomic.Duration
status atomic.String
}

// Store current total input bytes
Expand Down Expand Up @@ -157,11 +153,6 @@ func (s *ConnStats) setTotalCallFailures(n int64) {
s.totalFailedCalls.Store(uint64(n))
}

// set backend status
func (s *ConnStats) setStatus(st string) {
s.status.Store(st)
}

// set min latency
func (s *ConnStats) setMinLatency(mn time.Duration) {
s.minLatency.Store(mn)
Expand Down
Loading

0 comments on commit dce1854

Please sign in to comment.