@@ -55,10 +55,11 @@ static int cb_wasm_filter(const void *data, size_t bytes,
55
55
char * json_buf = NULL ;
56
56
size_t json_size ;
57
57
int root_type ;
58
- struct flb_wasm * wasm = NULL ;
58
+ /* Get the persistent WASM instance from the filter context. */
59
+ struct flb_filter_wasm * ctx = filter_context ;
60
+ struct flb_wasm * wasm = ctx -> wasm ;
59
61
size_t buf_size ;
60
62
61
- struct flb_filter_wasm * ctx = filter_context ;
62
63
struct flb_log_event_encoder log_encoder ;
63
64
struct flb_log_event_decoder log_decoder ;
64
65
struct flb_log_event log_event ;
@@ -67,6 +68,12 @@ static int cb_wasm_filter(const void *data, size_t bytes,
67
68
(void ) i_ins ;
68
69
(void ) config ;
69
70
71
+ /* Safeguard in case initialization failed. */
72
+ if (!wasm ) {
73
+ flb_plg_error (ctx -> ins , "WASM instance is not available, skipping." );
74
+ return FLB_FILTER_NOTOUCH ;
75
+ }
76
+
70
77
ret = flb_log_event_decoder_init (& log_decoder , (char * ) data , bytes );
71
78
72
79
if (ret != FLB_EVENT_DECODER_SUCCESS ) {
@@ -88,13 +95,6 @@ static int cb_wasm_filter(const void *data, size_t bytes,
88
95
return FLB_FILTER_NOTOUCH ;
89
96
}
90
97
91
- wasm = flb_wasm_instantiate (config , ctx -> wasm_path , ctx -> accessible_dir_list ,
92
- ctx -> wasm_conf );
93
- if (wasm == NULL ) {
94
- flb_plg_debug (ctx -> ins , "instantiate wasm [%s] failed" , ctx -> wasm_path );
95
- goto on_error ;
96
- }
97
-
98
98
while ((ret = flb_log_event_decoder_next (
99
99
& log_decoder ,
100
100
& log_event )) == FLB_EVENT_DECODER_SUCCESS ) {
@@ -117,8 +117,8 @@ static int cb_wasm_filter(const void *data, size_t bytes,
117
117
}
118
118
else {
119
119
flb_plg_error (ctx -> ins , "encode as JSON from msgpack is failed" );
120
-
121
- goto on_error ;
120
+ /* Go to on_error without destroying the persistent wasm instance */
121
+ goto on_error_without_wasm_destroy ;
122
122
}
123
123
break ;
124
124
case FLB_FILTER_WASM_FMT_MSGPACK :
@@ -127,8 +127,8 @@ static int cb_wasm_filter(const void *data, size_t bytes,
127
127
(void * * )& buf , & buf_size );
128
128
if (ret < 0 ) {
129
129
flb_plg_error (ctx -> ins , "format msgpack is failed" );
130
-
131
- goto on_error ;
130
+ /* Go to on_error without destroying the persistent wasm instance */
131
+ goto on_error_without_wasm_destroy ;
132
132
}
133
133
134
134
/* Execute WASM program */
@@ -227,9 +227,6 @@ static int cb_wasm_filter(const void *data, size_t bytes,
227
227
}
228
228
}
229
229
230
- /* Teardown WASM context */
231
- flb_wasm_destroy (wasm );
232
-
233
230
* out_buf = log_encoder .output_buffer ;
234
231
* out_bytes = log_encoder .output_length ;
235
232
@@ -240,14 +237,10 @@ static int cb_wasm_filter(const void *data, size_t bytes,
240
237
241
238
return FLB_FILTER_MODIFIED ;
242
239
243
- on_error :
240
+ /* A new error handler that doesn't destroy the persistent wasm instance */
241
+ on_error_without_wasm_destroy :
244
242
flb_log_event_decoder_destroy (& log_decoder );
245
243
flb_log_event_encoder_destroy (& log_encoder );
246
-
247
- if (wasm != NULL ) {
248
- flb_wasm_destroy (wasm );
249
- }
250
-
251
244
return FLB_FILTER_NOTOUCH ;
252
245
}
253
246
@@ -313,6 +306,7 @@ static int cb_wasm_pre_run(struct flb_filter_instance *f_ins,
313
306
/* Check accessibility for the wasm path */
314
307
ret = access (ctx -> wasm_path , R_OK );
315
308
if (ret != 0 ) {
309
+ flb_plg_error (f_ins , "cannot access wasm program at %s" , ctx -> wasm_path );
316
310
goto pre_run_error ;
317
311
}
318
312
@@ -341,6 +335,9 @@ static int cb_wasm_init(struct flb_filter_instance *f_ins,
341
335
if (!ctx ) {
342
336
return -1 ;
343
337
}
338
+ /* Initialize wasm pointer to NULL */
339
+ ctx -> wasm = NULL ;
340
+
344
341
345
342
/* Initialize exec config */
346
343
ret = filter_wasm_config_read (ctx , f_ins , config );
@@ -378,22 +375,50 @@ static int cb_wasm_init(struct flb_filter_instance *f_ins,
378
375
wasm_conf -> stack_size = ctx -> wasm_stack_size ;
379
376
}
380
377
381
- /* Set context */
378
+ /* Set context before instantiating */
382
379
flb_filter_set_context (f_ins , ctx );
380
+
381
+ /* Instantiate the WASM module once and store it in the context */
382
+ ctx -> wasm = flb_wasm_instantiate (config , ctx -> wasm_path ,
383
+ ctx -> accessible_dir_list ,
384
+ ctx -> wasm_conf );
385
+
386
+ if (ctx -> wasm == NULL ) {
387
+ flb_plg_error (ctx -> ins , "failed to instantiate wasm program: %s" ,
388
+ ctx -> wasm_path );
389
+ goto init_error ;
390
+ }
391
+
383
392
return 0 ;
384
393
385
394
init_error :
386
- delete_wasm_config (ctx );
387
-
395
+ if (ctx ) {
396
+ if (ctx -> wasm_conf ) {
397
+ flb_wasm_config_destroy (ctx -> wasm_conf );
398
+ ctx -> wasm_conf = NULL ;
399
+ }
400
+ delete_wasm_config (ctx );
401
+ }
402
+ flb_filter_set_context (f_ins , NULL );
388
403
return -1 ;
389
404
}
390
405
391
406
static int cb_wasm_exit (void * data , struct flb_config * config )
392
407
{
393
408
struct flb_filter_wasm * ctx = data ;
394
409
395
- flb_wasm_config_destroy (ctx -> wasm_conf );
396
- flb_wasm_destroy_all (config );
410
+ if (!ctx ) {
411
+ return 0 ;
412
+ }
413
+
414
+ /* Destroy the single, persistent WASM instance */
415
+ if (ctx -> wasm ) {
416
+ flb_wasm_destroy (ctx -> wasm );
417
+ }
418
+ /* Destroy the WASM configuration */
419
+ if (ctx -> wasm_conf ) {
420
+ flb_wasm_config_destroy (ctx -> wasm_conf );
421
+ }
397
422
delete_wasm_config (ctx );
398
423
return 0 ;
399
424
}
0 commit comments