Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relaxed init settings. #5

Merged
merged 6 commits into from Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions json_logger.go
Expand Up @@ -58,6 +58,11 @@ type JSONLoggerConfig struct {
// older messages will start being dropped. Defaults to 100
FailureQueueLength int

// Whether an active connection is required on initialization. Defaults to false.
// If true, the Init function will return a nil error on connection failure, but
// retry to connect in the background.
AllowDisconnectedInit bool

// The backoff strategy to use when reconnecting a connection
ReconnectBackoff backoff.Backoff
}
Expand Down Expand Up @@ -144,6 +149,7 @@ func (l *JSONLogger) connect() error {

return nil
}

func (l *JSONLogger) disconnect() error {
l.connMut.Lock()
defer l.connMut.Unlock()
Expand All @@ -165,6 +171,14 @@ func (l *JSONLogger) SetBase(base *gomol.Base) {
l.base = base
}

// Healthy will return true if the logger is connected to the remote host
func (l *JSONLogger) Healthy() bool {
l.connMut.RLock()
defer l.connMut.RUnlock()

return l.isConnected
}

// InitLogger does any initialization the logger may need before being used
func (l *JSONLogger) InitLogger() error {
if len(l.config.HostURI) == 0 {
Expand All @@ -184,9 +198,13 @@ func (l *JSONLogger) InitLogger() error {
l.unprefixedMap[fieldName] = true
}

err = l.connect()
if err != nil {
return err
if err := l.connect(); err != nil {
if !l.config.AllowDisconnectedInit {
return err
}

// Reconnect in background
l.performReconnect()
}

l.isInitialized = true
Expand Down
33 changes: 33 additions & 0 deletions json_logger_test.go
Expand Up @@ -2,6 +2,7 @@ package gomoljson

import (
"errors"
"net"
"testing"
"time"

Expand Down Expand Up @@ -86,6 +87,8 @@ func (s *GomolSuite) TestInitializeInvalidHostURI(t sweet.T) {

func (s *GomolSuite) TestInitializeConnectFailure(t sweet.T) {
cfg, fd := newFakeCfg()
cfg.AllowDisconnectedInit = false

fd.DialError = errors.New("Dial error")
l, _ := NewJSONLogger(cfg)
Expect(l).ToNot(BeNil())
Expand All @@ -95,6 +98,36 @@ func (s *GomolSuite) TestInitializeConnectFailure(t sweet.T) {
Expect(err.Error()).To(Equal("Dial error"))
}

func (s *GomolSuite) TestInitializeConnectInBackground(t sweet.T) {
sync := make(chan struct{})
dials := 0

cfg, _ := newFakeCfg()
cfg.AllowDisconnectedInit = true

cfg.netDial = func(network string, address string) (net.Conn, error) {
dials++
if dials < 3 {
return nil, errors.New("Dial error")
}

if dials == 3 {
close(sync)
}

return newFakeConn(network, address), nil
}

l, _ := NewJSONLogger(cfg)
Expect(l).ToNot(BeNil())

err := l.InitLogger()
Expect(err).To(BeNil())
Expect(l.Healthy()).To(BeFalse())
<-sync
Eventually(l.Healthy()).Should(BeTrue())
}

func (s *GomolSuite) TestConnectWithExistingConnection(t sweet.T) {
cfg, _ := newFakeCfg()
l, _ := NewJSONLogger(cfg)
Expand Down