1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 02:00:54 +00:00

Merge pull request #4056 from gmelodie/reconn-bug-3971

Fix reconnection bug in logs/conn.go
This commit is contained in:
Ming Deng 2020-07-07 20:25:44 +08:00 committed by GitHub
commit 9dc660c1da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 4 deletions

View File

@ -63,7 +63,10 @@ func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
defer c.innerWriter.Close() defer c.innerWriter.Close()
} }
c.lg.writeln(when, msg) _, err := c.lg.writeln(when, msg)
if err != nil {
return err
}
return nil return nil
} }
@ -101,7 +104,6 @@ func (c *connWriter) connect() error {
func (c *connWriter) needToConnectOnMsg() bool { func (c *connWriter) needToConnectOnMsg() bool {
if c.Reconnect { if c.Reconnect {
c.Reconnect = false
return true return true
} }

View File

@ -15,11 +15,65 @@
package logs package logs
import ( import (
"net"
"os"
"testing" "testing"
) )
// ConnTCPListener takes a TCP listener and accepts n TCP connections
// Returns connections using connChan
func connTCPListener(t *testing.T, n int, ln net.Listener, connChan chan<- net.Conn) {
// Listen and accept n incoming connections
for i := 0; i < n; i++ {
conn, err := ln.Accept()
if err != nil {
t.Log("Error accepting connection: ", err.Error())
os.Exit(1)
}
// Send accepted connection to channel
connChan <- conn
}
ln.Close()
close(connChan)
}
func TestConn(t *testing.T) { func TestConn(t *testing.T) {
log := NewLogger(1000) log := NewLogger(1000)
log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`) log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`)
log.Informational("informational") log.Informational("informational")
} }
func TestReconnect(t *testing.T) {
// Setup connection listener
newConns := make(chan net.Conn)
connNum := 2
ln, err := net.Listen("tcp", ":6002")
if err != nil {
t.Log("Error listening:", err.Error())
os.Exit(1)
}
go connTCPListener(t, connNum, ln, newConns)
// Setup logger
log := NewLogger(1000)
log.SetPrefix("test")
log.SetLogger(AdapterConn, `{"net":"tcp","reconnect":true,"level":6,"addr":":6002"}`)
log.Informational("informational 1")
// Refuse first connection
first := <-newConns
first.Close()
// Send another log after conn closed
log.Informational("informational 2")
// Check if there was a second connection attempt
select {
case second := <-newConns:
second.Close()
default:
t.Error("Did not reconnect")
}
}

View File

@ -30,11 +30,12 @@ func newLogWriter(wr io.Writer) *logWriter {
return &logWriter{writer: wr} return &logWriter{writer: wr}
} }
func (lg *logWriter) writeln(when time.Time, msg string) { func (lg *logWriter) writeln(when time.Time, msg string) (int, error) {
lg.Lock() lg.Lock()
h, _, _ := formatTimeHeader(when) h, _, _ := formatTimeHeader(when)
lg.writer.Write(append(append(h, msg...), '\n')) n, err := lg.writer.Write(append(append(h, msg...), '\n'))
lg.Unlock() lg.Unlock()
return n, err
} }
const ( const (