From 677e0a2ec4cadf78151c0779966400ac3c827175 Mon Sep 17 00:00:00 2001
From: guoguangwu <guoguangwu@magic-shield.com>
Date: Wed, 31 May 2023 13:05:48 +0800
Subject: [PATCH] code optimization

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
---
 blobstore/blobstore_test.go | 13 ++++++-------
 blobstore/read.go           |  7 +++----
 datastore/datastore_test.go |  2 +-
 datastore/prop_test.go      |  2 +-
 internal/api.go             |  4 ++--
 5 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/blobstore/blobstore_test.go b/blobstore/blobstore_test.go
index 4616211e..957fba3e 100644
--- a/blobstore/blobstore_test.go
+++ b/blobstore/blobstore_test.go
@@ -13,7 +13,6 @@ import (
 	"mime/quotedprintable"
 	"net/http"
 	"net/textproto"
-	"os"
 	"strconv"
 	"strings"
 	"testing"
@@ -89,9 +88,9 @@ var readerTest = []struct {
 		{"Read", 1, 0, 0, "e", nil},
 		{"Read", 2, 0, 0, "fg", nil},
 		// Test Seek.
-		{"Seek", 0, 2, os.SEEK_SET, "2", nil},
+		{"Seek", 0, 2, io.SeekStart, "2", nil},
 		{"Read", 5, 0, 0, "cdefg", nil},
-		{"Seek", 0, 2, os.SEEK_CUR, "9", nil},
+		{"Seek", 0, 2, io.SeekCurrent, "9", nil},
 		{"Read", 1, 0, 0, "j", nil},
 		// Test reads up to and past EOF.
 		{"Read", 5, 0, 0, "klmno", nil},
@@ -106,7 +105,7 @@ var readerTest = []struct {
 	}},
 	{"a14p.1", []step{
 		// Test Seek before any reads.
-		{"Seek", 0, 2, os.SEEK_SET, "2", nil},
+		{"Seek", 0, 2, io.SeekStart, "2", nil},
 		{"Read", 1, 0, 0, "c", nil},
 		// Test that ReadAt doesn't affect the Read offset.
 		{"ReadAt", 3, 9, 0, "jkl", nil},
@@ -120,11 +119,11 @@ var readerTest = []struct {
 		// Test basic read.
 		{"Read", 1, 0, 0, "A", nil},
 		// Test that Read returns early when the buffer is exhausted.
-		{"Seek", 0, rbs - 2, os.SEEK_SET, strconv.Itoa(rbs - 2), nil},
+		{"Seek", 0, rbs - 2, io.SeekStart, strconv.Itoa(rbs - 2), nil},
 		{"Read", 5, 0, 0, "AA", nil},
 		{"Read", 3, 0, 0, "BBB", nil},
 		// Test that what we just read is still in the buffer.
-		{"Seek", 0, rbs - 2, os.SEEK_SET, strconv.Itoa(rbs - 2), nil},
+		{"Seek", 0, rbs - 2, io.SeekStart, strconv.Itoa(rbs - 2), nil},
 		{"Read", 5, 0, 0, "AABBB", nil},
 		// Test ReadAt.
 		{"ReadAt", 3, rbs - 4, 0, "AAA", nil},
@@ -133,7 +132,7 @@ var readerTest = []struct {
 		{"ReadAt", 5, rbs - 4, 0, "AAAAB", nil},
 		{"ReadAt", 2, rbs - 4, 0, "AA", nil},
 		// Test seeking backwards from the Read offset.
-		{"Seek", 0, 2*rbs - 8, os.SEEK_SET, strconv.Itoa(2*rbs - 8), nil},
+		{"Seek", 0, 2*rbs - 8, io.SeekStart, strconv.Itoa(2*rbs - 8), nil},
 		{"Read", 1, 0, 0, "B", nil},
 		{"Read", 1, 0, 0, "B", nil},
 		{"Read", 1, 0, 0, "B", nil},
diff --git a/blobstore/read.go b/blobstore/read.go
index 6fec0e72..e8c84bf6 100644
--- a/blobstore/read.go
+++ b/blobstore/read.go
@@ -9,7 +9,6 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"os"
 	"sync"
 
 	"github.com/golang/protobuf/proto"
@@ -113,11 +112,11 @@ func (r *reader) Seek(offset int64, whence int) (ret int64, err error) {
 	r.mu.Lock()
 	defer r.mu.Unlock()
 	switch whence {
-	case os.SEEK_SET:
+	case io.SeekStart:
 		ret = offset
-	case os.SEEK_CUR:
+	case io.SeekCurrent:
 		ret = r.off + int64(r.r) + offset
-	case os.SEEK_END:
+	case io.SeekEnd:
 		return 0, errors.New("seeking relative to the end of a blob isn't supported")
 	default:
 		return 0, fmt.Errorf("invalid Seek whence value: %d", whence)
diff --git a/datastore/datastore_test.go b/datastore/datastore_test.go
index 683cd15f..f267f367 100644
--- a/datastore/datastore_test.go
+++ b/datastore/datastore_test.go
@@ -1452,7 +1452,7 @@ var testCases = []testCase{
 func checkErr(want string, err error) string {
 	if err != nil {
 		got := err.Error()
-		if want == "" || strings.Index(got, want) == -1 {
+		if want == "" || !strings.Contains(got, want) {
 			return got
 		}
 	} else if want != "" {
diff --git a/datastore/prop_test.go b/datastore/prop_test.go
index 646a18f0..f67eb493 100644
--- a/datastore/prop_test.go
+++ b/datastore/prop_test.go
@@ -563,7 +563,7 @@ func TestSaveStructOmitEmpty(t *testing.T) {
 				actualPropNames[i] = props[i].Name
 			}
 			// Sort actuals for comparing with already sorted expected names
-			sort.Sort(sort.StringSlice(actualPropNames))
+			sort.Strings(actualPropNames)
 			if !reflect.DeepEqual(actualPropNames, expectedPropNames) {
 				t.Errorf("Expected this properties: %v, got: %v", expectedPropNames, actualPropNames)
 			}
diff --git a/internal/api.go b/internal/api.go
index 0569f5dd..62433cfb 100644
--- a/internal/api.go
+++ b/internal/api.go
@@ -453,7 +453,7 @@ func Call(ctx context.Context, service, method string, in, out proto.Message) er
 	// Default RPC timeout is 60s.
 	timeout := 60 * time.Second
 	if deadline, ok := ctx.Deadline(); ok {
-		timeout = deadline.Sub(time.Now())
+		timeout = time.Until(deadline)
 	}
 
 	data, err := proto.Marshal(in)
@@ -634,7 +634,7 @@ func (c *aeContext) logFlusher(stop <-chan int) {
 			tick.Stop()
 			return
 		case <-tick.C:
-			force := time.Now().Sub(lastFlush) > forceFlushInterval
+			force := time.Since(lastFlush) > forceFlushInterval
 			if c.flushLog(force) {
 				lastFlush = time.Now()
 			}