limits: sync streams, listings per author, inbound connections per IP
A peer may hold 4 sync streams and open 1500 a minute; banned peers get none and cannot redial. A node keeps at most 100 unexpired listings per author (--max-per-author); updates, renewals and deletions of held ones still work. At most 16 inbound connections per public IP or IPv6 /64 (--max-conns-per-ip); local and private addresses are exempt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -26,6 +26,10 @@ var (
|
||||
ErrNotFound = errors.New("store: not found")
|
||||
// ErrRevoked is returned for listings signed by a revoked device.
|
||||
ErrRevoked = errors.New("store: device revoked")
|
||||
// ErrQuota is returned for a new listing from an author who already has
|
||||
// the most this node keeps (Options.MaxPerAuthor). Updates, renewals and
|
||||
// deletions of listings the node holds are never refused for this.
|
||||
ErrQuota = errors.New("store: this author has too many listings on this node")
|
||||
)
|
||||
|
||||
// Outcome tells the caller what Put did, so the p2p layer knows whether a
|
||||
@@ -57,8 +61,19 @@ type Options struct {
|
||||
|
||||
// Trust says whose abuse reports hide listings on this node (see Trust).
|
||||
Trust Trust
|
||||
|
||||
// MaxPerAuthor caps the unexpired listings (tombstones included) this node
|
||||
// keeps for one author; 0 means DefaultMaxPerAuthor, negative no cap. A
|
||||
// listing past the cap is not stored and so not passed on. Each node
|
||||
// decides for itself, so nodes with different caps hold different sets.
|
||||
MaxPerAuthor int
|
||||
}
|
||||
|
||||
// DefaultMaxPerAuthor is generous for a person or a small agency and makes one
|
||||
// identity a poor way to fill a node: more takes more identities, and each
|
||||
// listing of each costs its own proof-of-work.
|
||||
const DefaultMaxPerAuthor = 100
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
difficulty int
|
||||
@@ -68,6 +83,7 @@ type Store struct {
|
||||
ownerQueue int
|
||||
mailboxQueue int
|
||||
trust Trust
|
||||
maxPerAuthor int // 0: no cap
|
||||
}
|
||||
|
||||
const schema = `
|
||||
@@ -217,6 +233,12 @@ func Open(path string, opt Options) (*Store, error) {
|
||||
msgDifficulty: opt.MessageDifficulty, maxMessages: opt.MaxMessages,
|
||||
ownerQueue: opt.OwnerQueue, mailboxQueue: opt.MailboxQueue}
|
||||
s.trust = opt.Trust
|
||||
switch {
|
||||
case opt.MaxPerAuthor == 0:
|
||||
s.maxPerAuthor = DefaultMaxPerAuthor
|
||||
case opt.MaxPerAuthor > 0:
|
||||
s.maxPerAuthor = opt.MaxPerAuthor
|
||||
}
|
||||
if s.trust.Threshold <= 0 {
|
||||
s.trust.Threshold = DefaultHideThreshold
|
||||
}
|
||||
@@ -284,6 +306,18 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
outcome := Inserted
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
if s.maxPerAuthor > 0 {
|
||||
var held int
|
||||
err = tx.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM listings WHERE author = ? AND expires_at > ?`,
|
||||
l.Author, s.clock().Unix()).Scan(&held)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if held >= s.maxPerAuthor {
|
||||
return Ignored, ErrQuota
|
||||
}
|
||||
}
|
||||
case err != nil:
|
||||
return Ignored, err
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user