Adding Rate Limits to a Netlify Chat Endpoint with Postgres for Palm Beach Contractors
说明与用途
作者在 Postgres 中实现了基于 IP 的速率限制,阻止了攻击 Netlify 聊天端点的机器人垃圾邮件,保持了当地承包商数据库的完整性。
问题
Automated scrapers were flooding a Netlify chat endpoint with spam, exhausting Postgres connections and corrupting lead data.
结果
The IP‑hashing and hourly counter in Postgres immediately stopped bot traffic, preventing database exhaustion and keeping lead pipelines clean for local contractors.
工作流
- Deploy Netlify Function with Node.js
- Connect to Postgres via DATABASE_URL with SSL
- On each POST request: 1) Retrieve client IP from headers, 2) Hash IP with SHA‑256, 3) Delete rate_limit rows older than 1 hour, 4) Count requests for the hashed IP in the last hour, 5) If count ≥5, return 429 and close DB, 6) Else insert new rate_limit record, 7) Proceed with chat logic, 8) Close DB and return 200
证据
bots were hammering the endpoint fifty times a minute, trying to inject SQL strings and garbage URLs into my database
I needed a hard server‑side rate limit tied to the visitor's IP address, stored directly in my Postgres database without adding heavy third‑party SaaS dependencies
Implementing this simple IP hashing and Postgres counter immediately stopped the bot traffic on my site
- 记录日期: 2026-11-03T14:14:14.287Z