Skip to content

Commit 97d7f5d

Browse files
nokute78edsiper
authored andcommitted
in_docker_events: support reconnect options.
- reconnect.retry_limits: The maximum number of retries allowed. - reconnect.retry_inteval: The retrying interval. Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent f6e1bdc commit 97d7f5d

File tree

2 files changed

+87
-26
lines changed

2 files changed

+87
-26
lines changed

plugins/in_docker_events/docker_events.c

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,78 @@ static int de_unix_create(struct flb_in_de_config *ctx)
7575
return 0;
7676
}
7777

78+
static int in_de_collect(struct flb_input_instance *ins,
79+
struct flb_config *config, void *in_context);
80+
81+
static int reconnect_docker_sock(struct flb_input_instance *ins,
82+
struct flb_config *config,
83+
struct flb_in_de_config *ctx)
84+
{
85+
int ret;
86+
87+
/* remove old socket collector */
88+
if (ctx->coll_id >= 0) {
89+
ret = flb_input_collector_delete(ctx->coll_id, ins);
90+
if (ret < 0) {
91+
flb_plg_error(ctx->ins, "failed to pause event");
92+
return -1;
93+
}
94+
ctx->coll_id = -1;
95+
}
96+
if (ctx->fd > 0) {
97+
close(ctx->fd);
98+
ctx->fd = -1;
99+
}
100+
101+
/* create socket again */
102+
if (de_unix_create(ctx) < 0) {
103+
flb_plg_error(ctx->ins, "failed to re-initialize socket");
104+
return -1;
105+
}
106+
/* set event */
107+
ctx->coll_id = flb_input_set_collector_event(ins,
108+
in_de_collect,
109+
ctx->fd, config);
110+
if (ctx->coll_id < 0) {
111+
flb_plg_error(ctx->ins,
112+
"could not set collector for IN_DOCKER_EVENTS plugin");
113+
close(ctx->fd);
114+
ctx->fd = -1;
115+
return -1;
116+
}
117+
ret = flb_input_collector_start(ctx->coll_id, ins);
118+
if (ret < 0) {
119+
flb_plg_error(ctx->ins,
120+
"could not start collector for IN_DOCKER_EVENTS plugin");
121+
flb_input_collector_delete(ctx->coll_id, ins);
122+
close(ctx->fd);
123+
ctx->coll_id = -1;
124+
ctx->fd = -1;
125+
return -1;
126+
}
127+
return 0;
128+
}
129+
130+
static int reconnect_docker_sock_retry(struct flb_input_instance *ins,
131+
struct flb_config *config,
132+
struct flb_in_de_config *ctx)
133+
{
134+
int i;
135+
int ret;
136+
for (i=0; i<ctx->reconnect_retry_limits; i++) {
137+
ret = reconnect_docker_sock(ins, config, ctx);
138+
if (ret >= 0) {
139+
return ret;
140+
}
141+
flb_plg_info(ctx->ins, "Retry(%d) after %d seconds.",i+1,
142+
ctx->reconnect_retry_interval);
143+
sleep(ctx->reconnect_retry_interval);
144+
}
145+
146+
flb_plg_error(ctx->ins, "Failed to reconnect docker.");
147+
return -1;
148+
}
149+
78150
/**
79151
* Callback function to process events recieved on the unix
80152
* socket.
@@ -157,35 +229,12 @@ static int in_de_collect(struct flb_input_instance *ins,
157229
}
158230
else if (ret == 0) {
159231
/* EOF */
232+
160233
/* docker service may be restarted */
161234
flb_plg_info(ctx->ins, "EOF detected. Re-initialize");
162-
/* remove old socket collector */
163-
ret = flb_input_collector_delete(ctx->coll_id, ins);
164-
close(ctx->fd);
235+
ret = reconnect_docker_sock_retry(ins, config, ctx);
165236
if (ret < 0) {
166-
flb_plg_error(ctx->ins, "failed to pause event");
167-
return -1;
168-
}
169-
ctx->fd = -1;
170-
/* create socket again */
171-
if (de_unix_create(ctx) < 0) {
172-
flb_plg_error(ctx->ins, "failed to re-initialize socket");
173-
return -1;
174-
}
175-
/* set event */
176-
ctx->coll_id = flb_input_set_collector_event(ins,
177-
in_de_collect,
178-
ctx->fd, config);
179-
if (ctx->coll_id < 0) {
180-
flb_plg_error(ctx->ins,
181-
"could not set collector for IN_DOCKER_EVENTS plugin");
182-
return -1;
183-
}
184-
ret = flb_input_collector_start(ctx->coll_id, ins);
185-
if (ret < 0) {
186-
flb_plg_error(ctx->ins,
187-
"could not start collector for IN_DOCKER_EVENTS plugin");
188-
return -1;
237+
return ret;
189238
}
190239
}
191240
else {
@@ -286,6 +335,16 @@ static struct flb_config_map config_map[] = {
286335
0, FLB_TRUE, offsetof(struct flb_in_de_config, key),
287336
"Set the key name to store unparsed Docker events"
288337
},
338+
{
339+
FLB_CONFIG_MAP_INT, "reconnect.retry_limits", "5",
340+
0, FLB_TRUE, offsetof(struct flb_in_de_config, reconnect_retry_limits),
341+
"Maximum number to retry to connect docker socket"
342+
},
343+
{
344+
FLB_CONFIG_MAP_INT, "reconnect.retry_interval", "1",
345+
0, FLB_TRUE, offsetof(struct flb_in_de_config, reconnect_retry_interval),
346+
"Retry interval to connect docker socket"
347+
},
289348
/* EOF */
290349
{0}
291350
};

plugins/in_docker_events/docker_events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct flb_in_de_config
3838
char *buf;
3939
size_t buf_size;
4040
flb_sds_t key;
41+
int reconnect_retry_limits;
42+
int reconnect_retry_interval;
4143
struct flb_parser *parser;
4244
struct flb_input_instance *ins; /* Input plugin instace */
4345

0 commit comments

Comments
 (0)