This is an automated email from the ASF dual-hosted git repository. littlecui pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
commit ab938885f074bf39ce82628e71ec73a19a80ad0e Author: little-cui <[email protected]> AuthorDate: Sat Dec 29 19:31:12 2018 +0800 SCB-1092 Use a simple time struct to save memory (#522) --- pkg/notify/notice.go | 11 +++++++---- pkg/notify/notification_test.go | 3 ++- pkg/time/time.go | 40 +++++++++++++++++++++++++++++++++++++++ pkg/time/time_test.go | 34 +++++++++++++++++++++++++++++++++ server/core/backend/lease.go | 10 ++++------ server/core/backend/lease_test.go | 5 ++--- server/rest/reporter.go | 4 ++++ 7 files changed, 93 insertions(+), 14 deletions(-) diff --git a/pkg/notify/notice.go b/pkg/notify/notice.go index 87c0862..e5ac265 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 9b6a06c..c1b3c5d 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 0000000..a0524b6 --- /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 0000000..887c676 --- /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 5c6c2b3..f31737e 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 cc8538d..863063b 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") diff --git a/server/rest/reporter.go b/server/rest/reporter.go index 2b9a2fb..6c4e291 100644 --- a/server/rest/reporter.go +++ b/server/rest/reporter.go @@ -37,6 +37,10 @@ type APIReporter struct { func (r *APIReporter) Report() { details := metric.Gatherer.Records.Get(httpRequestTotal) + if details == nil { + return + } + defer func() { r.cache = details }() if r.cache == nil {
