libs/service: pass logger explicitly (#7288)

This is a very small change, but removes a method from the
`service.Service` interface (a win!) and forces callers to explicitly
pass loggers in to objects during construction rather than (later)
injecting them. There's not a real need for this kind of lazy
construction of loggers, and I think a decent potential for confusion
for mutable loggers.

The main concern I have is that this changes the constructor API for
ABCI clients. I think this is fine, and I suspect that as we plumb
contexts through, and make changes to the RPC services there'll be a
number of similar sorts of changes to various (quasi) public
interfaces, which I think we should welcome.
This commit is contained in:
Sam Kleinman
2021-11-16 16:20:56 +00:00
committed by GitHub
parent dbac109d01
commit d7606777cf
57 changed files with 347 additions and 356 deletions
+1 -1
View File
@@ -136,10 +136,10 @@ type Option func(*Server)
// provided, the resulting server's queue is unbuffered.
func NewServer(options ...Option) *Server {
s := new(Server)
s.BaseService = *service.NewBaseService(nil, "PubSub", s)
for _, opt := range options {
opt(s)
}
s.BaseService = *service.NewBaseService(nil, "PubSub", s)
// The queue receives items to be published.
s.queue = make(chan item, s.queueCap)
+8 -4
View File
@@ -357,8 +357,10 @@ func TestUnsubscribeAll(t *testing.T) {
}
func TestBufferCapacity(t *testing.T) {
s := pubsub.NewServer(pubsub.BufferCapacity(2))
s.SetLogger(log.TestingLogger())
s := pubsub.NewServer(pubsub.BufferCapacity(2),
func(s *pubsub.Server) {
s.Logger = log.TestingLogger()
})
require.Equal(t, 2, s.BufferCapacity())
@@ -376,8 +378,10 @@ func TestBufferCapacity(t *testing.T) {
func newTestServer(t testing.TB) *pubsub.Server {
t.Helper()
s := pubsub.NewServer()
s.SetLogger(log.TestingLogger())
s := pubsub.NewServer(func(s *pubsub.Server) {
s.Logger = log.TestingLogger()
})
require.NoError(t, s.Start())
t.Cleanup(func() {
assert.NoError(t, s.Stop())
-8
View File
@@ -48,9 +48,6 @@ type Service interface {
// String representation of the service
String() string
// SetLogger sets a logger.
SetLogger(log.Logger)
// Wait blocks until the service is stopped.
Wait()
}
@@ -122,11 +119,6 @@ func NewBaseService(logger log.Logger, name string, impl Service) *BaseService {
}
}
// SetLogger implements Service by setting a logger.
func (bs *BaseService) SetLogger(l log.Logger) {
bs.Logger = l
}
// Start implements Service by calling OnStart (if defined). An error will be
// returned if the service is already running or stopped. Not to start the
// stopped service, you need to call Reset.