Fix linter findings for: forbidigo, funlen, forcetypeassert, stylecheck

chubin/logging
Igor Chubin 2 years ago
parent fca62e63c3
commit 173b501a2d

@ -14,21 +14,16 @@ linters:
- gofmt - gofmt
# to be fixed: # to be fixed:
- ireturn
- gosec - gosec
- noctx - noctx
- funlen - funlen
- nestif - nestif
- forbidigo
- funlen
- interfacer - interfacer
- revive - revive
- cyclop - cyclop
- goerr113 - goerr113
- forcetypeassert
- gocognit - gocognit
- golint
- stylecheck
- ireturn
# deprecated: # deprecated:
- scopelint - scopelint
@ -38,3 +33,4 @@ linters:
- ifshort - ifshort
- nosnakecase - nosnakecase
- structcheck - structcheck
- golint

@ -75,12 +75,19 @@ func (rp *RequestProcessor) prefetchPeakRequests(peakRequestMap *sync.Map) {
log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen) log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen)
sleepBetweenRequests := time.Duration(rp.config.Uplink.PrefetchInterval*1000/peakRequestLen) * time.Millisecond sleepBetweenRequests := time.Duration(rp.config.Uplink.PrefetchInterval*1000/peakRequestLen) * time.Millisecond
peakRequestMap.Range(func(key interface{}, value interface{}) bool { peakRequestMap.Range(func(key interface{}, value interface{}) bool {
req, ok := value.(http.Request)
if !ok {
log.Println("missing value for:", key)
return true
}
go func(r http.Request) { go func(r http.Request) {
err := rp.prefetchRequest(&r) err := rp.prefetchRequest(&r)
if err != nil { if err != nil {
log.Println("prefetch request:", err) log.Println("prefetch request:", err)
} }
}(value.(http.Request)) }(req)
peakRequestMap.Delete(key) peakRequestMap.Delete(key)
time.Sleep(sleepBetweenRequests) time.Sleep(sleepBetweenRequests)

@ -107,8 +107,9 @@ func (rp *RequestProcessor) Start() error {
func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader, error) { func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader, error) {
var ( var (
response *responseWithHeader response *responseWithHeader
err error cacheEntry responseWithHeader
err error
) )
ip := util.ReadUserIP(r) ip := util.ReadUserIP(r)
@ -143,9 +144,11 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
rp.savePeakRequest(cacheDigest, r) rp.savePeakRequest(cacheDigest, r)
cacheBody, ok := rp.lruCache.Get(cacheDigest) cacheBody, ok := rp.lruCache.Get(cacheDigest)
if ok {
cacheEntry, ok = cacheBody.(responseWithHeader)
}
if ok { if ok {
rp.stats.Inc("cache1") rp.stats.Inc("cache1")
cacheEntry := cacheBody.(responseWithHeader)
// if after all attempts we still have no answer, // if after all attempts we still have no answer,
// we try to make the query on our own // we try to make the query on our own
@ -156,7 +159,9 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
time.Sleep(30 * time.Millisecond) time.Sleep(30 * time.Millisecond)
cacheBody, ok = rp.lruCache.Get(cacheDigest) cacheBody, ok = rp.lruCache.Get(cacheDigest)
if ok && cacheBody != nil { if ok && cacheBody != nil {
cacheEntry = cacheBody.(responseWithHeader) if v, ok := cacheBody.(responseWithHeader); ok {
cacheEntry = v
}
} }
} }
if cacheEntry.InProgress { if cacheEntry.InProgress {
@ -168,34 +173,37 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
} }
} }
if !foundInCache { if foundInCache {
// Handling query. return response, nil
format := r.URL.Query().Get("format") }
if len(format) != 0 {
rp.stats.Inc("format")
if format == "j1" {
rp.stats.Inc("format=j1")
}
}
// How many IP addresses are known. // Response was not found in cache.
_, err = rp.geoIPCache.Read(ip) // Starting real handling.
if err == nil { format := r.URL.Query().Get("format")
rp.stats.Inc("geoip") if len(format) != 0 {
rp.stats.Inc("format")
if format == "j1" {
rp.stats.Inc("format=j1")
} }
}
rp.lruCache.Add(cacheDigest, responseWithHeader{InProgress: true}) // How many IP addresses are known.
_, err = rp.geoIPCache.Read(ip)
if err == nil {
rp.stats.Inc("geoip")
}
response, err = get(r, rp.upstreamTransport) rp.lruCache.Add(cacheDigest, responseWithHeader{InProgress: true})
if err != nil {
return nil, err response, err = get(r, rp.upstreamTransport)
} if err != nil {
if response.StatusCode == 200 || response.StatusCode == 304 || response.StatusCode == 404 { return nil, err
rp.lruCache.Add(cacheDigest, *response) }
} else { if response.StatusCode == 200 || response.StatusCode == 304 || response.StatusCode == 404 {
log.Printf("REMOVE: %d response for %s from cache\n", response.StatusCode, cacheDigest) rp.lruCache.Add(cacheDigest, *response)
rp.lruCache.Remove(cacheDigest) } else {
} log.Printf("REMOVE: %d response for %s from cache\n", response.StatusCode, cacheDigest)
rp.lruCache.Remove(cacheDigest)
} }
return response, nil return response, nil

@ -76,7 +76,7 @@ func serveHTTPS(mux *http.ServeMux, port int, certFile, keyFile string, logFile
func serve(conf *config.Config) error { func serve(conf *config.Config) error {
var ( var (
// mux is main HTTP/HTTP requests multiplexer. // mux is main HTTP/HTTP requests multiplexer.
mux *http.ServeMux = http.NewServeMux() mux = http.NewServeMux()
// logger is optimized requests logger. // logger is optimized requests logger.
logger *logging.RequestLogger logger *logging.RequestLogger
@ -182,10 +182,13 @@ func main() {
} }
if cli.ConfigDump { if cli.ConfigDump {
//nolint:forbidigo
fmt.Print(string(conf.Dump())) fmt.Print(string(conf.Dump()))
return
} }
if cli.ConfigCheck || cli.ConfigDump { if cli.ConfigCheck {
return return
} }
@ -216,6 +219,7 @@ func main() {
loc, err := sr.Search(cli.GeoResolve) loc, err := sr.Search(cli.GeoResolve)
ctx.FatalIfErrorf(err) ctx.FatalIfErrorf(err)
if loc != nil { if loc != nil {
//nolint:forbidigo
fmt.Println(*loc) fmt.Println(*loc)
} }
} }

Loading…
Cancel
Save