Skip to content

Commit f0ea9f5

Browse files
committed
fix nil conn panic
Signed-off-by: bnyu <xuyue1106@gmail.com>
1 parent a4c69ac commit f0ea9f5

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

fluent/fluent.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,11 @@ func e(x, y float64) int {
355355
}
356356

357357
func (f *Fluent) write(msg *msgToSend) error {
358-
358+
var c net.Conn
359359
for i := 0; i < f.Config.MaxRetry; i++ {
360-
360+
c = f.conn
361361
// Connect if needed
362-
if f.conn == nil {
362+
if c == nil {
363363
f.muconn.Lock()
364364
if f.conn == nil {
365365
err := f.connect()
@@ -373,28 +373,29 @@ func (f *Fluent) write(msg *msgToSend) error {
373373
continue
374374
}
375375
}
376+
c = f.conn
376377
f.muconn.Unlock()
377378
}
378379

379380
// We're connected, write msg
380381
t := f.Config.WriteTimeout
381382
if time.Duration(0) < t {
382-
f.conn.SetWriteDeadline(time.Now().Add(t))
383+
c.SetWriteDeadline(time.Now().Add(t))
383384
} else {
384-
f.conn.SetWriteDeadline(time.Time{})
385+
c.SetWriteDeadline(time.Time{})
385386
}
386-
_, err := f.conn.Write(msg.data)
387+
_, err := c.Write(msg.data)
387388
if err != nil {
388389
f.close()
389390
} else {
390391
// Acknowledgment check
391392
if msg.ack != "" {
392393
resp := &AckResp{}
393394
if f.Config.MarshalAsJSON {
394-
dec := json.NewDecoder(f.conn)
395+
dec := json.NewDecoder(c)
395396
err = dec.Decode(resp)
396397
} else {
397-
r := msgp.NewReader(f.conn)
398+
r := msgp.NewReader(c)
398399
err = resp.DecodeMsg(r)
399400
}
400401
if err != nil || resp.Ack != msg.ack {

0 commit comments

Comments
 (0)