-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add filter call go lib filter
- Loading branch information
Showing
7 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(src | ||
cgolib.c | ||
) | ||
|
||
FLB_PLUGIN(filter_cgolib "${src}" "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
|
||
call golib from c | ||
|
||
example go src: | ||
|
||
```go | ||
|
||
package main | ||
|
||
import "C" | ||
import ( | ||
"fmt" | ||
) | ||
|
||
//export Golib_init | ||
func Golib_init(name, value []string) int { | ||
|
||
fmt.Printf("get name %#v. ", name) | ||
fmt.Printf("get value %#v. ", value) | ||
|
||
return 0 | ||
} | ||
|
||
//export Golib_filter | ||
func Golib_filter(srcName, srcValue []string) int { | ||
// go will return the result in the slice which called in. so can't append item that max slice's cap. | ||
// must use cgoAppend instead of append if you want append value. | ||
// use cgoSetSlice if you want change value of slice or just append. | ||
|
||
src := loadCallIn(srcName, srcValue) | ||
|
||
src["myadd"] ="teat" | ||
|
||
return unLoadCallIn(src, srcName, srcValue) | ||
} | ||
|
||
//export Golib_exit | ||
func Golib_exit() int { | ||
|
||
fmt.Println("go exit") | ||
|
||
return 0 | ||
} | ||
|
||
func loadCallIn(name, value[]string) map[string]string { | ||
res := make(map[string]string) | ||
for idx, n := range name { | ||
res[n] = value[idx] | ||
} | ||
return res | ||
} | ||
|
||
func unLoadCallIn(src map[string]string, name, value []string) int { | ||
index := 0 | ||
for k, v := range src { | ||
name = cgoSetSlice(name, index, k) | ||
value = cgoSetSlice(value, index, v) | ||
index++ | ||
} | ||
fmt.Println("unload ok: ", name, value) | ||
return index; | ||
} | ||
|
||
|
||
// can't append parameters that will over slice's cap. | ||
// if over slice's cap, then go will malloc new memory, the c can't get the results. | ||
func cgoAppend(src []string, parameters... string) []string { | ||
if cap(src) - len(src) < len(parameters) { | ||
fmt.Println(" cann't set parameters. slice cap is full. ", len(src) , cap(src), len(parameters)) | ||
return src | ||
} | ||
src = append(src, parameters...) | ||
return src | ||
} | ||
|
||
func cgoSetSlice(src []string, index int, value string) []string { | ||
if index > cap(src) { | ||
fmt.Println(" cann't set parameters. index overflow. ", index, cap(src)) | ||
return src | ||
} | ||
|
||
if index >= len(src) { | ||
src = append(src, value) | ||
} else { | ||
src[index] = value | ||
} | ||
|
||
return src | ||
} | ||
|
||
func main() {} | ||
|
||
``` | ||
|
||
build | ||
|
||
```shell | ||
go build -o cgolib.so -buildmode=c-shared cgolib.go | ||
``` | ||
|
||
config | ||
|
||
```ini | ||
[FILTER] | ||
Name cgolib | ||
Match * | ||
golib_so /usr/local/libs/cgolib.so | ||
url http://12321 | ||
url2 http://12321 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Created by "go tool cgo" - DO NOT EDIT. */ | ||
|
||
/* package command-line-arguments */ | ||
|
||
|
||
#line 1 "cgo-builtin-prolog" | ||
|
||
#include <stddef.h> /* for ptrdiff_t below */ | ||
|
||
#ifndef GO_CGO_EXPORT_PROLOGUE_H | ||
#define GO_CGO_EXPORT_PROLOGUE_H | ||
|
||
typedef struct { const char *p; ptrdiff_t n; } _GoString_; | ||
|
||
#endif | ||
|
||
/* Start of boilerplate cgo prologue. */ | ||
#line 1 "cgo-gcc-export-header-prolog" | ||
|
||
#ifndef GO_CGO_PROLOGUE_H | ||
#define GO_CGO_PROLOGUE_H | ||
|
||
typedef signed char GoInt8; | ||
typedef unsigned char GoUint8; | ||
typedef short GoInt16; | ||
typedef unsigned short GoUint16; | ||
typedef int GoInt32; | ||
typedef unsigned int GoUint32; | ||
typedef long long GoInt64; | ||
typedef unsigned long long GoUint64; | ||
typedef GoInt64 GoInt; | ||
typedef GoUint64 GoUint; | ||
typedef __SIZE_TYPE__ GoUintptr; | ||
typedef float GoFloat32; | ||
typedef double GoFloat64; | ||
typedef float _Complex GoComplex64; | ||
typedef double _Complex GoComplex128; | ||
|
||
/* | ||
static assertion to make sure the file is being used on architecture | ||
at least with matching size of GoInt. | ||
*/ | ||
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; | ||
|
||
typedef _GoString_ GoString; | ||
typedef void *GoMap; | ||
typedef void *GoChan; | ||
typedef struct { void *t; void *v; } GoInterface; | ||
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; | ||
|
||
#endif |
Oops, something went wrong.