From b0c0631468391008440ad9b6d7611e3f04f09ed8 Mon Sep 17 00:00:00 2001 From: Silas Davis Date: Wed, 13 Jul 2016 18:50:06 +0100 Subject: [PATCH 1/4] Spelling --- errors.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index e168a75b7..3a1b09542 100644 --- a/errors.go +++ b/errors.go @@ -23,23 +23,23 @@ func (se StackError) Error() string { // A panic resulting from a sanity check means there is a programmer error // and some gaurantee is not satisfied. func PanicSanity(v interface{}) { - panic(Fmt("Paniced on a Sanity Check: %v", v)) + panic(Fmt("Panicked on a Sanity Check: %v", v)) } // A panic here means something has gone horribly wrong, in the form of data corruption or // failure of the operating system. In a correct/healthy system, these should never fire. // If they do, it's indicative of a much more serious problem. func PanicCrisis(v interface{}) { - panic(Fmt("Paniced on a Crisis: %v", v)) + panic(Fmt("Panicked on a Crisis: %v", v)) } // Indicates a failure of consensus. Someone was malicious or something has // gone horribly wrong. These should really boot us into an "emergency-recover" mode func PanicConsensus(v interface{}) { - panic(Fmt("Paniced on a Consensus Failure: %v", v)) + panic(Fmt("Panicked on a Consensus Failure: %v", v)) } // For those times when we're not sure if we should panic func PanicQ(v interface{}) { - panic(Fmt("Paniced questionably: %v", v)) + panic(Fmt("Panicked questionably: %v", v)) } From c46ffe39a894fb0d7ba0ddc5caf2af5b7e779da5 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 27 Mar 2017 20:46:46 +0400 Subject: [PATCH 2/4] [service] check for error returned by impl otherwise, we mark it as started when it is not in fact --- service.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/service.go b/service.go index e2d31925b..8cd064721 100644 --- a/service.go +++ b/service.go @@ -94,6 +94,11 @@ func (bs *BaseService) Start() (bool, error) { } } err := bs.impl.OnStart() + if err != nil { + // revert flag + atomic.StoreUint32(&bs.started, 0) + return false, err + } return true, err } else { if bs.log != nil { From 7a12594edb113e07b864ff4fb5255e56e3cb70c1 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 28 Mar 2017 13:56:48 +0400 Subject: [PATCH 3/4] [service] recreate Quit channel on reset don't think that user should do this thing him/herself --- service.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service.go b/service.go index e2d31925b..f17371336 100644 --- a/service.go +++ b/service.go @@ -136,6 +136,7 @@ func (bs *BaseService) Reset() (bool, error) { // whether or not we've started, we can reset atomic.CompareAndSwapUint32(&bs.started, 1, 0) + bs.Quit = make(chan struct{}) return true, bs.impl.OnReset() } else { if bs.log != nil { From dec518eb06c0823bd6462a64decf59663402332c Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 29 Mar 2017 16:03:05 +0400 Subject: [PATCH 4/4] update comment [ci skip] [circleci skip] --- service.go | 81 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/service.go b/service.go index 8cd064721..c263bfa81 100644 --- a/service.go +++ b/service.go @@ -1,42 +1,3 @@ -/* - -Classical-inheritance-style service declarations. -Services can be started, then stopped, then optionally restarted. -Users can override the OnStart/OnStop methods. -By default, these methods are guaranteed to be called at most once. -A call to Reset will panic, unless OnReset is overwritten, allowing OnStart/OnStop to be called again. -Caller must ensure that Start() and Stop() are not called concurrently. -It is ok to call Stop() without calling Start() first. -Services cannot be re-started unless OnReset is overwritten to allow it. - -Typical usage: - -type FooService struct { - BaseService - // private fields -} - -func NewFooService() *FooService { - fs := &FooService{ - // init - } - fs.BaseService = *NewBaseService(log, "FooService", fs) - return fs -} - -func (fs *FooService) OnStart() error { - fs.BaseService.OnStart() // Always call the overridden method. - // initialize private fields - // start subroutines, etc. -} - -func (fs *FooService) OnStop() error { - fs.BaseService.OnStop() // Always call the overridden method. - // close/destroy private fields - // stop subroutines, etc. -} - -*/ package common import ( @@ -60,6 +21,48 @@ type Service interface { String() string } +/* +Classical-inheritance-style service declarations. Services can be started, then +stopped, then optionally restarted. + +Users can override the OnStart/OnStop methods. In the absence of errors, these +methods are guaranteed to be called at most once. If OnStart returns an error, +service won't be marked as started, so the user can call Start again. + +A call to Reset will panic, unless OnReset is overwritten, allowing +OnStart/OnStop to be called again. + +The caller must ensure that Start and Stop are not called concurrently. + +It is ok to call Stop without calling Start first. + +Typical usage: + + type FooService struct { + BaseService + // private fields + } + + func NewFooService() *FooService { + fs := &FooService{ + // init + } + fs.BaseService = *NewBaseService(log, "FooService", fs) + return fs + } + + func (fs *FooService) OnStart() error { + fs.BaseService.OnStart() // Always call the overridden method. + // initialize private fields + // start subroutines, etc. + } + + func (fs *FooService) OnStop() error { + fs.BaseService.OnStop() // Always call the overridden method. + // close/destroy private fields + // stop subroutines, etc. + } +*/ type BaseService struct { log log15.Logger name string