libs/common: refactor libs common 3 (#4232)

* libs/common: refactor libs common 3

- move nil.go into types folder and make private
- move service & baseservice out of common into service pkg

ref #4147

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* add changelog entry
This commit is contained in:
Marko
2019-12-11 09:31:25 +01:00
committed by GitHub
parent efc27ab0b9
commit 27b00cf8d1
50 changed files with 163 additions and 150 deletions

View File

@@ -14,7 +14,7 @@ import (
"sync"
"time"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/service"
)
const (
@@ -53,7 +53,7 @@ The Group can also be used to binary-search for some line,
assuming that marker lines are written occasionally.
*/
type Group struct {
cmn.BaseService
service.BaseService
ID string
Head *AutoFile // The head AutoFile to write to
@@ -102,7 +102,7 @@ func OpenGroup(headPath string, groupOptions ...func(*Group)) (g *Group, err err
option(g)
}
g.BaseService = *cmn.NewBaseService(nil, "Group", g)
g.BaseService = *service.NewBaseService(nil, "Group", g)
gInfo := g.readGroupInfo()
g.minIndex = gInfo.MinIndex
@@ -131,7 +131,7 @@ func GroupTotalSizeLimit(limit int64) func(*Group) {
}
}
// OnStart implements cmn.Service by starting the goroutine that checks file
// OnStart implements service.Service by starting the goroutine that checks file
// and group limits.
func (g *Group) OnStart() error {
g.ticker = time.NewTicker(g.groupCheckDuration)
@@ -139,7 +139,7 @@ func (g *Group) OnStart() error {
return nil
}
// OnStop implements cmn.Service by stopping the goroutine described above.
// OnStop implements service.Service by stopping the goroutine described above.
// NOTE: g.Head must be closed separately using Close.
func (g *Group) OnStop() {
g.ticker.Stop()

View File

@@ -1,29 +0,0 @@
package common
import "reflect"
// Go lacks a simple and safe way to see if something is a typed nil.
// See:
// - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2
// - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion
// - https://github.com/golang/go/issues/21538
func IsTypedNil(o interface{}) bool {
rv := reflect.ValueOf(o)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
// Returns true if it has zero length.
func IsEmpty(o interface{}) bool {
rv := reflect.ValueOf(o)
switch rv.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return rv.Len() == 0
default:
return false
}
}

View File

@@ -110,7 +110,7 @@ via concrete implementation of this interface
## <a name="EventSwitch">type</a> [EventSwitch](/src/target/events.go?s=560:771#L29)
``` go
type EventSwitch interface {
cmn.Service
service.Service
Fireable
AddListenerForEvent(listenerID, event string, cb EventCallback)

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"sync"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/service"
)
// ErrListenerWasRemoved is returned by AddEvent if the listener was removed.
@@ -43,7 +43,7 @@ type Fireable interface {
// They can be removed by calling either RemoveListenerForEvent or
// RemoveListener (for all events).
type EventSwitch interface {
cmn.Service
service.Service
Fireable
AddListenerForEvent(listenerID, event string, cb EventCallback) error
@@ -52,7 +52,7 @@ type EventSwitch interface {
}
type eventSwitch struct {
cmn.BaseService
service.BaseService
mtx sync.RWMutex
eventCells map[string]*eventCell
@@ -64,7 +64,7 @@ func NewEventSwitch() EventSwitch {
eventCells: make(map[string]*eventCell),
listeners: make(map[string]*eventListener),
}
evsw.BaseService = *cmn.NewBaseService(nil, "EventSwitch", evsw)
evsw.BaseService = *service.NewBaseService(nil, "EventSwitch", evsw)
return evsw
}

View File

@@ -39,7 +39,7 @@ import (
"sync"
"github.com/pkg/errors"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/service"
)
type operation int
@@ -88,7 +88,7 @@ type cmd struct {
// Server allows clients to subscribe/unsubscribe for messages, publishing
// messages with or without events, and manages internal state.
type Server struct {
cmn.BaseService
service.BaseService
cmds chan cmd
cmdsCap int
@@ -109,7 +109,7 @@ func NewServer(options ...Option) *Server {
s := &Server{
subscriptions: make(map[string]map[string]struct{}),
}
s.BaseService = *cmn.NewBaseService(nil, "PubSub", s)
s.BaseService = *service.NewBaseService(nil, "PubSub", s)
for _, option := range options {
option(s)

View File

@@ -1,4 +1,4 @@
package common
package service
import (
"errors"

View File

@@ -1,4 +1,4 @@
package common
package service
import (
"testing"