Skip to content

Commit 620befa

Browse files
cosmo0920edsiper
authored andcommitted
input: Implement second callback to deallocate heap memory that is
allocated by C style (C.calloc) Signed-off-by: Hiroshi Hatake <hatake@calyptia.com>
1 parent 1aee0eb commit 620befa

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

examples/in_gdummy/README.md

+31-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ specified instance configuration to the input callback.
77

88
Every output plugin go through four callbacks associated to different phases:
99

10-
| Plugin Phase | Callback |
11-
|---------------------|----------------------------|
12-
| Registration | FLBPluginRegister() |
13-
| Initialization | FLBPluginInit() |
14-
| Input Callback | FLBPluginInputCallback() |
15-
| Exit | FLBPluginExit() |
10+
| Plugin Phase | Callback |
11+
|------------------------|---------------------------------|
12+
| Registration | FLBPluginRegister() |
13+
| Initialization | FLBPluginInit() |
14+
| Input Callback | FLBPluginInputCallback() |
15+
| Input Cleanup Callback | FLBPluginInputCleanupCallback() |
16+
| Exit | FLBPluginExit() |
1617

1718
## Plugin Registration
1819

@@ -58,10 +59,6 @@ func alloc(size int) unsafe.Pointer {
5859
func makeSlice(p unsafe.Pointer, n int) *Slice {
5960
data := &c_slice_t{p, n}
6061

61-
runtime.SetFinalizer(data, func(data *c_slice_t){
62-
C.free(data.p)
63-
})
64-
6562
s := &Slice{data: data}
6663
h := (*reflect.SliceHeader)(unsafe.Pointer(&s.Data))
6764
h.Data = uintptr(p)
@@ -93,6 +90,29 @@ func FLBPluginInputCallback(data **C.void, size *C.size_t) int {
9390
}
9491
```
9592

93+
### Input Cleanup Callback
94+
95+
For cleaning up to be allocated resources, this callback will be triggered after _Input Callback_.
96+
97+
This callback is mainly used for deallocating heap memories which are allocated by `C.malloc` or `C.calloc`.
98+
99+
```go
100+
import "sync"
101+
102+
var barrior sync.Mutex
103+
104+
//export FLBPluginInputCleanupCallback
105+
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()
109+
110+
return input.FLB_OK
111+
}
112+
```
113+
114+
#### Returning Status Values
115+
96116
> for more details about how to process the sending msgpack data into Fluent Bit core, please refer to the [in_gdummy.go](in_gdummy.go) file.
97117
98118
When done, there are three returning values available:
@@ -103,6 +123,7 @@ When done, there are three returning values available:
103123
| FLB\_ERROR | An internal error have ocurred, the plugin will not handle the set of records/data again. |
104124
| FLB\_RETRY | A recoverable error have ocurred, the engine can try to flush the records/data later.|
105125

126+
106127
## Plugin Exit
107128

108129
When Fluent Bit will stop using the instance of the plugin, it will trigger the exit callback. e.g:

examples/in_gdummy/in_gdummy.go

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

1414
"github.com/fluent/fluent-bit-go/input"
@@ -24,6 +24,8 @@ type c_slice_t struct {
2424
n int
2525
}
2626

27+
var barrior sync.Mutex
28+
2729
//export FLBPluginRegister
2830
func FLBPluginRegister(def unsafe.Pointer) int {
2931
return input.FLBPluginRegister(def, "gdummy", "dummy GO!")
@@ -44,11 +46,7 @@ func alloc(size int) unsafe.Pointer {
4446
}
4547

4648
func makeSlice(p unsafe.Pointer, n int) *Slice {
47-
data := &c_slice_t{p, n}
48-
49-
runtime.SetFinalizer(data, func(data *c_slice_t){
50-
C.free(data.p)
51-
})
49+
data := &c_slice_t{p: p, n: n}
5250

5351
s := &Slice{data: data}
5452
h := (*reflect.SliceHeader)(unsafe.Pointer(&s.Data))
@@ -86,6 +84,15 @@ func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
8684
return input.FLB_OK
8785
}
8886

87+
//export FLBPluginInputCleanupCallback
88+
func FLBPluginInputCleanupCallback(data unsafe.Pointer) int {
89+
barrior.Lock()
90+
C.free(unsafe.Pointer(data))
91+
barrior.Unlock()
92+
93+
return input.FLB_OK
94+
}
95+
8996
//export FLBPluginExit
9097
func FLBPluginExit() int {
9198
return input.FLB_OK

0 commit comments

Comments
 (0)