@@ -36,10 +36,6 @@ const (
3636 collectInterval = 1000 * time .Nanosecond
3737)
3838
39- const (
40- maxBufferedMessages = defaultMaxBufferedMessages
41- )
42-
4339// FLBPluginPreRegister -
4440//
4541//export FLBPluginPreRegister
@@ -138,17 +134,10 @@ func (p *pluginInstance) inputPreRun() error {
138134 return fmt .Errorf ("invalid plugin state %q" , p .state )
139135 }
140136
141- if p .runCtx != nil || p .runCancel != nil {
142- return fmt .Errorf ("plugin context already set" )
143- }
144- if p .msgChannel != nil {
145- return fmt .Errorf ("plugin channel already set" )
146- }
147-
148137 runCtx , runCancel := context .WithCancel (context .Background ())
149138 p .runCtx = runCtx
150139 p .runCancel = runCancel
151- p .msgChannel = make (chan Message , maxBufferedMessages )
140+ p .msgChannel = make (chan Message , p . maxBufferedMessages )
152141 p .state = instanceStateRunnable
153142
154143 go func () {
@@ -204,21 +193,24 @@ func FLBPluginInputPause() {
204193 panic (fmt .Errorf ("plugin not initialized" ))
205194 }
206195
207- if err := instance .pause (); err != nil {
196+ if err := instance .stop (); err != nil {
208197 panic (err )
209198 }
210199}
211200
212- func (p * pluginInstance ) pause () error {
201+ func (p * pluginInstance ) stop () error {
213202 p .mu .Lock ()
203+ p .runningWG .Wait ()
214204 defer p .mu .Unlock ()
215205
216- if p .state != instanceStateRunnable {
217- return fmt .Errorf ("cannot pause plugin in state %q" , p .state )
206+ if p .state != instanceStateRunnable && p . state != instanceStatePreExit {
207+ return fmt .Errorf ("cannot stop plugin in state %q" , p .state )
218208 }
219209
220- p .runCancel ()
221- close (p .msgChannel )
210+ if p .state == instanceStateRunnable {
211+ p .runCancel ()
212+ close (p .msgChannel )
213+ }
222214
223215 p .state = instanceStateInitialized
224216 p .runCtx = nil
@@ -286,10 +278,10 @@ func (p *pluginInstance) outputPreExit() error {
286278
287279 // Only output plugins have a pre-exit step
288280 if p .meta .output == nil {
289- return nil
281+ return fmt . Errorf ( "plugin is not an output plugin" )
290282 }
291283
292- if p .state != instanceStateInitialized {
284+ if p .state != instanceStateRunnable {
293285 return fmt .Errorf ("invalid plugin state %q" , p .state )
294286 }
295287
@@ -377,16 +369,18 @@ func FLBPluginInputCallback(data *unsafe.Pointer, csize *C.size_t) int {
377369}
378370
379371func (p * pluginInstance ) callback (data * unsafe.Pointer , csize * C.size_t ) int {
380- p .mu .RLock ()
381- defer p .mu .RUnlock ()
382-
372+ p .mu .Lock ()
383373 if p .state != instanceStateRunnable {
384374 return input .FLB_RETRY
385375 }
386376
377+ p .runningWG .Add (1 )
378+ defer p .runningWG .Done ()
379+ p .mu .Unlock ()
380+
387381 buf := bytes .NewBuffer ([]byte {})
388382
389- for loop := min (len (p .msgChannel ), maxBufferedMessages ); loop > 0 ; loop -- {
383+ for loop := min (len (p .msgChannel ), p . maxBufferedMessages ); loop > 0 ; loop -- {
390384 select {
391385 case msg , ok := <- p .msgChannel :
392386 if ! ok {
@@ -561,7 +555,7 @@ func FLBPluginExit() int {
561555 defer currInstanceMu .Unlock ()
562556
563557 if currInstance != nil {
564- currInstance .pause () //nolint:errcheck
558+ currInstance .stop () //nolint:errcheck
565559 currInstance = nil
566560 }
567561
0 commit comments