Forráskód Böngészése

Revert goroutine change

Niko Kovacevic 5 éve
szülő
commit
199f4d6fb7
2 módosított fájl, 87 hozzáadás és 33 törlés
  1. 24 33
      pkg/log/log.go
  2. 63 0
      pkg/log/profiler.go

+ 24 - 33
pkg/log/log.go

@@ -18,17 +18,14 @@ func Errorf(format string, a ...interface{}) {
 }
 
 func DedupedErrorf(logTypeLimit int, format string, a ...interface{}) {
-	// Run within a goroutine so that the original call does not block
-	go func(logTypeLimit int, format string, a ...interface{}) {
-		timesLogged := ctr.increment(format)
-
-		if timesLogged < logTypeLimit {
-			Errorf(format, a...)
-		} else if timesLogged == logTypeLimit {
-			Errorf(format, a...)
-			Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
-		}
-	}(logTypeLimit, format, a...)
+	timesLogged := ctr.increment(format)
+
+	if timesLogged < logTypeLimit {
+		Errorf(format, a...)
+	} else if timesLogged == logTypeLimit {
+		Errorf(format, a...)
+		Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
+	}
 }
 
 func Warningf(format string, a ...interface{}) {
@@ -36,17 +33,14 @@ func Warningf(format string, a ...interface{}) {
 }
 
 func DedupedWarningf(logTypeLimit int, format string, a ...interface{}) {
-	// Run within a goroutine so that the original call does not block
-	go func(logTypeLimit int, format string, a ...interface{}) {
-		timesLogged := ctr.increment(format)
-
-		if timesLogged < logTypeLimit {
-			Warningf(format, a...)
-		} else if timesLogged == logTypeLimit {
-			Warningf(format, a...)
-			Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
-		}
-	}(logTypeLimit, format, a...)
+	timesLogged := ctr.increment(format)
+
+	if timesLogged < logTypeLimit {
+		Warningf(format, a...)
+	} else if timesLogged == logTypeLimit {
+		Warningf(format, a...)
+		Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
+	}
 }
 
 func Infof(format string, a ...interface{}) {
@@ -54,17 +48,14 @@ func Infof(format string, a ...interface{}) {
 }
 
 func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
-	// Run within a goroutine so that the original call does not block
-	go func(logTypeLimit int, format string, a ...interface{}) {
-		timesLogged := ctr.increment(format)
-
-		if timesLogged < logTypeLimit {
-			Infof(format, a...)
-		} else if timesLogged == logTypeLimit {
-			Infof(format, a...)
-			Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
-		}
-	}(logTypeLimit, format, a...)
+	timesLogged := ctr.increment(format)
+
+	if timesLogged < logTypeLimit {
+		Infof(format, a...)
+	} else if timesLogged == logTypeLimit {
+		Infof(format, a...)
+		Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
+	}
 }
 
 func Profilef(format string, a ...interface{}) {

+ 63 - 0
pkg/log/profiler.go

@@ -0,0 +1,63 @@
+package log
+
+import "time"
+
+type Profiler struct {
+	profiles map[string]time.Duration
+	starts   map[string]time.Time
+}
+
+func NewProfiler() *Profiler {
+	return &Profiler{
+		profiles: map[string]time.Duration{},
+		starts:   map[string]time.Time{},
+	}
+}
+
+func (p *Profiler) Start(name string) {
+	if p == nil {
+		return
+	}
+	p.starts[name] = time.Now()
+}
+
+func (p *Profiler) Stop(name string) time.Duration {
+	if p == nil {
+		return 0
+	}
+	if start, ok := p.starts[name]; ok {
+		elapsed := time.Since(start)
+		p.profiles[name] += elapsed
+		return elapsed
+	}
+	return 0
+}
+
+func (p *Profiler) Log(name string) {
+	if p == nil {
+		return
+	}
+	Profilef("%s: %s", p.profiles[name], name)
+}
+
+func (p *Profiler) LogAll() {
+	if p == nil {
+		return
+	}
+
+	// Print profiles, largest to smallest. (Inefficienct, but shouldn't matter.)
+	print := map[string]time.Duration{}
+	for name, value := range p.profiles {
+		print[name] = value
+	}
+	for len(print) > 0 {
+		largest := ""
+		for name := range print {
+			if print[name] > print[largest] {
+				largest = name
+			}
+		}
+		Profilef("%s: %s", print[largest], largest)
+		delete(print, largest)
+	}
+}