Skip to content

Commit f4e0625

Browse files
cosmo0920edsiper
authored andcommitted
Use C.CBytes at data variable
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 9a8ab32 commit f4e0625

File tree

2 files changed

+7
-55
lines changed

2 files changed

+7
-55
lines changed

examples/in_gdummy/README.md

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,9 @@ When Fluent Bit wants to collect logs from Golang input plugin, the input callba
5656

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

59-
`data` will collect the assigned pointer and this passing pointer should be allocated by C style allocation (C.calloc/C.malloc).
59+
`data` will collect the assigned pointer and this passing pointer should be allocated by C style allocation (C.calloc/C.malloc) or C.CBytes that is using C style allocation internally.
6060

6161
```go
62-
import "reflect" // Import reflect package.
63-
64-
func alloc(size int) unsafe.Pointer {
65-
return C.calloc(C.size_t(size), 1)
66-
}
67-
68-
func makeSlice(p unsafe.Pointer, n int) *Slice {
69-
data := &c_slice_t{p, n}
70-
71-
s := &Slice{data: data}
72-
h := (*reflect.SliceHeader)(unsafe.Pointer(&s.Data))
73-
h.Data = uintptr(p)
74-
h.Len = n
75-
h.Cap = n
76-
77-
return s
78-
}
79-
8062
//export FLBPluginInputCallback
8163
func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
8264
now := time.Now()
@@ -90,11 +72,11 @@ func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
9072
// It needs to Wait for some period on Golang input plugin side, until the new records are emitted.
9173

9274
length := len(packed)
93-
p := alloc(length)
94-
s := makeSlice(p, length)
95-
copy(s.Data, packed)
96-
*data = unsafe.Pointer(&s.Data[0])
97-
*size = C.size_t(len(packed))
75+
*data = C.CBytes(packed)
76+
*size = C.size_t(length)
77+
// For emitting interval adjustment.
78+
time.Sleep(1000 * time.Millisecond)
79+
9880
return input.FLB_OK
9981
}
10082
```

examples/in_gdummy/in_gdummy.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,11 @@ import "C"
77
import (
88
"fmt"
99
"time"
10-
"reflect"
1110
"unsafe"
1211

1312
"github.com/fluent/fluent-bit-go/input"
1413
)
1514

16-
type Slice struct {
17-
Data []byte
18-
data *c_slice_t
19-
}
20-
21-
type c_slice_t struct {
22-
p unsafe.Pointer
23-
n int
24-
}
25-
2615
//export FLBPluginRegister
2716
func FLBPluginRegister(def unsafe.Pointer) int {
2817
return input.FLBPluginRegister(def, "gdummy", "dummy GO!")
@@ -38,22 +27,6 @@ func FLBPluginInit(plugin unsafe.Pointer) int {
3827
return input.FLB_OK
3928
}
4029

41-
func alloc(size int) unsafe.Pointer {
42-
return C.calloc(C.size_t(size), 1)
43-
}
44-
45-
func makeSlice(p unsafe.Pointer, n int) *Slice {
46-
data := &c_slice_t{p: p, n: n}
47-
48-
s := &Slice{data: data}
49-
h := (*reflect.SliceHeader)(unsafe.Pointer(&s.Data))
50-
h.Data = uintptr(p)
51-
h.Len = n
52-
h.Cap = n
53-
54-
return s
55-
}
56-
5730
//export FLBPluginInputCallback
5831
func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
5932
now := time.Now()
@@ -70,10 +43,7 @@ func FLBPluginInputCallback(data *unsafe.Pointer, size *C.size_t) int {
7043
}
7144

7245
length := len(packed)
73-
p := alloc(length)
74-
s := makeSlice(p, length)
75-
copy(s.Data, packed)
76-
*data = unsafe.Pointer(&s.Data[0])
46+
*data = C.CBytes(packed)
7747
*size = C.size_t(length)
7848
// For emitting interval adjustment.
7949
time.Sleep(1000 * time.Millisecond)

0 commit comments

Comments
 (0)