rs / zerolog

Zero Allocation JSON Logger
MIT License
10.62k stars 572 forks source link

Loop error when write log to kafka fail #513

Open vuhoanghiep1993 opened 1 year ago

vuhoanghiep1993 commented 1 year ago

I create a multi writer to file and to kafka as in project https://github.com/vasupal1996/go-app.git

The implement of kafka is : https://github.com/vasupal1996/go-app/blob/master/server/logger/kafkaLogger.go

Problem is when kafka server is down, the diode.poll() loop infinite so it write log a lot

func (kw *KafkaLogWriter) Write(p []byte) (n int, err error) {
    m := kafka.Message{
        Value: p,
    }
    err = kw.WriteMessages(context.Background(), m)
    if err != nil {
        log.Printf("failed to write log message to kafka topic:")
        return 0, err
    }
    return len(p), nil
}

I also want to ask how this infinite for break ,

func (dw Writer) poll() {
    defer close(dw.done)
    for {
        d := dw.d.Next() // -----> when debug, in normal case it out of for loop here, when kafka error, it loop infinite
        if d == nil {
            return
        }
        p := *(*[]byte)(d)
        dw.w.Write(p)

        // Proper usage of a sync.Pool requires each entry to have approximately
        // the same memory cost. To obtain this property when the stored type
        // contains a variably-sized buffer, we add a hard limit on the maximum buffer
        // to place back in the pool.
        //
        // See https://golang.org/issue/23199
        const maxSize = 1 << 16 // 64KiB
        if cap(p) <= maxSize {
            bufPool.Put(p[:0])
        }
    }
}