Skip to content

Commit

Permalink
correctly set connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed May 7, 2024
1 parent 313ef20 commit 251162a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func (g *Glutton) Start() error {

g.Logger.Debug("new connection", slog.String("addr", conn.LocalAddr().String()), slog.String("handler", rule.Target))

g.ctx = context.WithValue(g.ctx, ctxTimeout("timeout"), int64(viper.GetInt("conn_timeout")))
if err := g.UpdateConnectionTimeout(g.ctx, conn); err != nil {
g.Logger.Error("failed to set connection timeout", producer.ErrAttr(err))
}
Expand Down Expand Up @@ -253,10 +254,12 @@ func (g *Glutton) makeID() error {
return nil
}

type ctxTimeout string

// UpdateConnectionTimeout increase connection timeout limit on connection I/O operation
func (g *Glutton) UpdateConnectionTimeout(ctx context.Context, conn net.Conn) error {
if timeout, ok := ctx.Value("timeout").(time.Duration); ok {
if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
if timeout, ok := ctx.Value(ctxTimeout("timeout")).(int64); ok {
if err := conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/tcp/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *telnetServer) read(conn net.Conn) (string, error) {
return msg, err
}
s.events = append(s.events, parsedTelnet{Direction: "read", Message: msg})
return msg, err
return msg, nil
}

func (s *telnetServer) getSample(cmd string, logger interfaces.Logger) error {
Expand Down

0 comments on commit 251162a

Please sign in to comment.