@@ -24,6 +24,7 @@ package output
24
24
*/
25
25
import "C"
26
26
import (
27
+ "sync"
27
28
"unsafe"
28
29
)
29
30
@@ -69,22 +70,24 @@ func FLBPluginConfigKey(plugin unsafe.Pointer, key string) string {
69
70
return value
70
71
}
71
72
72
- var contexts = make ( map [ uintptr ] interface {})
73
+ var contexts sync. Map
73
74
75
+ // FLBPluginSetContext sets the context for plugin to ctx.
76
+ //
77
+ // Limit FLBPluginSetContext calls to once per plugin instance for best performance.
74
78
func FLBPluginSetContext (plugin unsafe.Pointer , ctx interface {}) {
75
79
// Allocate a byte of memory in the C heap and fill it with '\0',
76
80
// then convert its pointer into the C type void*, represented by unsafe.Pointer.
77
81
// The C string is not managed by Go GC, so it will not be freed automatically.
78
82
i := unsafe .Pointer (C .CString ("" ))
79
- // uintptr(i) produces the memory address of i, and malloc() guarantees uniqueness of it.
80
- //
81
- // FLBPluginSetContext must not be called concurrently with itself or FLBPluginGetContext.
82
- // A sync.RWMutex must be added if this might happen.
83
- contexts [uintptr (i )] = ctx
83
+ // uintptr(i) returns the memory address of i, which is unique in the heap.
84
+ contexts .Store (uintptr (i ), ctx )
84
85
p := (* FLBOutPlugin )(plugin )
85
86
p .context .remote_context = i
86
87
}
87
88
88
- func FLBPluginGetContext (i unsafe.Pointer ) interface {} {
89
- return contexts [uintptr (i )]
89
+ // FLBPluginGetContext reads the context associated with proxyCtx.
90
+ func FLBPluginGetContext (proxyCtx unsafe.Pointer ) interface {} {
91
+ v , _ := contexts .Load (uintptr (proxyCtx ))
92
+ return v
90
93
}
0 commit comments