feat(identity): confirm registration email atomically

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 09:12:15 +03:00
1 parent 8e7ce8d71c
commit fade4e76e4
75 files changed
+4188 -403

No files matched your search

@@ -39,9 +39,8 @@ type mailMetrics struct {
// senderCancelled counts deliveries that the sending worker ended without
// sending, by cancel reason, a closed set from the domain.
senderCancelled *client.CounterVec
// identityRateLimited counts registration and resend requests that the
// identity limiter refused, by scope: address or client. No address,
// email, or key is ever a label.
// identityRateLimited counts identity requests the limiter refused, by
// scope: address, client, or token. No address, email, or key is a label.
identityRateLimited *client.CounterVec
}
@@ -95,7 +94,7 @@ func newMailMetrics() mailMetrics {
}, []string{"reason"}),
identityRateLimited: client.NewCounterVec(client.CounterOpts{
Name: "lincodex_identity_rate_limit_refusals_total",
Help: "Registration and verification resend requests refused with 429 by the identity limiter, by scope: address (per email address) or client (per network address). A large share of client refusals hints at a broken proxy chain, where many clients share one address; a rise of address refusals can mean a targeted lockout of one address.",
Help: "Identity requests refused with 429 by the limiter, by scope: address (per email address), client (per network address), or token (per verification token). A large share of client refusals hints at a broken proxy chain, where many clients share one address; a rise of address refusals can mean a targeted lockout of one address.",
}, []string{"scope"}),
senderRunning: client.NewGauge(client.GaugeOpts{
Name: "lincodex_mail_sender_running",
@@ -107,7 +106,7 @@ func newMailMetrics() mailMetrics {
for _, reason := range domain.DeliveryCancelReasons() {
metrics.senderCancelled.WithLabelValues(string(reason))
}
for _, scope := range []string{IdentityScopeAddress, IdentityScopeClient} {
for _, scope := range []string{IdentityScopeAddress, IdentityScopeClient, IdentityScopeToken} {
metrics.identityRateLimited.WithLabelValues(scope)
}
return metrics
@@ -117,12 +116,13 @@ func newMailMetrics() mailMetrics {
const (
IdentityScopeAddress = "address"
IdentityScopeClient = "client"
IdentityScopeToken = "token"
)
// ObserveIdentityRateLimited counts one refused identity request. A scope
// outside the two known ones counts as address, so the label stays bounded.
// outside the known scopes counts as address, so the label stays bounded.
func (metrics *Metrics) ObserveIdentityRateLimited(scope string) {
if scope != IdentityScopeClient {
if scope != IdentityScopeClient && scope != IdentityScopeToken {
scope = IdentityScopeAddress
}
metrics.mail.identityRateLimited.WithLabelValues(scope).Inc()
@@ -44,10 +44,12 @@ func TestIdentityRateLimitRefusalsAreCountedByBoundedScope(t *testing.T) {
metrics := New()
metrics.ObserveIdentityRateLimited(IdentityScopeClient)
metrics.ObserveIdentityRateLimited(IdentityScopeAddress)
metrics.ObserveIdentityRateLimited(IdentityScopeToken)
metrics.ObserveIdentityRateLimited("203.0.113.9")
body := scrape(t, metrics)
requireSeries(t, body, `lincodex_identity_rate_limit_refusals_total{scope="client"} 1`)
requireSeries(t, body, `lincodex_identity_rate_limit_refusals_total{scope="address"} 2`)
requireSeries(t, body, `lincodex_identity_rate_limit_refusals_total{scope="token"} 1`)
if strings.Contains(body, "203.0.113.9") {
t.Fatal("an unknown scope became a label value")
}