diff --git a/go.mod b/go.mod index 0766634c..c9ab2a83 100644 --- a/go.mod +++ b/go.mod @@ -5,13 +5,15 @@ go 1.18 require ( github.com/fergusstrange/embedded-postgres v1.19.0 github.com/panjf2000/gnet/v2 v2.1.2 - github.com/sirupsen/logrus v1.9.0 + github.com/rs/zerolog v1.28.0 github.com/stretchr/testify v1.8.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/lib/pq v1.10.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect go.uber.org/atomic v1.10.0 // indirect diff --git a/go.sum b/go.sum index b8fd5206..4a81bc31 100644 --- a/go.sum +++ b/go.sum @@ -2,11 +2,13 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fergusstrange/embedded-postgres v1.19.0 h1:NqDufJHeA03U7biULlPHZ0pZ10/mDOMKPILEpT50Fyk= github.com/fergusstrange/embedded-postgres v1.19.0/go.mod h1:0B+3bPsMvcNgR9nN+bdM2x9YaNYDnf3ksUqYp1OAub0= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -15,16 +17,24 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/panjf2000/ants/v2 v2.4.8 h1:JgTbolX6K6RreZ4+bfctI0Ifs+3mrE5BIHudQxUDQ9k= github.com/panjf2000/ants/v2 v2.4.8/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A= github.com/panjf2000/gnet/v2 v2.1.2 h1:WJ/PkbfV6G0wcGOng2pyCwv8oadKiqtP8p+38smN7ao= github.com/panjf2000/gnet/v2 v2.1.2/go.mod h1:unWr2B4jF0DQPJH3GsXBGQiDcAamM6+Pf5FiK705kc4= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= +github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -69,8 +79,10 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/logging/logger.go b/logging/logger.go new file mode 100644 index 00000000..81a71b2c --- /dev/null +++ b/logging/logger.go @@ -0,0 +1,40 @@ +package logging + +import ( + "io" + "os" + + "github.com/rs/zerolog" +) + +func NewLogger( + writer io.Writer, + timeFieldFormat string, + level zerolog.Level, + timestamp bool, +) zerolog.Logger { + // Create a new logger + consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: timeFieldFormat} + + if writer == nil { + // Default to stdout + writer = consoleWriter + } + + if timeFieldFormat == "" { + timeFieldFormat = zerolog.TimeFieldFormat + } + + zerolog.SetGlobalLevel(level) + zerolog.TimeFieldFormat = timeFieldFormat + + // Create a new logger + logger := zerolog.New(writer) + if timestamp { + logger = logger.With().Timestamp().Logger() + } + + logger.Debug().Msg("Created a new logger") + + return logger +} diff --git a/main.go b/main.go index 0a535f57..f025d827 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,10 @@ import ( "syscall" "time" + "github.com/gatewayd-io/gatewayd/logging" "github.com/gatewayd-io/gatewayd/network" "github.com/panjf2000/gnet/v2" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) const ( @@ -17,23 +18,26 @@ const ( //nolint:funlen func main() { + // Create a logger + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.InfoLevel, true) + // Create a pool - pool := network.NewPool() + pool := network.NewPool(logger) // Add a client to the pool for i := 0; i < network.DefaultPoolSize; i++ { - client := network.NewClient("tcp", "localhost:5432", network.DefaultBufferSize) + client := network.NewClient("tcp", "localhost:5432", network.DefaultBufferSize, logger) if client != nil { if err := pool.Put(client); err != nil { - logrus.Panic(err) + logger.Panic().Err(err).Msg("Failed to add client to pool") } } } // Verify that the pool is properly populated - logrus.Infof("There are %d clients in the pool", len(pool.ClientIDs())) + logger.Debug().Msgf("There are %d clients in the pool", len(pool.ClientIDs())) if len(pool.ClientIDs()) != network.DefaultPoolSize { - logrus.Error( + logger.Error().Msg( "The pool size is incorrect, either because " + "the clients are cannot connect (no network connectivity) " + "or the server is not running. Exiting...") @@ -45,7 +49,7 @@ func main() { Network: "tcp", Address: "localhost:5432", ReceiveBufferSize: network.DefaultBufferSize, - }) + }, logger) // Create a server server := network.NewServer( @@ -89,6 +93,7 @@ func main() { nil, nil, proxy, + logger, ) // Shutdown the server gracefully @@ -117,6 +122,6 @@ func main() { // Run the server if err := server.Run(); err != nil { - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to start server") } } diff --git a/network/client.go b/network/client.go index 4a76657a..1ac2200d 100644 --- a/network/client.go +++ b/network/client.go @@ -4,7 +4,7 @@ import ( "fmt" "net" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) const ( @@ -14,6 +14,8 @@ const ( type Client struct { net.Conn + logger zerolog.Logger + ID string ReceiveBufferSize int Network string // tcp/udp/unix @@ -23,13 +25,15 @@ type Client struct { // TODO: implement a better connection management algorithm -func NewClient(network, address string, receiveBufferSize int) *Client { +func NewClient(network, address string, receiveBufferSize int, logger zerolog.Logger) *Client { var client Client + client.logger = logger + // Try to resolve the address and log an error if it can't be resolved - addr, err := Resolve(network, address) + addr, err := Resolve(network, address, logger) if err != nil { - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to resolve address") } // Create a resolved client @@ -49,7 +53,7 @@ func NewClient(network, address string, receiveBufferSize int) *Client { // Create a new connection conn, err := net.Dial(client.Network, client.Address) if err != nil { - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to create a new connection") return nil } @@ -57,19 +61,18 @@ func NewClient(network, address string, receiveBufferSize int) *Client { if client.ReceiveBufferSize == 0 { client.ReceiveBufferSize = DefaultBufferSize } - logrus.Debugf("New client created: %s", client.Address) - client.ID = GetID(conn.LocalAddr().Network(), conn.LocalAddr().String(), DefaultSeed) + logger.Debug().Msgf("New client created: %s", client.Address) + client.ID = GetID(conn.LocalAddr().Network(), conn.LocalAddr().String(), DefaultSeed, logger) return &client } func (c *Client) Send(data []byte) error { if _, err := c.Write(data); err != nil { - logrus.Errorf("Couldn't send data to the server: %s", err) + c.logger.Error().Err(err).Msgf("Couldn't send data to the server: %s", err) return fmt.Errorf("couldn't send data to the server: %w", err) } - logrus.Debugf("Sent %d bytes to %s", len(data), c.Address) - // logrus.Infof("Sent data: %s", data) + c.logger.Debug().Msgf("Sent %d bytes to %s", len(data), c.Address) return nil } @@ -77,16 +80,15 @@ func (c *Client) Receive() (int, []byte, error) { buf := make([]byte, c.ReceiveBufferSize) read, err := c.Read(buf) if err != nil { - logrus.Errorf("Couldn't receive data from the server: %s", err) + c.logger.Error().Err(err).Msgf("Couldn't receive data from the server: %s", err) return 0, nil, fmt.Errorf("couldn't receive data from the server: %w", err) } - logrus.Debugf("Received %d bytes from %s", read, c.Address) - // logrus.Infof("Received data: %s", buf[:read]) + c.logger.Debug().Msgf("Received %d bytes from %s", read, c.Address) return read, buf, nil } func (c *Client) Close() { - logrus.Debugf("Closing connection to %s", c.Address) + c.logger.Debug().Msgf("Closing connection to %s", c.Address) if c.Conn != nil { c.Conn.Close() } diff --git a/network/client_test.go b/network/client_test.go index fcc4bbe6..0f47c254 100644 --- a/network/client_test.go +++ b/network/client_test.go @@ -4,6 +4,8 @@ import ( "testing" embeddedpostgres "github.com/fergusstrange/embedded-postgres" + "github.com/gatewayd-io/gatewayd/logging" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) @@ -19,7 +21,9 @@ func TestNewClient(t *testing.T) { } }() - client := NewClient("tcp", "localhost:5432", DefaultBufferSize) + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + client := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) defer client.Close() assert.Equal(t, "tcp", client.Network) @@ -41,7 +45,9 @@ func TestSend(t *testing.T) { } }() - client := NewClient("tcp", "localhost:5432", DefaultBufferSize) + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + client := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) defer client.Close() assert.NotNil(t, client) @@ -61,7 +67,9 @@ func TestReceive(t *testing.T) { } }() - client := NewClient("tcp", "localhost:5432", DefaultBufferSize) + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + client := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) defer client.Close() assert.NotNil(t, client) @@ -91,7 +99,9 @@ func TestClose(t *testing.T) { } }() - client := NewClient("tcp", "localhost:5432", DefaultBufferSize) + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + client := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NotNil(t, client) client.Close() assert.Equal(t, "", client.ID) diff --git a/network/pool.go b/network/pool.go index 3b81c061..cf5be82c 100644 --- a/network/pool.go +++ b/network/pool.go @@ -3,7 +3,7 @@ package network import ( "sync" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) type Pool interface { @@ -18,7 +18,8 @@ type Pool interface { } type PoolImpl struct { - pool sync.Map + pool sync.Map + logger zerolog.Logger } var _ Pool = &PoolImpl{} @@ -28,7 +29,7 @@ func (p *PoolImpl) ForEach(callback func(client *Client) error) { if c, ok := value.(*Client); ok { err := callback(c) if err != nil { - logrus.Errorf("an error occurred running the callback: %v", err) + p.logger.Debug().Err(err).Msg("an error occurred running the callback") } return true } @@ -55,7 +56,7 @@ func (p *PoolImpl) ClientIDs() []string { func (p *PoolImpl) Put(client *Client) error { p.pool.Store(client.ID, client) - logrus.Debugf("Client %s has been put on the pool", client.ID) + p.logger.Debug().Msgf("Client %s has been put on the pool", client.ID) return nil } @@ -63,7 +64,7 @@ func (p *PoolImpl) Put(client *Client) error { func (p *PoolImpl) Pop(id string) *Client { if client, ok := p.pool.Load(id); ok { p.pool.Delete(id) - logrus.Debugf("Client %s has been popped from the pool", id) + p.logger.Debug().Msgf("Client %s has been popped from the pool", id) if c, ok := client.(*Client); ok { return c } @@ -104,6 +105,6 @@ func (p *PoolImpl) Shutdown() { p.pool = sync.Map{} } -func NewPool() *PoolImpl { - return &PoolImpl{pool: sync.Map{}} +func NewPool(logger zerolog.Logger) *PoolImpl { + return &PoolImpl{pool: sync.Map{}, logger: logger} } diff --git a/network/pool_test.go b/network/pool_test.go index f35a4fdd..e70f6a57 100644 --- a/network/pool_test.go +++ b/network/pool_test.go @@ -4,11 +4,14 @@ import ( "testing" embeddedpostgres "github.com/fergusstrange/embedded-postgres" + "github.com/gatewayd-io/gatewayd/logging" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) func TestNewPool(t *testing.T) { - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) @@ -27,14 +30,16 @@ func TestPool_Put(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize))) + assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize, logger))) assert.Equal(t, 1, pool.Size()) - assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize))) + assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize, logger))) assert.Equal(t, 2, pool.Size()) } @@ -50,15 +55,17 @@ func TestPool_Pop(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client1)) assert.Equal(t, 1, pool.Size()) - client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client2)) assert.Equal(t, 2, pool.Size()) client := pool.Pop(client1.ID) @@ -81,14 +88,16 @@ func TestPool_Close(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client1)) assert.Equal(t, 1, pool.Size()) - client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client2)) assert.Equal(t, 2, pool.Size()) err := pool.Close() @@ -108,15 +117,17 @@ func TestPool_Shutdown(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client1)) assert.Equal(t, 1, pool.Size()) - client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client2)) assert.Equal(t, 2, pool.Size()) pool.Shutdown() @@ -135,15 +146,17 @@ func TestPool_ForEach(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client1)) assert.Equal(t, 1, pool.Size()) - client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client2)) assert.Equal(t, 2, pool.Size()) pool.ForEach(func(client *Client) error { @@ -164,15 +177,17 @@ func TestPool_ClientIDs(t *testing.T) { } }() - pool := NewPool() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + + pool := NewPool(logger) defer pool.Close() assert.NotNil(t, pool) assert.NotNil(t, pool.Pool()) assert.Equal(t, 0, pool.Size()) - client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client1 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client1)) assert.Equal(t, 1, pool.Size()) - client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize) + client2 := NewClient("tcp", "localhost:5432", DefaultBufferSize, logger) assert.NoError(t, pool.Put(client2)) assert.Equal(t, 2, pool.Size()) ids := pool.ClientIDs() diff --git a/network/proxy.go b/network/proxy.go index 260cfa80..5c9bbc2d 100644 --- a/network/proxy.go +++ b/network/proxy.go @@ -7,7 +7,7 @@ import ( "sync" "github.com/panjf2000/gnet/v2" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) type Traffic func(gconn gnet.Conn, cl *Client, buf []byte, err error) error @@ -24,6 +24,7 @@ type Proxy interface { type ProxyImpl struct { pool Pool clients sync.Map + logger zerolog.Logger Elastic bool ReuseElasticClients bool @@ -35,10 +36,11 @@ type ProxyImpl struct { var _ Proxy = &ProxyImpl{} func NewProxy( - pool Pool, elastic, reuseElasticClients bool, clientConfig *Client, + pool Pool, elastic, reuseElasticClients bool, clientConfig *Client, logger zerolog.Logger, ) *ProxyImpl { proxy := ProxyImpl{ clients: sync.Map{}, + logger: logger, Elastic: elastic, ReuseElasticClients: reuseElasticClients, ClientConfig: clientConfig, @@ -47,7 +49,7 @@ func NewProxy( if pool != nil { proxy.pool = pool } else { - proxy.pool = NewPool() + proxy.pool = NewPool(logger) } return &proxy @@ -65,26 +67,27 @@ func (pr *ProxyImpl) Connect(gconn gnet.Conn) error { pr.ClientConfig.Network, pr.ClientConfig.Address, pr.ClientConfig.ReceiveBufferSize, + pr.logger, ) - logrus.Debugf("Reused the client %s by putting it back in the pool", client.ID) + pr.logger.Debug().Msgf("Reused the client %s by putting it back in the pool", client.ID) } else { return ErrPoolExhausted } } else { // Get a client from the pool - logrus.Debugf("Available clients: %v", len(clientIDs)) + pr.logger.Debug().Msgf("Available clients: %v", len(clientIDs)) client = pr.pool.Pop(clientIDs[0]) } if client.ID != "" { pr.clients.Store(gconn, client) - logrus.Debugf("Client %s has been assigned to %s", client.ID, gconn.RemoteAddr().String()) + pr.logger.Debug().Msgf("Client %s has been assigned to %s", client.ID, gconn.RemoteAddr().String()) } else { return ErrClientNotConnected } - logrus.Debugf("[C] There are %d clients in the pool", len(pr.pool.ClientIDs())) - logrus.Debugf("[C] There are %d clients in use", pr.Size()) + pr.logger.Debug().Msgf("[C] There are %d clients in the pool", len(pr.pool.ClientIDs())) + pr.logger.Debug().Msgf("[C] There are %d clients in use", pr.Size()) return nil } @@ -104,6 +107,7 @@ func (pr *ProxyImpl) Disconnect(gconn gnet.Conn) error { client = pr.Reconnect(client) if client != nil && client.ID != "" { if err := pr.pool.Put(client); err != nil { + pr.logger.Error().Err(err).Msgf("Failed to put the client %s back in the pool", client.ID) return fmt.Errorf("failed to put the client back in the pool: %w", err) } } @@ -111,8 +115,8 @@ func (pr *ProxyImpl) Disconnect(gconn gnet.Conn) error { client.Close() } - logrus.Debugf("[D] There are %d clients in the pool", len(pr.pool.ClientIDs())) - logrus.Debugf("[D] There are %d clients in use", pr.Size()) + pr.logger.Debug().Msgf("[D] There are %d clients in the pool", len(pr.pool.ClientIDs())) + pr.logger.Debug().Msgf("[D] There are %d clients in use", pr.Size()) return nil } @@ -137,16 +141,16 @@ func (pr *ProxyImpl) PassThrough(gconn gnet.Conn, onIncomingTraffic, onOutgoingT // buf contains the data from the client (query) buf, err := gconn.Next(-1) if err != nil { - logrus.Errorf("Error reading from client: %v", err) + pr.logger.Error().Err(err).Msgf("Error reading from client: %v", err) } if err = onIncomingTraffic(gconn, client, buf, err); err != nil { - logrus.Errorf("Error processing data from client: %v", err) + pr.logger.Error().Err(err).Msgf("Error processing data from client: %v", err) } // TODO: parse the buffer and send the response or error // TODO: This is a very basic implementation of the gateway // and it is synchronous. I should make it asynchronous. - logrus.Debugf("Received %d bytes from %s", len(buf), gconn.RemoteAddr().String()) + pr.logger.Debug().Msgf("Received %d bytes from %s", len(buf), gconn.RemoteAddr().String()) // Send the query to the server err = client.Send(buf) @@ -157,12 +161,12 @@ func (pr *ProxyImpl) PassThrough(gconn gnet.Conn, onIncomingTraffic, onOutgoingT // Receive the response from the server size, response, err := client.Receive() if err := onOutgoingTraffic(gconn, client, response[:size], err); err != nil { - logrus.Errorf("Error processing data from server: %s", err) + pr.logger.Error().Err(err).Msgf("Error processing data from server: %s", err) } if err != nil && errors.Is(err, io.EOF) { // The server has closed the connection - logrus.Error("The client is not connected to the server anymore") + pr.logger.Error().Err(err).Msg("The client is not connected to the server anymore") // Either the client is not connected to the server anymore or // server forceful closed the connection // Reconnect the client @@ -176,7 +180,7 @@ func (pr *ProxyImpl) PassThrough(gconn gnet.Conn, onIncomingTraffic, onOutgoingT // Write the error to the client _, err := gconn.Write(response[:size]) if err != nil { - logrus.Errorf("Error writing the error to client: %v", err) + pr.logger.Error().Err(err).Msgf("Error writing the error to client: %v", err) } return fmt.Errorf("error receiving data from server: %w", err) } @@ -184,7 +188,7 @@ func (pr *ProxyImpl) PassThrough(gconn gnet.Conn, onIncomingTraffic, onOutgoingT // Write the response to the incoming connection _, err = gconn.Write(response[:size]) if err != nil { - logrus.Errorf("Error writing to client: %v", err) + pr.logger.Error().Err(err).Msgf("Error writing to client: %v", err) } return nil @@ -199,19 +203,20 @@ func (pr *ProxyImpl) Reconnect(cl *Client) *Client { pr.ClientConfig.Network, pr.ClientConfig.Address, pr.ClientConfig.ReceiveBufferSize, + pr.logger, ) } func (pr *ProxyImpl) Shutdown() { pr.pool.Shutdown() - logrus.Debug("All busy client connections have been closed") + pr.logger.Debug().Msg("All busy client connections have been closed") availableClients := pr.pool.ClientIDs() for _, clientID := range availableClients { client := pr.pool.Pop(clientID) client.Close() } - logrus.Debug("All available client connections have been closed") + pr.logger.Debug().Msg("All available client connections have been closed") } func (pr *ProxyImpl) Size() int { diff --git a/network/proxy_test.go b/network/proxy_test.go index 485ec29a..1e3549ee 100644 --- a/network/proxy_test.go +++ b/network/proxy_test.go @@ -4,6 +4,8 @@ import ( "testing" embeddedpostgres "github.com/fergusstrange/embedded-postgres" + "github.com/gatewayd-io/gatewayd/logging" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) @@ -19,12 +21,14 @@ func TestNewProxy(t *testing.T) { } }() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + // Create a connection pool - pool := NewPool() - assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize))) + pool := NewPool(logger) + assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize, logger))) // Create a proxy with a fixed buffer pool - proxy := NewProxy(pool, false, false, nil) + proxy := NewProxy(pool, false, false, nil, logger) assert.NotNil(t, proxy) assert.Equal(t, 0, proxy.Size(), "Proxy should have no connected clients") @@ -37,15 +41,17 @@ func TestNewProxy(t *testing.T) { } func TestNewProxyElastic(t *testing.T) { + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + // Create a connection pool - pool := NewPool() + pool := NewPool(logger) // Create a proxy with an elastic buffer pool proxy := NewProxy(pool, true, false, &Client{ Network: "tcp", Address: "localhost:5432", ReceiveBufferSize: DefaultBufferSize, - }) + }, logger) assert.NotNil(t, proxy) assert.Equal(t, 0, proxy.Size()) @@ -60,15 +66,17 @@ func TestNewProxyElastic(t *testing.T) { } func TestNewProxyElasticReuse(t *testing.T) { + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + // Create a connection pool - pool := NewPool() + pool := NewPool(logger) // Create a proxy with an elastic buffer pool proxy := NewProxy(pool, true, true, &Client{ Network: "tcp", Address: "localhost:5432", ReceiveBufferSize: DefaultBufferSize, - }) + }, logger) assert.NotNil(t, proxy) assert.Equal(t, 0, proxy.Size()) diff --git a/network/server.go b/network/server.go index cc0ffd8e..373716ab 100644 --- a/network/server.go +++ b/network/server.go @@ -6,7 +6,7 @@ import ( "time" "github.com/panjf2000/gnet/v2" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) type Status string @@ -24,6 +24,7 @@ type Server struct { gnet.BuiltinEventEngine engine gnet.Engine proxy Proxy + logger zerolog.Logger Network string // tcp/udp/unix Address string @@ -43,9 +44,6 @@ type Server struct { func (s *Server) OnBoot(engine gnet.Engine) gnet.Action { s.engine = engine - // Create a proxy with a fixed/elastic buffer pool - // s.proxy = NewProxy(s.PoolSize, s.BufferSize, s.ElasticPool, s.ReuseElasticClients) - // Set the status to running s.Status = Running @@ -53,15 +51,17 @@ func (s *Server) OnBoot(engine gnet.Engine) gnet.Action { } func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) { - logrus.Debugf("GatewayD is opening a connection from %s", gconn.RemoteAddr().String()) + s.logger.Debug().Msgf("GatewayD is opening a connection from %s", gconn.RemoteAddr().String()) + if uint64(s.engine.CountConnections()) >= s.SoftLimit { - logrus.Warn("Soft limit reached") + s.logger.Warn().Msg("Soft limit reached") } + if uint64(s.engine.CountConnections()) >= s.HardLimit { - logrus.Error("Hard limit reached") + s.logger.Error().Msg("Hard limit reached") _, err := gconn.Write([]byte("Hard limit reached\n")) if err != nil { - logrus.Error(err) + s.logger.Error().Err(err).Msg("Failed to write to connection") } gconn.Close() return nil, gnet.Close @@ -75,10 +75,10 @@ func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) { } func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action { - logrus.Debugf("GatewayD is closing a connection from %s", gconn.RemoteAddr().String()) + s.logger.Debug().Msgf("GatewayD is closing a connection from %s", gconn.RemoteAddr().String()) if err := s.proxy.Disconnect(gconn); err != nil { - logrus.Error(err) + s.logger.Error().Err(err).Msg("Failed to disconnect from the client") } if uint64(s.engine.CountConnections()) == 0 && s.Status == Stopped { @@ -90,7 +90,7 @@ func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action { func (s *Server) OnTraffic(gconn gnet.Conn) gnet.Action { if err := s.proxy.PassThrough(gconn, s.OnIncomingTraffic, s.OnOutgoingTraffic); err != nil { - logrus.Error(err) + s.logger.Error().Err(err).Msg("Failed to pass through traffic") // TODO: Close the connection *gracefully* return gnet.Close } @@ -99,29 +99,29 @@ func (s *Server) OnTraffic(gconn gnet.Conn) gnet.Action { } func (s *Server) OnShutdown(engine gnet.Engine) { - logrus.Println("GatewayD is shutting down...") + s.logger.Debug().Msg("GatewayD is shutting down...") s.proxy.Shutdown() s.Status = Stopped } func (s *Server) OnTick() (time.Duration, gnet.Action) { - logrus.Println("GatewayD is ticking...") - logrus.Infof("Active connections: %d", s.engine.CountConnections()) + s.logger.Debug().Msg("GatewayD is ticking...") + s.logger.Info().Msgf("Active connections: %d", s.engine.CountConnections()) return time.Duration(s.TickInterval * int(time.Second)), gnet.None } func (s *Server) Run() error { - logrus.Infof("GatewayD is running with PID %d", os.Getpid()) + s.logger.Info().Msgf("GatewayD is running with PID %d", os.Getpid()) // Try to resolve the address and log an error if it can't be resolved - addr, err := Resolve(s.Network, s.Address) + addr, err := Resolve(s.Network, s.Address, s.logger) if err != nil { - logrus.Error(err) + s.logger.Error().Err(err).Msg("Failed to resolve address") } err = gnet.Run(s, s.Network+"://"+addr, s.Options...) if err != nil { - logrus.Error(err) + s.logger.Error().Err(err).Msg("Failed to start server") return fmt.Errorf("failed to start server: %w", err) } @@ -145,6 +145,7 @@ func NewServer( options []gnet.Option, onIncomingTraffic, onOutgoingTraffic Traffic, proxy Proxy, + logger zerolog.Logger, ) *Server { server := Server{ Network: network, @@ -155,41 +156,43 @@ func NewServer( } // Try to resolve the address and log an error if it can't be resolved - addr, err := Resolve(server.Network, server.Address) + addr, err := Resolve(server.Network, server.Address, logger) if err != nil { - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to resolve address") } if addr != "" { server.Address = addr - logrus.Infof("GatewayD is listening on %s", addr) + logger.Debug().Msgf("Resolved address to %s", addr) + logger.Info().Msgf("GatewayD is listening on %s", addr) } else { - logrus.Warnf("GatewayD is listening on %s (cannot resolve address)", server.Address) + logger.Error().Msg("Failed to resolve address") + logger.Warn().Msgf("GatewayD is listening on %s (unresolved address)", server.Address) } // Get the current limits - limits := GetRLimit() + limits := GetRLimit(logger) // Set the soft and hard limits if they are not set if softLimit == 0 { server.SoftLimit = limits.Cur - logrus.Debugf("Soft limit is not set, using the current system soft limit") + logger.Debug().Msg("Soft limit is not set, using the current system soft limit") } else { server.SoftLimit = softLimit - logrus.Debugf("Soft limit is set to %d", softLimit) + logger.Debug().Msgf("Soft limit is set to %d", softLimit) } if hardLimit == 0 { server.HardLimit = limits.Max - logrus.Debugf("Hard limit is not set, using the current system hard limit") + logger.Debug().Msgf("Hard limit is not set, using the current system hard limit") } else { server.HardLimit = hardLimit - logrus.Debugf("Hard limit is set to %d", hardLimit) + logger.Debug().Msgf("Hard limit is set to %d", hardLimit) } if tickInterval == 0 { server.TickInterval = int(DefaultTickInterval) - logrus.Debugf("Tick interval is not set, using the default value") + logger.Debug().Msgf("Tick interval is not set, using the default value") } else { server.TickInterval = tickInterval } @@ -197,7 +200,10 @@ func NewServer( if onIncomingTraffic == nil { server.OnIncomingTraffic = func(gconn gnet.Conn, cl *Client, buf []byte, err error) error { // TODO: Implement the traffic handler - logrus.Infof("GatewayD is passing traffic from %s to %s", gconn.RemoteAddr().String(), gconn.LocalAddr().String()) + logger.Info().Msgf( + "GatewayD is passing traffic from %s to %s", + gconn.RemoteAddr().String(), + gconn.LocalAddr().String()) return nil } } else { @@ -207,7 +213,10 @@ func NewServer( if onOutgoingTraffic == nil { server.OnOutgoingTraffic = func(gconn gnet.Conn, cl *Client, buf []byte, err error) error { // TODO: Implement the traffic handler - logrus.Infof("GatewayD is passing traffic from %s to %s", gconn.LocalAddr().String(), gconn.RemoteAddr().String()) + logger.Info().Msgf( + "GatewayD is passing traffic from %s to %s", + gconn.LocalAddr().String(), + gconn.RemoteAddr().String()) return nil } } else { @@ -215,6 +224,7 @@ func NewServer( } server.proxy = proxy + server.logger = logger return &server } diff --git a/network/server_test.go b/network/server_test.go index ba88e754..13524653 100644 --- a/network/server_test.go +++ b/network/server_test.go @@ -5,8 +5,9 @@ import ( "testing" embeddedpostgres "github.com/fergusstrange/embedded-postgres" + "github.com/gatewayd-io/gatewayd/logging" "github.com/panjf2000/gnet/v2" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) @@ -17,31 +18,34 @@ func TestRunServer(t *testing.T) { t.Fatal(err) } + // Create a logger + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + onIncomingTraffic := func(gconn gnet.Conn, cl *Client, buf []byte, err error) error { - logrus.Info("Incoming traffic") + logger.Info().Msg("Incoming traffic") assert.Equal(t, CreatePgStartupPacket(), buf) assert.Nil(t, err) return nil } onOutgoingTraffic := func(gconn gnet.Conn, cl *Client, buf []byte, err error) error { - logrus.Info("Outgoing traffic") + logger.Info().Msg("Outgoing traffic") assert.Equal(t, CreatePostgreSQLPacket('R', []byte{0x0, 0x0, 0x0, 0x3}), buf) assert.Nil(t, err) return nil } // Create a connection pool - pool := NewPool() - assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize))) - assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize))) + pool := NewPool(logger) + assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize, logger))) + assert.NoError(t, pool.Put(NewClient("tcp", "localhost:5432", DefaultBufferSize, logger))) // Create a proxy with a fixed buffer pool proxy := NewProxy(pool, false, false, &Client{ Network: "tcp", Address: "localhost:5432", ReceiveBufferSize: DefaultBufferSize, - }) + }, logger) // Create a server server := NewServer( @@ -56,6 +60,7 @@ func TestRunServer(t *testing.T) { onIncomingTraffic, onOutgoingTraffic, proxy, + logger, ) assert.NotNil(t, server) @@ -76,7 +81,7 @@ func TestRunServer(t *testing.T) { for { if server.IsRunning() { - client := NewClient("tcp", "127.0.0.1:15432", DefaultBufferSize) + client := NewClient("tcp", "127.0.0.1:15432", DefaultBufferSize, logger) defer client.Close() assert.NotNil(t, client) diff --git a/network/utils.go b/network/utils.go index 63ab5dc6..30af5440 100644 --- a/network/utils.go +++ b/network/utils.go @@ -7,29 +7,29 @@ import ( "net" "syscall" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) -func GetRLimit() syscall.Rlimit { +func GetRLimit(logger zerolog.Logger) syscall.Rlimit { var limits syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { //nolint:nosnakecase - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to get rlimit") } - logrus.Debugf("Current system soft limit: %d", limits.Cur) - logrus.Debugf("Current system hard limit: %d", limits.Max) + logger.Debug().Msgf("Current system soft limit: %d", limits.Cur) + logger.Debug().Msgf("Current system hard limit: %d", limits.Max) return limits } -func GetID(network, address string, seed int) string { +func GetID(network, address string, seed int, logger zerolog.Logger) string { hash := sha256.New() _, err := hash.Write([]byte(fmt.Sprintf("%s://%s%d", network, address, seed))) if err != nil { - logrus.Error(err) + logger.Error().Err(err).Msg("Failed to generate ID") } return hex.EncodeToString(hash.Sum(nil)) } -func Resolve(network, address string) (string, error) { +func Resolve(network, address string, logger zerolog.Logger) (string, error) { switch network { case "tcp", "tcp4", "tcp6": addr, err := net.ResolveTCPAddr(network, address) @@ -41,7 +41,7 @@ func Resolve(network, address string) (string, error) { addr, err := net.ResolveUnixAddr(network, address) return addr.String(), err default: - logrus.Errorf("Network %s is not supported", network) + logger.Error().Msgf("Network %s is not supported", network) return "", ErrNetworkNotSupported } } diff --git a/network/utils_test.go b/network/utils_test.go index 2d624bc0..859ca0dc 100644 --- a/network/utils_test.go +++ b/network/utils_test.go @@ -3,22 +3,27 @@ package network import ( "testing" + "github.com/gatewayd-io/gatewayd/logging" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) func TestGetRlimit(t *testing.T) { - rlimit := GetRLimit() + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + rlimit := GetRLimit(logger) assert.Greater(t, rlimit.Cur, uint64(1)) assert.Greater(t, rlimit.Max, uint64(1)) } func TestGetID(t *testing.T) { - id := GetID("tcp", "localhost:5432", 1) + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + id := GetID("tcp", "localhost:5432", 1, logger) assert.Equal(t, "0cf47ee4e436ecb40dbd1d2d9a47179d1f6d98e2ea18d6fbd1cdfa85d3cec94f", id) } func TestResolve(t *testing.T) { - address, err := Resolve("udp", "localhost:53") + logger := logging.NewLogger(nil, zerolog.TimeFormatUnix, zerolog.DebugLevel, true) + address, err := Resolve("udp", "localhost:53", logger) assert.NoError(t, err) assert.Equal(t, "127.0.0.1:53", address) }