From b5788192023b83361b03017b6e32e49c4a4cc346 Mon Sep 17 00:00:00 2001 From: Wang Guoliang Date: Wed, 17 Jan 2018 19:03:01 +0800 Subject: [PATCH] Optimize code --- builder/builder.go | 2 +- cache/cache.go | 4 ++-- crd/client.go | 2 +- crd/crd_test.go | 8 ++++---- environments/fetcher/fetcher.go | 4 ++-- executor/executor_test.go | 2 +- executor/fscache/functionServiceCache.go | 4 ++-- executor/poolmgr/gp.go | 6 +++--- fission-bundle/main.go | 6 +++--- router/functionHandler.go | 2 +- storagesvc/client/storagesvc_test.go | 2 +- 11 files changed, 21 insertions(+), 21 deletions(-) diff --git a/builder/builder.go b/builder/builder.go index 16e09832..3abe84d0 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -78,7 +78,7 @@ func (builder *Builder) Handler(w http.ResponseWriter, r *http.Request) { startTime := time.Now() defer func() { - elapsed := time.Now().Sub(startTime) + elapsed := time.Since(startTime) log.Printf("elapsed time in build request = %v", elapsed) }() diff --git a/cache/cache.go b/cache/cache.go index 845c4136..d7c89c3e 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -61,11 +61,11 @@ type ( ) func (c *Cache) IsOld(v *Value) bool { - if (c.ctimeExpiry != time.Duration(0)) && (time.Now().Sub(v.ctime) > c.ctimeExpiry) { + if (c.ctimeExpiry != time.Duration(0)) && (time.Since(v.ctime) > c.ctimeExpiry) { return true } - if (c.atimeExpiry != time.Duration(0)) && (time.Now().Sub(v.atime) > c.atimeExpiry) { + if (c.atimeExpiry != time.Duration(0)) && (time.Since(v.atime) > c.atimeExpiry) { return true } diff --git a/crd/client.go b/crd/client.go index e6ba8fba..5b8fff29 100644 --- a/crd/client.go +++ b/crd/client.go @@ -169,7 +169,7 @@ func waitForCRDs(crdClient *rest.RESTClient) error { return nil } - if time.Now().Sub(start) > 30*time.Second { + if time.Since(start) > 30*time.Second { return errors.New("timeout waiting for CRDs") } } diff --git a/crd/crd_test.go b/crd/crd_test.go index 061ca649..16f109f0 100644 --- a/crd/crd_test.go +++ b/crd/crd_test.go @@ -128,7 +128,7 @@ func functionTests(crdClient *rest.RESTClient) { if wf.Spec.Environment.Name != function.Spec.Environment.Name { log.Panicf("Bad object from watch: %#v", wf) } - log.Printf("watch event took %v", time.Now().Sub(start)) + log.Printf("watch event took %v", time.Since(start)) recvd = true } @@ -221,7 +221,7 @@ func environmentTests(crdClient *rest.RESTClient) { if obj.Spec.Runtime.Image != environment.Spec.Runtime.Image { log.Panicf("Bad object from watch: %#v", obj) } - log.Printf("watch event took %v", time.Now().Sub(start)) + log.Printf("watch event took %v", time.Since(start)) recvd = true } @@ -313,7 +313,7 @@ func httpTriggerTests(crdClient *rest.RESTClient) { if obj.Spec.Method != httpTrigger.Spec.Method { log.Panicf("Bad object from watch: %#v", obj) } - log.Printf("watch event took %v", time.Now().Sub(start)) + log.Printf("watch event took %v", time.Since(start)) recvd = true } @@ -408,7 +408,7 @@ func kubernetesWatchTriggerTests(crdClient *rest.RESTClient) { if obj.Spec.Type != kubernetesWatchTrigger.Spec.Type { log.Panicf("Bad object from watch: %#v", obj) } - log.Printf("watch event took %v", time.Now().Sub(start)) + log.Printf("watch event took %v", time.Since(start)) recvd = true } diff --git a/environments/fetcher/fetcher.go b/environments/fetcher/fetcher.go index faf52cb3..be8e5fcc 100644 --- a/environments/fetcher/fetcher.go +++ b/environments/fetcher/fetcher.go @@ -137,7 +137,7 @@ func (fetcher *Fetcher) FetchHandler(w http.ResponseWriter, r *http.Request) { startTime := time.Now() defer func() { - elapsed := time.Now().Sub(startTime) + elapsed := time.Since(startTime) log.Printf("elapsed time in fetch request = %v", elapsed) }() @@ -251,7 +251,7 @@ func (fetcher *Fetcher) UploadHandler(w http.ResponseWriter, r *http.Request) { startTime := time.Now() defer func() { - elapsed := time.Now().Sub(startTime) + elapsed := time.Since(startTime) log.Printf("elapsed time in upload request = %v", elapsed) }() diff --git a/executor/executor_test.go b/executor/executor_test.go index 4f0e5721..39da2e40 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -237,7 +237,7 @@ func TestExecutor(t *testing.T) { if err != nil { log.Panicf("failed to get func svc: %v", err) } - log.Printf("svc for function created at: %v (in %v)", svc, time.Now().Sub(t1)) + log.Printf("svc for function created at: %v (in %v)", svc, time.Since(t1)) // ensure that a pod with the label functionName=f.Metadata.Name exists podCount := countPods(kubeClient, functionNs, map[string]string{"functionName": f.Metadata.Name}) diff --git a/executor/fscache/functionServiceCache.go b/executor/fscache/functionServiceCache.go index 3398cd0c..26a24b89 100644 --- a/executor/fscache/functionServiceCache.go +++ b/executor/fscache/functionServiceCache.go @@ -100,7 +100,7 @@ func (fsc *FunctionServiceCache) service() { for _, funcSvc := range fscs { fsvc := funcSvc.(*FuncSvc) if fsvc.Environment.Metadata.UID == req.env.UID && - time.Now().Sub(fsvc.Atime) > req.age { + time.Since(fsvc.Atime) > req.age { funcObjects = append(funcObjects, fsvc) } } @@ -196,7 +196,7 @@ func (fsc *FunctionServiceCache) _touchByAddress(address string) error { } func (fsc *FunctionServiceCache) DeleteOld(fsvc *FuncSvc, minAge time.Duration) (bool, error) { - if time.Now().Sub(fsvc.Atime) < minAge { + if time.Since(fsvc.Atime) < minAge { return false, nil } diff --git a/executor/poolmgr/gp.go b/executor/poolmgr/gp.go index 718053ac..20432222 100644 --- a/executor/poolmgr/gp.go +++ b/executor/poolmgr/gp.go @@ -198,7 +198,7 @@ func (gp *GenericPool) _choosePod(newLabels map[string]string) (*apiv1.Pod, erro startTime := time.Now() for { // Retries took too long, error out. - if time.Now().Sub(startTime) > gp.podReadyTimeout { + if time.Since(startTime) > gp.podReadyTimeout { log.Printf("[%v] Erroring out, timed out", newLabels) return nil, errors.New("timeout: waited too long to get a ready pod") } @@ -260,7 +260,7 @@ func (gp *GenericPool) _choosePod(newLabels map[string]string) (*apiv1.Pod, erro continue } } - log.Printf("Chosen pod: %v (in %v)", chosenPod.ObjectMeta.Name, time.Now().Sub(startTime)) + log.Printf("Chosen pod: %v (in %v)", chosenPod.ObjectMeta.Name, time.Since(startTime)) return chosenPod, nil } } @@ -510,7 +510,7 @@ func (gp *GenericPool) waitForReadyPod() error { return nil } - if time.Now().Sub(startTime) > gp.podReadyTimeout { + if time.Since(startTime) > gp.podReadyTimeout { return errors.New("timeout: waited too long for pod to be ready") } time.Sleep(1000 * time.Millisecond) diff --git a/fission-bundle/main.go b/fission-bundle/main.go index 16cb48c9..c4c0d52b 100644 --- a/fission-bundle/main.go +++ b/fission-bundle/main.go @@ -163,15 +163,15 @@ Options: runExecutor(port, fissionNs, functionNs) } - if arguments["--kubewatcher"] == true { + if arguments["--kubewatcher"] { runKubeWatcher(routerUrl) } - if arguments["--timer"] == true { + if arguments["--timer"] { runTimer(routerUrl) } - if arguments["--mqt"] == true { + if arguments["--mqt"] { runMessageQueueMgr(routerUrl) } diff --git a/router/functionHandler.go b/router/functionHandler.go index 60b37ce2..2e26148d 100644 --- a/router/functionHandler.go +++ b/router/functionHandler.go @@ -163,7 +163,7 @@ func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request * initalTimeout: 50 * time.Millisecond, }, } - delay := time.Now().Sub(reqStartTime) + delay := time.Since(reqStartTime) if delay > 100*time.Millisecond { log.Printf("Request delay for %v: %v", serviceUrl, delay) } diff --git a/storagesvc/client/storagesvc_test.go b/storagesvc/client/storagesvc_test.go index 7e49ec9b..d94a60c5 100644 --- a/storagesvc/client/storagesvc_test.go +++ b/storagesvc/client/storagesvc_test.go @@ -81,7 +81,7 @@ func TestStorageService(t *testing.T) { panicIf(err) contents2, err := ioutil.ReadFile(retrievedfile.Name()) panicIf(err) - if bytes.Compare(contents1, contents2) != 0 { + if !bytes.Equal(contents1, contents2) { log.Panicf("Contents don't match") }