NewWriter creates a writer wrapping w with a many-to-one diode in order to never block log producers and drop events if the writer can't keep up with the flow of data. Use a diode.Writer when wr := diode.NewWriter(w, 1000, 0, func(missed int) { log.Printf("Dropped %d messages", missed) }) log := z
(w io.Writer, size int, pollInterval time.Duration, f Alerter)
| 49 | // |
| 50 | // See code.cloudfoundry.org/go-diodes for more info on diode. |
| 51 | func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Writer { |
| 52 | ctx, cancel := context.WithCancel(context.Background()) |
| 53 | dw := Writer{ |
| 54 | w: w, |
| 55 | c: cancel, |
| 56 | done: make(chan struct{}), |
| 57 | } |
| 58 | if f == nil { |
| 59 | f = func(int) {} |
| 60 | } |
| 61 | d := diodes.NewManyToOne(size, diodes.AlertFunc(f)) |
| 62 | if pollInterval > 0 { |
| 63 | dw.d = diodes.NewPoller(d, |
| 64 | diodes.WithPollingInterval(pollInterval), |
| 65 | diodes.WithPollingContext(ctx)) |
| 66 | } else { |
| 67 | dw.d = diodes.NewWaiter(d, |
| 68 | diodes.WithWaiterContext(ctx)) |
| 69 | } |
| 70 | go dw.poll() |
| 71 | return dw |
| 72 | } |
| 73 | |
| 74 | func (dw Writer) Write(p []byte) (n int, err error) { |
| 75 | // p is pooled in zerolog so we can't hold it passed this call, hence the |