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

This commit is contained in:
Igor Chubin 2022-12-11 14:50:06 +01:00
parent fca62e63c3
commit 173b501a2d
4 changed files with 52 additions and 37 deletions

View file

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

View file

@ -75,12 +75,19 @@ func (rp *RequestProcessor) prefetchPeakRequests(peakRequestMap *sync.Map) {
log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen)
sleepBetweenRequests := time.Duration(rp.config.Uplink.PrefetchInterval*1000/peakRequestLen) * time.Millisecond
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) {
err := rp.prefetchRequest(&r)
if err != nil {
log.Println("prefetch request:", err)
}
}(value.(http.Request))
}(req)
peakRequestMap.Delete(key)
time.Sleep(sleepBetweenRequests)

View file

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

8
srv.go
View file

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