Skip to content

Commit

Permalink
sqlproxy/admitter: reset IPs on successful connection
Browse files Browse the repository at this point in the history
When a connection from an IP succeeds, remove the IP from the internal
table that tracks the number of attempts per IP.

Release note: none.
  • Loading branch information
Spas Bojanov committed Nov 2, 2020
1 parent be97f8a commit f373eb9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/ccl/sqlproxyccl/admitter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ type Service interface {
// AllowRequest determines whether a request should be allowed to proceed. It
// rate limits requests from IP addresses regardless of tenant id.
AllowRequest(ipAddress string, now time.Time) error

// RequestSuccess records the result of a successful request.
RequestSuccess(ipAddress string)
}
8 changes: 8 additions & 0 deletions pkg/ccl/sqlproxyccl/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Metrics struct {
ClientDisconnectCount *metric.Counter
CurConnCount *metric.Gauge
RoutingErrCount *metric.Counter
RefusedConnCount *metric.Counter
}

// MetricStruct implements the metrics.Struct interface.
Expand Down Expand Up @@ -56,6 +57,12 @@ var (
Measurement: "Disconnects",
Unit: metric.Unit_COUNT,
}
metaRefusedConnCount = metric.Metadata{
Name: "proxy.err.refused_conn",
Help: "Number of refused connections initiated by a given IP",
Measurement: "Refused",
Unit: metric.Unit_COUNT,
}
)

// MakeProxyMetrics instantiates the metrics holder for proxy monitoring.
Expand All @@ -66,5 +73,6 @@ func MakeProxyMetrics() Metrics {
ClientDisconnectCount: metric.NewCounter(metaClientDisconnectCount),
CurConnCount: metric.NewGauge(metaCurConnCount),
RoutingErrCount: metric.NewCounter(metaRoutingErrCount),
RefusedConnCount: metric.NewCounter(metaBackendDisconnectCount),
}
}
5 changes: 5 additions & 0 deletions pkg/ccl/sqlproxyccl/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *Server) Proxy(conn net.Conn) error {
// TODO(spaskob): check for previous successful connection from the same IP
// in which case allow connection.
if err := s.admitter.AllowRequest(conn.RemoteAddr().String(), timeutil.Now()); err != nil {
s.metrics.RefusedConnCount.Inc(1)
return newErrorf(CodeProxyRefusedConnection, "too many connection attempts")
}
}
Expand Down Expand Up @@ -137,6 +138,10 @@ func (s *Server) Proxy(conn net.Conn) error {
return newErrorf(CodeBackendDown, "sending SSLRequest to target server: %v", err)
}

if s.admitter != nil {
s.admitter.RequestSuccess(conn.RemoteAddr().String())
}

response := make([]byte, 1)
if _, err = io.ReadFull(crdbConn, response); err != nil {
s.metrics.BackendDownCount.Inc(1)
Expand Down

0 comments on commit f373eb9

Please sign in to comment.