summaryrefslogtreecommitdiff
path: root/internal/server
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2025-07-15 19:25:43 +0300
committerKyren223 <Kyren223@proton.me>2025-07-19 18:32:12 +0300
commit8f58905364915fce4929649afe4835358d9d831b (patch)
treea3f4132014bf38600d533f97b99db3e305a5e515 /internal/server
parentafee78d6256264d8b1fb556218be1a5434f7a295 (diff)
Added in progress requests metric and also returning an error when the
user hits the rate limit
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/api/api.go1
-rw-r--r--internal/server/metrics/metrics.go24
-rw-r--r--internal/server/server.go6
3 files changed, 10 insertions, 21 deletions
diff --git a/internal/server/api/api.go b/internal/server/api/api.go
index 2f66a9b..8b1d711 100644
--- a/internal/server/api/api.go
+++ b/internal/server/api/api.go
@@ -21,6 +21,7 @@ var (
ErrInternalError = packet.Error{Error: "internal server error"}
ErrPermissionDenied = packet.Error{Error: "permission denied"}
ErrNotImplemented = packet.Error{Error: "not implemented yet"}
+ ErrRateLimited = packet.Error{Error: "rate limited"}
ErrSuccess = packet.Error{Error: "success"}
DefaultBanReason = ""
diff --git a/internal/server/metrics/metrics.go b/internal/server/metrics/metrics.go
index 5f7eb15..4937787 100644
--- a/internal/server/metrics/metrics.go
+++ b/internal/server/metrics/metrics.go
@@ -13,11 +13,11 @@ var RequestsProcessed = promauto.NewCounterVec(prometheus.CounterOpts{
Help: "The total number of processed requests",
}, []string{"request_type", "dropped"})
-// var RequestsInProgress = promauto.NewCounterVec(prometheus.CounterOpts{
-// Namespace: namespace,
-// Name: "requests_in_progress_total",
-// Help: "The total number of in-progress requests",
-// }, []string{"request_type"})
+var RequestsInProgress = promauto.NewGaugeVec(prometheus.GaugeOpts{
+ Namespace: namespace,
+ Name: "requests_in_progress_total",
+ Help: "The total number of in-progress requests",
+}, []string{"request_type"})
var RequestProcessingDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
@@ -26,20 +26,6 @@ var RequestProcessingDuration = promauto.NewHistogramVec(prometheus.HistogramOpt
NativeHistogramBucketFactor: 1.00271,
}, []string{"request_type", "dropped"})
-// var RequestProcessingDuration = promauto.NewSummaryVec(prometheus.SummaryOpts{
-// Namespace: namespace,
-// Name: "request_processing_duration_seconds",
-// Help: "The duration in seconds it took to process a request",
-// Objectives: map[float64]float64{
-// 0.01: 0.001,
-// 0.50: 0.005,
-// 0.90: 0.009,
-// 0.95: 0.0095,
-// 0.99: 0.0099,
-// },
-// MaxAge: 1 * time.Hour,
-// }, []string{"request_type"})
-
var ConnectionsEstablished = promauto.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "connections_established_total",
diff --git a/internal/server/server.go b/internal/server/server.go
index 668ec38..0620793 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -300,22 +300,23 @@ func (server *server) handleConnection(conn net.Conn) {
// will still have a time limit upper bound, from timeout()
for request := range framer.Out {
+ metrics.RequestsInProgress.WithLabelValues(request.Type().String()).Inc()
start := time.Now().UTC()
success := processPacket(localCtx, sess, request)
duration := time.Since(start)
+ metrics.RequestsInProgress.WithLabelValues(request.Type().String()).Dec()
labels := prometheus.Labels{
"request_type": request.Type().String(),
"dropped": strconv.FormatBool(!success),
}
- metrics.RequestsProcessed.With(labels).Inc()
+
metrics.RequestProcessingDuration.With(labels).Observe(float64(duration.Seconds()))
if success {
slog.InfoContext(ctx, "processed request", "request_type", request.Type().String(), "duration", duration.String(), "duration_ns", duration.Nanoseconds())
} else {
slog.InfoContext(ctx, "dropped request", "request_type", request.Type().String(), "duration", duration.String(), "duration_ns", duration.Nanoseconds())
}
-
}
slog.InfoContext(ctx, "processor done")
}()
@@ -370,6 +371,7 @@ func (server *server) handleConnection(conn net.Conn) {
func processPacket(ctx context.Context, sess *session.Session, pkt packet.Packet) bool {
tokens := TokensPerRequest(pkt.Type())
if !sess.RateLimiter().Take(tokens) {
+ _ = sess.Write(ctx, &api.ErrRateLimited)
return false // Rate limit was hit
}