Skip to content

Commit cc1ddf0

Browse files
committed
wait for influxdb connection
Signed-off-by: Jeeva Kandasamy <jkandasa@gmail.com>
1 parent 66c06e9 commit cc1ddf0

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

plugin/database/metric/influxdb_v2/client.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const (
4040
)
4141

4242
const (
43-
defaultFlushInterval = 1 * time.Second
43+
defaultFlushInterval = 1 * time.Second
44+
defaultConnectTimeout = 10 * time.Second
4445
)
4546

4647
// Config of the influxdb_v2
@@ -56,6 +57,7 @@ type Config struct {
5657
Insecure bool `yaml:"insecure"`
5758
QueryClientVersion string `yaml:"query_client_version"`
5859
FlushInterval string `yaml:"flush_interval"`
60+
ConnectTimeout string `yaml:"connect_timeout"`
5961
}
6062

6163
// LoggerConfig struct
@@ -114,6 +116,9 @@ func NewClient(ctx context.Context, config cmap.CustomMap) (metricTY.Plugin, err
114116
flushInterval = defaultFlushInterval
115117
}
116118

119+
connectTimeout := utils.ToDuration(cfg.ConnectTimeout, defaultConnectTimeout)
120+
namedLogger.Debug("connect timeout details", zap.String("connectTimeout", cfg.ConnectTimeout), zap.Duration("connectTimeoutDuration", connectTimeout))
121+
117122
// replace influxdb2 logger with our custom logger
118123
_logger := getLogger(namedLogger)
119124
influxdb2log.Log = _logger
@@ -133,11 +138,28 @@ func NewClient(ctx context.Context, config cmap.CustomMap) (metricTY.Plugin, err
133138
logger: namedLogger,
134139
}
135140

136-
err = c.Ping()
137-
if err != nil {
138-
return nil, err
141+
// repeat ping in 5 seconds interval, until timeout
142+
pingInterval := 5 * time.Second
143+
ticker := time.NewTicker(pingInterval)
144+
defer ticker.Stop()
145+
146+
for {
147+
select {
148+
case <-time.After(connectTimeout):
149+
return nil, fmt.Errorf("influxdb connection timeout after %v", connectTimeout)
150+
case <-ticker.C:
151+
err = c.Ping()
152+
if err == nil {
153+
// ping successful, continue
154+
c.logger.Debug("influxdb ping successful")
155+
goto pingSuccess
156+
}
157+
c.logger.Warn("influxdb ping failed, retrying...", zap.Error(err))
158+
}
139159
}
140160

161+
pingSuccess:
162+
141163
influxAutoDetectVersion := ""
142164

143165
if influxAutoDetectVersion == "" {

0 commit comments

Comments
 (0)