Skip to content

Commit

Permalink
Fix improve reconnection logging
Browse files Browse the repository at this point in the history
disconnect/connect within a second will be logged, but with logLevel 2 and with a single "reconnected" message
  • Loading branch information
hons82 committed Oct 11, 2021
1 parent b61db3c commit e2fbec3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Configuration options:
* `multiplier`: interval multiplier if reconnect failed, *default:* `1.5`
* `max_interval`: maximal time client would wait before redialing the server, *default:* `1m`
* `max_time`: maximal time client would try to reconnect to the server if connection was lost, set `0` to never stop trying, *default:* `15m`
* `keep_alive`**
* `keep_alive`
* `interval`: the amount of time to wait between sending keepalive packets, *default:* `25s`

## Configuration - Server
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hons82/go-http-tunnel
go 1.13

require (
github.com/bep/debounce v1.2.0
github.com/calmh/luhn v2.0.0+incompatible
github.com/cenkalti/backoff v2.1.1+incompatible
github.com/golang/mock v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo=
github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
github.com/calmh/luhn v2.0.0+incompatible h1:xHkbAc8FBgMiGUaKsiYcwtf8xhSXVtRKA2NhY7hFCAc=
github.com/calmh/luhn v2.0.0+incompatible/go.mod h1:70IGmMi0GKRs073gl/oH5/yiJnTt61h35YQhvo/k3Cc=
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
Expand Down
20 changes: 20 additions & 0 deletions id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,23 @@ func untypeoify(s string) string {
s = strings.Replace(s, "8", "B", -1)
return s
}

// https://play.golang.org/p/Qg_uv_inCek
// contains checks if a string is present in a slice
func Contains(s []ID, item ID) bool {
for _, v := range s {
if v.Equals(item) {
return true
}
}
return false
}

func Remove(s []ID, item ID) ([]ID, bool) {
for i, v := range s {
if v.Equals(item) {
return append(s[:i], s[i+1:]...), true
}
}
return s, false
}
53 changes: 43 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"golang.org/x/net/http2"

"github.com/bep/debounce"
"github.com/hons82/go-http-tunnel/connection"
"github.com/hons82/go-http-tunnel/id"
"github.com/hons82/go-http-tunnel/log"
Expand Down Expand Up @@ -57,6 +58,14 @@ type Server struct {
httpClient *http.Client
logger log.Logger
vhostMuxer *vhost.TLSMuxer

debounce Debounced
}

// Debounced Hold IDs that are disconnected for a short time before executing the function.
type Debounced struct {
debounced func(f func())
disconnectedIDs []id.ID
}

// NewServer creates a new Server.
Expand All @@ -71,11 +80,16 @@ func NewServer(config *ServerConfig) (*Server, error) {
logger = log.NewNopLogger()
}

debounced := &Debounced{
debounced: debounce.New(1000 * time.Millisecond),
}

s := &Server{
registry: newRegistry(logger),
config: config,
listener: listener,
logger: logger,
debounce: *debounced,
}

t := &http2.Transport{}
Expand Down Expand Up @@ -158,11 +172,18 @@ func listener(config *ServerConfig) (net.Listener, error) {
// disconnected clears resources used by client, it's invoked by connection pool
// when client goes away.
func (s *Server) disconnected(identifier id.ID) {
s.logger.Log(
"level", 1,
"action", "disconnected",
"identifier", identifier,
)
s.debounce.disconnectedIDs = append(s.debounce.disconnectedIDs, identifier)

s.debounce.debounced(func() {
for _, id := range s.debounce.disconnectedIDs {
s.logger.Log(
"level", 1,
"action", "disconnected",
"identifier", id,
)
}
s.debounce.disconnectedIDs = nil
})

i := s.registry.clear(identifier)
if i == nil {
Expand Down Expand Up @@ -233,7 +254,7 @@ func (s *Server) handleClient(conn net.Conn) {
logger := log.NewContext(s.logger).With("remote addr", conn.RemoteAddr())

logger.Log(
"level", 1,
"level", 2,
"action", "try connect",
)

Expand All @@ -246,6 +267,9 @@ func (s *Server) handleClient(conn net.Conn) {
ok bool

inConnPool bool

remainingIDs []id.ID
found bool
)

tlsConn, ok := conn.(*tls.Conn)
Expand Down Expand Up @@ -373,10 +397,19 @@ func (s *Server) handleClient(conn net.Conn) {
goto reject
}

logger.Log(
"level", 1,
"action", "connected",
)
remainingIDs, found = id.Remove(s.debounce.disconnectedIDs, identifier)
if found {
s.debounce.disconnectedIDs = remainingIDs
logger.Log(
"level", 2,
"action", "reconnected",
)
} else {
logger.Log(
"level", 1,
"action", "connected",
)
}

return

Expand Down

0 comments on commit e2fbec3

Please sign in to comment.