refactor(formmailer): use *dataset.View directly, tighter timeouts

- Drop CohortSource interface — it had exactly one implementation.
  Blacklist is now *dataset.View[ipcohort.Cohort] directly, matching
  check-ip's usage. One concrete type, no premature abstraction.
- SMTP 15s → 5s, MX 3s → 2s. A relay or resolver that isn't
  responding inside those bounds isn't going to deliver the mail;
  faster failure is better than holding the request goroutine.
This commit is contained in:
AJ ONeal 2026-04-20 19:36:51 -06:00
parent b23610fdf1
commit f972d6f117
No known key found for this signature in database
2 changed files with 6 additions and 10 deletions

View File

@ -43,6 +43,7 @@ import (
"golang.org/x/time/rate" "golang.org/x/time/rate"
"github.com/therootcompany/golib/net/ipcohort" "github.com/therootcompany/golib/net/ipcohort"
"github.com/therootcompany/golib/sync/dataset"
) )
const ( const (
@ -55,8 +56,8 @@ const (
defaultRPM = 5 defaultRPM = 5
defaultBurst = 3 defaultBurst = 3
defaultSMTPTimeout = 15 * time.Second defaultSMTPTimeout = 5 * time.Second
defaultMXTimeout = 3 * time.Second defaultMXTimeout = 2 * time.Second
limiterTTL = 10 * time.Minute limiterTTL = 10 * time.Minute
limiterSweepEvery = 1024 // sweep once every N handler invocations limiterSweepEvery = 1024 // sweep once every N handler invocations
@ -72,12 +73,6 @@ var (
phoneRe = regexp.MustCompile(`^[0-9+\-\(\) ]{7,20}$`) phoneRe = regexp.MustCompile(`^[0-9+\-\(\) ]{7,20}$`)
) )
// CohortSource returns the current cohort snapshot, or nil if not yet loaded.
// *dataset.View[ipcohort.Cohort] satisfies this interface directly.
type CohortSource interface {
Value() *ipcohort.Cohort
}
// FormFields maps logical field names to the HTML form field names. // FormFields maps logical field names to the HTML form field names.
// Zero values use GravityForms-compatible defaults (input_1, input_3, etc.). // Zero values use GravityForms-compatible defaults (input_1, input_3, etc.).
type FormFields struct { type FormFields struct {
@ -119,8 +114,8 @@ type FormMailer struct {
ContentType string // inferred from SuccessBody if empty ContentType string // inferred from SuccessBody if empty
// Blacklist — if set, matching IPs are rejected before any other processing. // Blacklist — if set, matching IPs are rejected before any other processing.
// *dataset.View[ipcohort.Cohort] satisfies this interface. // Value() returns nil before the first successful load (no blocks applied).
Blacklist CohortSource Blacklist *dataset.View[ipcohort.Cohort]
// AllowedCountries — if non-nil, only requests from listed ISO codes are // AllowedCountries — if non-nil, only requests from listed ISO codes are
// accepted. Unknown country ("") is always allowed. // accepted. Unknown country ("") is always allowed.

View File

@ -5,5 +5,6 @@ go 1.26.0
require ( require (
github.com/phuslu/iploc v1.0.20260415 github.com/phuslu/iploc v1.0.20260415
github.com/therootcompany/golib/net/ipcohort v0.0.0 github.com/therootcompany/golib/net/ipcohort v0.0.0
github.com/therootcompany/golib/sync/dataset v0.0.0
golang.org/x/time v0.15.0 golang.org/x/time v0.15.0
) )