From 17070d92c1d6e184cfd3065ef87fc5f43403b93c Mon Sep 17 00:00:00 2001 From: John Arbash Meinel Date: Wed, 26 Feb 2014 16:25:40 +0400 Subject: [PATCH 1/3] go fmt, and some formatting tweaks when using fatal it wasn't appending a newline, so messages were confusing this fixes that. also, add millisecond-level timing for how long it takes us to respond to requests. The *data* is available in UnixNano() but it requires doing the subtraction somehow, and also misses some of the overhead as the 'received challenge' only triggers after we've processed some of the request. --- pollen.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pollen.go b/pollen.go index 4f0c04c..8398a92 100644 --- a/pollen.go +++ b/pollen.go @@ -38,11 +38,12 @@ var ( size = flag.Int("bytes", 64, "The size in bytes to transmit and receive each time") cert = flag.String("cert", "/etc/pollen/cert.pem", "The full path to cert.pem") key = flag.String("key", "/etc/pollen/key.pem", "The full path to key.pem") - log *syslog.Writer - dev *os.File + log *syslog.Writer + dev *os.File ) func handler(w http.ResponseWriter, r *http.Request) { + startTime := time.Now() challenge := r.FormValue("challenge") if challenge == "" { http.Error(w, "Please use the pollinate client. 'sudo apt-get install pollinate' or download from: https://bazaar.launchpad.net/~pollinate/pollinate/trunk/view/head:/pollinate", http.StatusBadRequest) @@ -70,14 +71,14 @@ func handler(w http.ResponseWriter, r *http.Request) { /* The checksum of the bytes from /dev/urandom is simply for print-ability, when debugging */ seed := checksum.Sum(nil) fmt.Fprintf(w, "%x\n%x\n", challengeResponse, seed) - log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v]", r.RemoteAddr, r.UserAgent(), time.Now().UnixNano())) + log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v] in %.3fs", r.RemoteAddr, r.UserAgent(), time.Now().UnixNano(), time.Since(startTime))) } func init() { var err error log, err = syslog.New(syslog.LOG_ERR, "pollen") if err != nil { - fatalf("Cannot open syslog:", err) + fatalf("Cannot open syslog: %s\n", err) } dev, err = os.OpenFile(*device, os.O_RDWR, 0) if err != nil { @@ -102,6 +103,7 @@ func main() { func fatal(args ...interface{}) { log.Crit(fmt.Sprint(args...)) + args = append(args, "\n") fmt.Fprint(os.Stderr, args...) os.Exit(1) } From 055ed371fbaa972c955f9e387f5b651aa638230a Mon Sep 17 00:00:00 2001 From: John Arbash Meinel Date: Wed, 26 Feb 2014 16:30:47 +0400 Subject: [PATCH 2/3] actually do the right formatting --- pollen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pollen.go b/pollen.go index 8398a92..312bc56 100644 --- a/pollen.go +++ b/pollen.go @@ -71,7 +71,7 @@ func handler(w http.ResponseWriter, r *http.Request) { /* The checksum of the bytes from /dev/urandom is simply for print-ability, when debugging */ seed := checksum.Sum(nil) fmt.Fprintf(w, "%x\n%x\n", challengeResponse, seed) - log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v] in %.3fs", r.RemoteAddr, r.UserAgent(), time.Now().UnixNano(), time.Since(startTime))) + log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v] in %.3fs", r.RemoteAddr, r.UserAgent(), time.Now().UnixNano(), time.Since(startTime).Seconds())) } func init() { From cd0117f280634ed8e810e75dfdced8bc15b4a5e3 Mon Sep 17 00:00:00 2001 From: John Arbash Meinel Date: Wed, 26 Feb 2014 17:11:50 +0400 Subject: [PATCH 3/3] use microsecond timing (ms was always 0) also, add a log when the process starts --- pollen.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pollen.go b/pollen.go index 312bc56..a3c76aa 100644 --- a/pollen.go +++ b/pollen.go @@ -71,7 +71,8 @@ func handler(w http.ResponseWriter, r *http.Request) { /* The checksum of the bytes from /dev/urandom is simply for print-ability, when debugging */ seed := checksum.Sum(nil) fmt.Fprintf(w, "%x\n%x\n", challengeResponse, seed) - log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v] in %.3fs", r.RemoteAddr, r.UserAgent(), time.Now().UnixNano(), time.Since(startTime).Seconds())) + log.Info(fmt.Sprintf("Server sent response to [%s, %s] at [%v] in %.6fs", + r.RemoteAddr, r.UserAgent(), time.Now().UnixNano(), time.Since(startTime).Seconds())) } func init() { @@ -88,6 +89,7 @@ func init() { func main() { flag.Parse() + log.Info(fmt.Sprintf("pollen starting at [%v]", time.Now().UnixNano())) defer dev.Close() if *httpPort == "" && *httpsPort == "" { fatal("Nothing to do if http and https are both disabled")