Replace time.Now().Sub with time.Since

This commit is contained in:
shubhendra 2021-02-25 15:58:45 +05:30
parent 512133e14b
commit 644291c028
No known key found for this signature in database
GPG Key ID: 9AFEF5C98D542137
5 changed files with 6 additions and 18 deletions

View File

@ -1,12 +0,0 @@
version = 1
test_patterns = ["**/*_test.go"]
exclude_patterns = ["scripts/**"]
[[analyzers]]
name = "go"
enabled = true
[analyzers.meta]
import_paths = ["github.com/beego/beego"]

View File

@ -42,7 +42,7 @@ func (mi *MemoryItem) isExpire() bool {
if mi.lifespan == 0 { if mi.lifespan == 0 {
return false return false
} }
return time.Now().Sub(mi.createdTime) > mi.lifespan return time.Since(mi.createdTime) > mi.lifespan
} }
// MemoryCache is a memory cache adapter. // MemoryCache is a memory cache adapter.
@ -66,7 +66,7 @@ func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
bc.RLock() bc.RLock()
defer bc.RUnlock() defer bc.RUnlock()
if itm, ok := if itm, ok :=
bc.items[key]; ok { bc.items[key]; ok {
if itm.isExpire() { if itm.isExpire() {
return nil, ErrKeyExpired return nil, ErrKeyExpired
} }

View File

@ -85,7 +85,7 @@ func (builder *FilterChainBuilder) report(ctx context.Context, inv *orm.Invocati
} }
func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) { func (builder *FilterChainBuilder) reportTxn(ctx context.Context, inv *orm.Invocation) {
dur := time.Now().Sub(inv.TxStartTime) / time.Millisecond dur := time.Since(inv.TxStartTime) / time.Millisecond
summaryVec.WithLabelValues(inv.Method, inv.TxName, summaryVec.WithLabelValues(inv.Method, inv.TxName,
strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur)) strconv.FormatBool(inv.InsideTx), inv.TxName).Observe(float64(dur))
} }

View File

@ -41,7 +41,7 @@ func NewLog(out io.Writer) *Log {
func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error, args ...interface{}) { func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error, args ...interface{}) {
var logMap = make(map[string]interface{}) var logMap = make(map[string]interface{})
sub := time.Now().Sub(t) / 1e5 sub := time.Since(t) / 1e5
elsp := float64(int(sub)) / 10.0 elsp := float64(int(sub)) / 10.0
logMap["cost_time"] = elsp logMap["cost_time"] = elsp
flag := " OK" flag := " OK"

View File

@ -108,7 +108,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
if gcstats.NumGC > 0 { if gcstats.NumGC > 0 {
lastPause := gcstats.Pause[0] lastPause := gcstats.Pause[0]
elapsed := time.Now().Sub(startTime) elapsed := time.Since(startTime)
overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100 overhead := float64(gcstats.PauseTotal) / float64(elapsed) * 100
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds() allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
@ -125,7 +125,7 @@ func printGC(memStats *runtime.MemStats, gcstats *debug.GCStats, w io.Writer) {
utils.ToShortTimeFormat(gcstats.PauseQuantiles[99])) utils.ToShortTimeFormat(gcstats.PauseQuantiles[99]))
} else { } else {
// while GC has disabled // while GC has disabled
elapsed := time.Now().Sub(startTime) elapsed := time.Since(startTime)
allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds() allocatedRate := float64(memStats.TotalAlloc) / elapsed.Seconds()
fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n", fmt.Fprintf(w, "Alloc:%s Sys:%s Alloc(Rate):%s/s\n",