From 904d168f180676ea6e7879f1d4ab2d02f30517ec Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 22 Feb 2021 22:43:24 +0800 Subject: [PATCH] fix golint issues in core/service (#512) --- core/service/serviceconf.go | 13 ++++++++++--- core/service/servicegroup.go | 27 ++++++++++++++++++--------- core/service/servicegroup_test.go | 6 +++--- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/core/service/serviceconf.go b/core/service/serviceconf.go index 91cf6676..33eb09bf 100644 --- a/core/service/serviceconf.go +++ b/core/service/serviceconf.go @@ -10,12 +10,17 @@ import ( ) const ( - DevMode = "dev" + // DevMode means development mode. + DevMode = "dev" + // TestMode means test mode. TestMode = "test" - PreMode = "pre" - ProMode = "pro" + // PreMode means pre-release mode. + PreMode = "pre" + // ProMode means production mode. + ProMode = "pro" ) +// A ServiceConf is a service config. type ServiceConf struct { Name string Log logx.LogConf @@ -24,12 +29,14 @@ type ServiceConf struct { Prometheus prometheus.Config `json:",optional"` } +// MustSetUp sets up the service, exits on error. func (sc ServiceConf) MustSetUp() { if err := sc.SetUp(); err != nil { log.Fatal(err) } } +// SetUp sets up the service. func (sc ServiceConf) SetUp() error { if len(sc.Log.ServiceName) == 0 { sc.Log.ServiceName = sc.Name diff --git a/core/service/servicegroup.go b/core/service/servicegroup.go index fff4ec17..0968a627 100644 --- a/core/service/servicegroup.go +++ b/core/service/servicegroup.go @@ -9,39 +9,45 @@ import ( ) type ( + // Starter is the interface wraps the Start method. Starter interface { Start() } + // Stopper is the interface wraps the Stop method. Stopper interface { Stop() } + // Service is the interface that groups Start and Stop methods. Service interface { Starter Stopper } - ServiceGroup struct { + // A Group is a group of services. + Group struct { services []Service stopOnce func() } ) -func NewServiceGroup() *ServiceGroup { - sg := new(ServiceGroup) +// NewGroup returns a Group. +func NewGroup() *Group { + sg := new(Group) sg.stopOnce = syncx.Once(sg.doStop) return sg } -func (sg *ServiceGroup) Add(service Service) { +// Add adds service into sg. +func (sg *Group) Add(service Service) { sg.services = append(sg.services, service) } -// Start starts the ServiceGroup. +// Start starts the Group. // There should not be any logic code after calling this method, because this method is a blocking one. // Also, quitting this method will close the logx output. -func (sg *ServiceGroup) Start() { +func (sg *Group) Start() { proc.AddShutdownListener(func() { log.Println("Shutting down...") sg.stopOnce() @@ -50,11 +56,12 @@ func (sg *ServiceGroup) Start() { sg.doStart() } -func (sg *ServiceGroup) Stop() { +// Stop stops the Group. +func (sg *Group) Stop() { sg.stopOnce() } -func (sg *ServiceGroup) doStart() { +func (sg *Group) doStart() { routineGroup := threading.NewRoutineGroup() for i := range sg.services { @@ -67,18 +74,20 @@ func (sg *ServiceGroup) doStart() { routineGroup.Wait() } -func (sg *ServiceGroup) doStop() { +func (sg *Group) doStop() { for _, service := range sg.services { service.Stop() } } +// WithStart wraps a start func as a Service. func WithStart(start func()) Service { return startOnlyService{ start: start, } } +// WithStarter wraps a Starter as a Service. func WithStarter(start Starter) Service { return starterOnlyService{ Starter: start, diff --git a/core/service/servicegroup_test.go b/core/service/servicegroup_test.go index 7f2165dd..e431cad5 100644 --- a/core/service/servicegroup_test.go +++ b/core/service/servicegroup_test.go @@ -41,7 +41,7 @@ func TestServiceGroup(t *testing.T) { multipliers := []int{2, 3, 5, 7} want := 1 - group := NewServiceGroup() + group := NewGroup() for _, multiplier := range multipliers { want *= multiplier service := newMockedService(multiplier) @@ -68,7 +68,7 @@ func TestServiceGroup_WithStart(t *testing.T) { var wait sync.WaitGroup var lock sync.Mutex wait.Add(len(multipliers)) - group := NewServiceGroup() + group := NewGroup() for _, multiplier := range multipliers { var mul = multiplier group.Add(WithStart(func() { @@ -95,7 +95,7 @@ func TestServiceGroup_WithStarter(t *testing.T) { var wait sync.WaitGroup var lock sync.Mutex wait.Add(len(multipliers)) - group := NewServiceGroup() + group := NewGroup() for _, multiplier := range multipliers { var mul = multiplier group.Add(WithStarter(mockedStarter{