little-cui closed pull request #522: SCB-1092 Use a simple time struct to save
memory
URL: https://github.com/apache/servicecomb-service-center/pull/522
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/pkg/notify/notice.go b/pkg/notify/notice.go
index 87c0862b..e5ac2654 100644
--- a/pkg/notify/notice.go
+++ b/pkg/notify/notice.go
@@ -16,7 +16,10 @@
*/
package notify
-import "time"
+import (
+ simple "github.com/apache/servicecomb-service-center/pkg/time"
+ "time"
+)
type Event interface {
Type() Type
@@ -29,7 +32,7 @@ type baseEvent struct {
nType Type
subject string
group string
- createAt time.Time
+ createAt simple.Time
}
func (s *baseEvent) Type() Type {
@@ -45,9 +48,9 @@ func (s *baseEvent) Group() string {
}
func (s *baseEvent) CreateAt() time.Time {
- return s.createAt
+ return s.createAt.Local()
}
func NewEvent(t Type, s string, g string) Event {
- return &baseEvent{t, s, g, time.Now()}
+ return &baseEvent{t, s, g, simple.FromTime(time.Now())}
}
diff --git a/pkg/notify/notification_test.go b/pkg/notify/notification_test.go
index 9b6a06c5..c1b3c5da 100644
--- a/pkg/notify/notification_test.go
+++ b/pkg/notify/notification_test.go
@@ -17,6 +17,7 @@
package notify
import (
+ simple "github.com/apache/servicecomb-service-center/pkg/time"
"testing"
"time"
)
@@ -59,7 +60,7 @@ func TestGetNotifyService(t *testing.T) {
if err != nil {
t.Fatalf("TestGetNotifyService failed, %v", err)
}
- j := &baseEvent{INSTANCE, "s", "g", time.Now()}
+ j := &baseEvent{INSTANCE, "s", "g", simple.FromTime(time.Now())}
err = notifyService.Publish(j)
if err != nil {
t.Fatalf("TestGetNotifyService failed")
diff --git a/pkg/time/time.go b/pkg/time/time.go
new file mode 100644
index 00000000..a0524b61
--- /dev/null
+++ b/pkg/time/time.go
@@ -0,0 +1,40 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package time
+
+import "time"
+
+type Time struct {
+ sec int64
+ nsec int64
+}
+
+func (t Time) String() string {
+ return t.Local().String()
+}
+
+func (t Time) UTC() time.Time {
+ return time.Unix(t.sec, t.nsec).UTC()
+}
+
+func (t Time) Local() time.Time {
+ return time.Unix(t.sec, t.nsec).Local()
+}
+
+func FromTime(t time.Time) Time {
+ utc := t.UTC()
+ return Time{utc.Unix(), int64(utc.Nanosecond())}
+}
diff --git a/pkg/time/time_test.go b/pkg/time/time_test.go
new file mode 100644
index 00000000..887c676d
--- /dev/null
+++ b/pkg/time/time_test.go
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package time
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+func TestNewTime(t *testing.T) {
+ now := time.Now().Local()
+ tt := FromTime(now)
+ if tt.String() != now.String() {
+ t.Fatal("TestNewTime failed", tt, "!=", now)
+ }
+ if tt.UTC().String() != now.UTC().String() {
+ t.Fatal("TestNewTime failed", tt, "!=", now)
+ }
+ fmt.Println("local:", tt, "utc:", tt.UTC())
+}
diff --git a/server/core/backend/lease.go b/server/core/backend/lease.go
index 5c6c2b35..f31737eb 100644
--- a/server/core/backend/lease.go
+++ b/server/core/backend/lease.go
@@ -19,6 +19,7 @@ package backend
import (
errorsEx "github.com/apache/servicecomb-service-center/pkg/errors"
"github.com/apache/servicecomb-service-center/pkg/log"
+ simple "github.com/apache/servicecomb-service-center/pkg/time"
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/server/plugin/pkg/registry"
"golang.org/x/net/context"
@@ -32,8 +33,7 @@ type LeaseTask struct {
LeaseID int64
TTL int64
- recvSec int64
- recvNsec int64
+ recvTime simple.Time
err error
}
@@ -78,17 +78,15 @@ func (lat *LeaseTask) Err() error {
}
func (lat *LeaseTask) ReceiveTime() time.Time {
- return time.Unix(lat.recvSec, lat.recvNsec).Local()
+ return lat.recvTime.Local()
}
func NewLeaseAsyncTask(op registry.PluginOp) *LeaseTask {
- now := time.Now().UTC()
return &LeaseTask{
Client: Registry(),
key:
ToLeaseAsyncTaskKey(util.BytesToStringWithNoCopy(op.Key)),
LeaseID: op.Lease,
- recvSec: now.Unix(),
- recvNsec: int64(now.Nanosecond()),
+ recvTime: simple.FromTime(time.Now()),
}
}
diff --git a/server/core/backend/lease_test.go
b/server/core/backend/lease_test.go
index cc8538d0..863063bb 100644
--- a/server/core/backend/lease_test.go
+++ b/server/core/backend/lease_test.go
@@ -19,6 +19,7 @@ package backend
import (
"fmt"
errorsEx "github.com/apache/servicecomb-service-center/pkg/errors"
+ simple "github.com/apache/servicecomb-service-center/pkg/time"
"github.com/apache/servicecomb-service-center/server/plugin/pkg/registry/buildin"
"golang.org/x/net/context"
"testing"
@@ -38,14 +39,12 @@ func (c *mockRegistry) LeaseRenew(ctx context.Context,
leaseID int64) (TTL int64
}
func TestLeaseTask_Do(t *testing.T) {
- now := time.Now().UTC()
c := &mockRegistry{}
lt := &LeaseTask{
Client: c,
key: ToLeaseAsyncTaskKey("/a"),
LeaseID: 1,
- recvSec: now.Unix(),
- recvNsec: int64(now.Nanosecond()),
+ recvTime: simple.FromTime(time.Now()),
}
c.LeaseErr = errorsEx.InternalError("lease not found")
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services