Skip to content

Commit

Permalink
Fix linter errors
Browse files Browse the repository at this point in the history
Remove unused dependencies (gnet/v2 and others)
  • Loading branch information
mostafa committed Oct 8, 2023
1 parent f89cfde commit 5cb26b2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 62 deletions.
1 change: 1 addition & 0 deletions config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const (
DefaultTCPKeepAliveDuration = 3 * time.Second
DefaultLoadBalancer = "roundrobin"
DefaultTCPNoDelay = true
DefaultEngineStopTimeout = 5 * time.Second

// Utility constants.
DefaultSeed = 1000
Expand Down
6 changes: 0 additions & 6 deletions config/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path/filepath"
"time"

"github.com/panjf2000/gnet/v2"
"github.com/rs/zerolog"
)

Expand All @@ -28,11 +27,6 @@ var (
"continue": Continue,
"stop": Stop,
}
loadBalancers = map[string]gnet.LoadBalancing{
"roundrobin": gnet.RoundRobin,
"leastconnections": gnet.LeastConnections,
"sourceaddrhash": gnet.SourceAddrHash,
}
logOutputs = map[string]LogOutput{
"console": Console,
"stdout": Stdout,
Expand Down
13 changes: 13 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
ErrCodeClientSendFailed
ErrCodeServerReceiveFailed
ErrCodeServerSendFailed
ErrCodeServerListenFailed
ErrCodeSplitHostPortFailed
ErrCodeAcceptFailed
ErrCodeReadFailed
ErrCodePutFailed
ErrCodeCastFailed
ErrCodeHookVerificationFailed
Expand Down Expand Up @@ -77,6 +81,15 @@ var (
ErrCodeServerSendFailed, "couldn't send data to the client", nil)
ErrServerReceiveFailed = NewGatewayDError(
ErrCodeServerReceiveFailed, "couldn't receive data from the client", nil)
ErrServerListenFailed = NewGatewayDError(
ErrCodeServerListenFailed, "couldn't listen on the server", nil)
ErrSplitHostPortFailed = NewGatewayDError(
ErrCodeSplitHostPortFailed, "failed to split host:port", nil)
ErrAcceptFailed = NewGatewayDError(
ErrCodeAcceptFailed, "failed to accept connection", nil)

ErrReadFailed = NewGatewayDError(
ErrCodeReadFailed, "failed to read from the client", nil)

ErrPutFailed = NewGatewayDError(
ErrCodePutFailed, "failed to put in pool", nil)
Expand Down
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/invopop/jsonschema v0.8.0
github.com/knadh/koanf v1.5.0
github.com/mitchellh/mapstructure v1.5.0
github.com/panjf2000/gnet/v2 v2.3.2
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
Expand Down Expand Up @@ -70,16 +69,12 @@ require (
github.com/prometheus/procfs v0.11.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
12 changes: 0 additions & 12 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ func (c *Client) Send(data []byte) (int, *gerr.GatewayDError) {
break
}

n, err := c.Conn.Write(data)
written, err := c.Conn.Write(data)
if err != nil {
c.logger.Error().Err(err).Msg("Couldn't send data to the server")
span.RecordError(err)
return 0, gerr.ErrClientSendFailed.Wrap(err)
}

sent += n
sent += written
}

c.logger.Debug().Fields(
Expand Down
60 changes: 29 additions & 31 deletions network/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net"
"strconv"
"time"

"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
)

type Option struct {
Expand Down Expand Up @@ -39,15 +42,15 @@ func (engine *Engine) CountConnections() int {
}

func (engine *Engine) Stop(ctx context.Context) error {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(2*time.Second))
_, cancel := context.WithDeadline(ctx, time.Now().Add(config.DefaultEngineStopTimeout))
defer cancel()

engine.stopServer <- struct{}{}
return nil
}

// Run starts a server and connects all the handlers.
func Run(network, address string, server *Server, opts Option) error {
func Run(network, address string, server *Server) *gerr.GatewayDError {
server.engine = Engine{
connections: 0,
stopServer: make(chan struct{}),
Expand All @@ -57,11 +60,11 @@ func Run(network, address string, server *Server, opts Option) error {
return nil
}

if ln, err := net.Listen(network, address); err != nil {
var err error
server.engine.listener, err = net.Listen(network, address)
if err != nil {
server.logger.Error().Err(err).Msg("Server failed to start listening")
return err
} else {
server.engine.listener = ln
return gerr.ErrServerListenFailed.Wrap(err)
}
defer server.engine.listener.Close()

Expand All @@ -70,25 +73,22 @@ func Run(network, address string, server *Server, opts Option) error {
return nil
}

if host, port, err := net.SplitHostPort(server.engine.listener.Addr().String()); err != nil {
var port string
server.engine.host, port, err = net.SplitHostPort(server.engine.listener.Addr().String())
if err != nil {
server.logger.Error().Err(err).Msg("Failed to split host and port")
return err
} else {
server.engine.host = host
if server.engine.port, err = strconv.Atoi(port); err != nil {
server.logger.Error().Err(err).Msg("Failed to convert port to integer")
return err
}
return gerr.ErrSplitHostPortFailed.Wrap(err)
}

if server.engine.port, err = strconv.Atoi(port); err != nil {
server.logger.Error().Err(err).Msg("Failed to convert port to integer")
return gerr.ErrCastFailed.Wrap(err)
}

go func(server *Server) {
for {
select {
case <-server.engine.stopServer:
server.OnShutdown(server.engine)
server.logger.Debug().Msg("Server stopped")
}
}
<-server.engine.stopServer
server.OnShutdown(server.engine)
server.logger.Debug().Msg("Server stopped")
}(server)

go func(server *Server) {
Expand All @@ -113,11 +113,13 @@ func Run(network, address string, server *Server, opts Option) error {
conn, err := server.engine.listener.Accept()
if err != nil {
server.logger.Error().Err(err).Msg("Failed to accept connection")
return err
return gerr.ErrAcceptFailed.Wrap(err)
}

if out, action := server.OnOpen(conn); action != None {
conn.Write(out)
if _, err := conn.Write(out); err != nil {
server.logger.Error().Err(err).Msg("Failed to write to connection")
}
conn.Close()
if action == Shutdown {
server.OnShutdown(server.engine)
Expand All @@ -136,14 +138,10 @@ func Run(network, address string, server *Server, opts Option) error {
}(server, conn, stopConnection)

go func(server *Server, conn net.Conn, stopConnection chan struct{}) {
for {
select {
case <-stopConnection:
server.engine.connections--
if action := server.OnClose(conn, err); action == Close {
return
}
}
<-stopConnection
server.engine.connections--
if action := server.OnClose(conn, err); action == Close {
return
}
}(server, conn, stopConnection)
}
Expand Down
9 changes: 4 additions & 5 deletions network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (pr *Proxy) BusyConnections() []string {
}

// receiveTrafficFromClient is a function that waits to receive data from the client.
func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, error) {
func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, *gerr.GatewayDError) {
_, span := otel.Tracer(config.TracerName).Start(pr.ctx, "receiveTrafficFromClient")
defer span.End()

Expand All @@ -571,7 +571,7 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, error) {
metrics.BytesReceivedFromClient.Observe(float64(read))
metrics.TotalTrafficBytes.Observe(float64(read))

return chunk[:read], err
return chunk[:read], gerr.ErrReadFailed.Wrap(err)
}

received += read
Expand All @@ -597,7 +597,6 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, error) {
metrics.BytesReceivedFromClient.Observe(float64(length))
metrics.TotalTrafficBytes.Observe(float64(length))

//nolint:wrapcheck
return buffer.Bytes(), nil
}

Expand Down Expand Up @@ -668,14 +667,14 @@ func (pr *Proxy) sendTrafficToClient(
break
}

n, origErr := conn.Write(response[:received])
written, origErr := conn.Write(response[:received])
if origErr != nil {
pr.logger.Error().Err(origErr).Msg("Error writing to client")
span.RecordError(origErr)
return gerr.ErrServerSendFailed.Wrap(origErr)
}

sent += n
sent += written
}

pr.logger.Debug().Fields(
Expand Down
2 changes: 1 addition & 1 deletion network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (s *Server) Run() error {
}

// Start the server.
origErr := Run(s.Network, addr, s, s.Options)
origErr := Run(s.Network, addr, s)
if origErr != nil {
s.logger.Error().Err(origErr).Msg("Failed to start server")
span.RecordError(origErr)
Expand Down

0 comments on commit 5cb26b2

Please sign in to comment.