-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternative implementation for closing a beat.Client
With the help of this change one can close a beat.Client instance indirectly, by signaling instead of calling Close(). One still should use `Close()` on shutdown, so to make sure that runner using the client for publishing blocks and keeps resources intact if WaitCloseTimeout has been configured. The interface CloseRef implements a subset of context.Context, which can be used to control shutdown. For example filebeat inputs normally run in a loop reading messages, transforming those into events, and publishing them to libbeat via a beat.Client instance. If the input accepts context.Context for cancellation, then the run loop follows this code pattern: func newInput(...) (*input, error) { // configure input // create context which is close/cancelled in `Close` method ctx, cancelFn := ... return &input{ ctx: ctx, cancel: cancelFn, ... }, nil } func (in *input) Start() { in.wg.Add(1) go func(){ defer in.wg.Done() in.Run() }() } func (in *input) Run() { reader := ... // init reader for collection the raw data defer reader.Close() outlet := connector.ConnectWith(beat.ClientConfig{ // underlying beat.Client will be closed if ctx is cancelled CloseRef: in.ctx, Processing: ... // give pipeline and ACKer a chance to ACK some inflight events during shutdown WaitClose: ... ACKEvents: func(private []interface{}) { for _, p := range private { reader.ACK(p) } }, }) // this blocks until all events have been acked or for a duration of WaitClose defer outlet.Close() for err := ctx.Err(); err == nil; err = ctx.Err() { // Read returns error if ctx is cancelled message, err := source.Read(in.ctx, ...) if err != nil { ... } // OnEvent blocks if queue is full, but unblocks if ctx is // cancelled. outlet.OnEvent(makeEvent(message)) } } func (in *input) Close() { // cancel context // -> reader or outleter unblock // -> run loop returns in.cancel() in.wg.Wait() // wait until run loop returned }
- Loading branch information
urso
committed
Jul 25, 2019
1 parent
d5b8171
commit 794f8f5
Showing
5 changed files
with
206 additions
and
47 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
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
Oops, something went wrong.