Skip to content

Commit 9a8ab32

Browse files
cosmo0920edsiper
authored andcommitted
Make Input Cleanup Callback to be optional
This is because putting cleanup phase for C.style allocated heap memory is not ensuring for deallocation them. Signed-off-by: Hiroshi Hatake <hatake@calyptia.com>
1 parent 620befa commit 9a8ab32

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

examples/in_gdummy/README.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ Every output plugin go through four callbacks associated to different phases:
1212
| Registration | FLBPluginRegister() |
1313
| Initialization | FLBPluginInit() |
1414
| Input Callback | FLBPluginInputCallback() |
15-
| Input Cleanup Callback | FLBPluginInputCleanupCallback() |
1615
| Exit | FLBPluginExit() |
1716

17+
And _Input Cleanup Callback_ is optional.
18+
19+
This callback is called right after _Input Callback_.
20+
21+
| Plugin Phase | Callback |
22+
|------------------------|---------------------------------|
23+
| Input Cleanup Callback | FLBPluginInputCleanupCallback() |
24+
1825
## Plugin Registration
1926

2027
When Fluent Bit loads a Golang input plugin, it looks up and loads the registration
@@ -49,6 +56,8 @@ When Fluent Bit wants to collect logs from Golang input plugin, the input callba
4956

5057
The callback will send a raw buffer of msgpack data with it proper bytes length into Fluent Bit core.
5158

59+
`data` will collect the assigned pointer and this passing pointer should be allocated by C style allocation (C.calloc/C.malloc).
60+
5261
```go
5362
import "reflect" // Import reflect package.
5463

@@ -69,7 +78,7 @@ func makeSlice(p unsafe.Pointer, n int) *Slice {
6978
}
7079

7180
//export FLBPluginInputCallback
72-
func FLBPluginInputCallback(data **C.void, size *C.size_t) int {
81+
func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
7382
now := time.Now()
7483
// To handle nanosecond precision on Golang input plugin, you must wrap up time instances with input.FLBTime type.
7584
flb_time := input.FLBTime{now}
@@ -92,20 +101,14 @@ func FLBPluginInputCallback(data **C.void, size *C.size_t) int {
92101

93102
### Input Cleanup Callback
94103

95-
For cleaning up to be allocated resources, this callback will be triggered after _Input Callback_.
104+
For cleaning up some sort of allocated resources, this callback will be triggered after _Input Callback_.
96105

97-
This callback is mainly used for deallocating heap memories which are allocated by `C.malloc` or `C.calloc`.
106+
This callback is mainly used for cleaning up resources not for the first argument of input callback.
98107

99108
```go
100-
import "sync"
101-
102-
var barrior sync.Mutex
103-
104109
//export FLBPluginInputCleanupCallback
105110
func FLBPluginInputCleanupCallback(data unsafe.Pointer) int {
106-
barrior.Lock() // Guarding for deallocating region is needed for safety.
107-
C.free(unsafe.Pointer(data))
108-
barrior.Unlock()
111+
// Some sort of cleaning up resources
109112

110113
return input.FLB_OK
111114
}

examples/in_gdummy/in_gdummy.go

-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"time"
1010
"reflect"
11-
"sync"
1211
"unsafe"
1312

1413
"github.com/fluent/fluent-bit-go/input"
@@ -24,8 +23,6 @@ type c_slice_t struct {
2423
n int
2524
}
2625

27-
var barrior sync.Mutex
28-
2926
//export FLBPluginRegister
3027
func FLBPluginRegister(def unsafe.Pointer) int {
3128
return input.FLBPluginRegister(def, "gdummy", "dummy GO!")
@@ -86,10 +83,6 @@ func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
8683

8784
//export FLBPluginInputCleanupCallback
8885
func FLBPluginInputCleanupCallback(data unsafe.Pointer) int {
89-
barrior.Lock()
90-
C.free(unsafe.Pointer(data))
91-
barrior.Unlock()
92-
9386
return input.FLB_OK
9487
}
9588

0 commit comments

Comments
 (0)