github.com/Jeffail/benthos/v3@v3.65.0/lib/broker/greedy.go (about) 1 package broker 2 3 import ( 4 "time" 5 6 "github.com/Jeffail/benthos/v3/internal/component/output" 7 "github.com/Jeffail/benthos/v3/lib/types" 8 ) 9 10 //------------------------------------------------------------------------------ 11 12 // Greedy is a broker that implements types.Consumer and sends each message 13 // out to a single consumer chosen from an array in round-robin fashion. 14 // Consumers that apply backpressure will block all consumers. 15 type Greedy struct { 16 outputs []types.Output 17 } 18 19 // NewGreedy creates a new Greedy type by providing consumers. 20 func NewGreedy(outputs []types.Output) (*Greedy, error) { 21 return &Greedy{ 22 outputs: outputs, 23 }, nil 24 } 25 26 //------------------------------------------------------------------------------ 27 28 // Consume assigns a new messages channel for the broker to read. 29 func (g *Greedy) Consume(ts <-chan types.Transaction) error { 30 for _, out := range g.outputs { 31 if err := out.Consume(ts); err != nil { 32 return err 33 } 34 } 35 return nil 36 } 37 38 // Connected returns a boolean indicating whether this output is currently 39 // connected to its target. 40 func (g *Greedy) Connected() bool { 41 for _, out := range g.outputs { 42 if !out.Connected() { 43 return false 44 } 45 } 46 return true 47 } 48 49 // MaxInFlight returns the maximum number of in flight messages permitted by the 50 // output. This value can be used to determine a sensible value for parent 51 // outputs, but should not be relied upon as part of dispatcher logic. 52 func (g *Greedy) MaxInFlight() (m int, ok bool) { 53 for _, out := range g.outputs { 54 if mif, exists := output.GetMaxInFlight(out); exists && mif > m { 55 m = mif 56 ok = true 57 } 58 } 59 return 60 } 61 62 //------------------------------------------------------------------------------ 63 64 // CloseAsync shuts down the Greedy broker and stops processing requests. 65 func (g *Greedy) CloseAsync() { 66 for _, out := range g.outputs { 67 out.CloseAsync() 68 } 69 } 70 71 // WaitForClose blocks until the Greedy broker has closed down. 72 func (g *Greedy) WaitForClose(timeout time.Duration) error { 73 tStarted := time.Now() 74 remaining := timeout 75 for _, out := range g.outputs { 76 if err := out.WaitForClose(remaining); err != nil { 77 return err 78 } 79 remaining -= time.Since(tStarted) 80 if remaining <= 0 { 81 return types.ErrTimeout 82 } 83 } 84 return nil 85 } 86 87 //------------------------------------------------------------------------------