mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
50 lines
1.9 KiB
Go
50 lines
1.9 KiB
Go
package ratelimit_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/hanzoai/mail/ratelimit"
|
|
)
|
|
|
|
func ExampleLimiter() {
|
|
// Make a new rate limit that has maxima per minute, hour and day. The maxima are
|
|
// tracked per ipmasked1 (ipv4 /32 or ipv6 /64), ipmasked2 (ipv4 /26 or ipv6 /48)
|
|
// and ipmasked3 (ipv4 /21 or ipv6 /32).
|
|
//
|
|
// It is common to allow short bursts (with a narrow window), but not allow a high
|
|
// sustained rate (with wide window).
|
|
limit := ratelimit.Limiter{
|
|
WindowLimits: []ratelimit.WindowLimit{
|
|
{Window: time.Minute, Limits: [...]int64{2, 3, 4}},
|
|
{Window: time.Hour, Limits: [...]int64{4, 6, 8}},
|
|
{Window: 24 * time.Hour, Limits: [...]int64{20, 40, 60}},
|
|
},
|
|
}
|
|
|
|
tm, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
|
|
|
|
fmt.Println("1:", limit.Add(net.ParseIP("127.0.0.1"), tm, 1)) // Success.
|
|
fmt.Println("2:", limit.Add(net.ParseIP("127.0.0.1"), tm, 1)) // Success.
|
|
fmt.Println("3:", limit.Add(net.ParseIP("127.0.0.1"), tm, 1)) // Failure, too many from same ip.
|
|
fmt.Println("4:", limit.Add(net.ParseIP("127.0.0.2"), tm, 1)) // Success, different IP, though nearby.
|
|
fmt.Println("5:", limit.Add(net.ParseIP("127.0.0.2"), tm, 1)) // Failure, hits ipmasked2 check.
|
|
fmt.Println("6:", limit.Add(net.ParseIP("127.0.0.1"), tm.Add(time.Minute), 1)) // Success, in next minute.
|
|
fmt.Println("7:", limit.Add(net.ParseIP("127.0.0.1"), tm.Add(2*time.Minute), 1)) // Success, in another minute.
|
|
fmt.Println("8:", limit.Add(net.ParseIP("127.0.0.1"), tm.Add(3*time.Minute), 1)) // Failure, hitting hourly window for ipmasked1.
|
|
limit.Reset(net.ParseIP("127.0.0.1"), tm.Add(3*time.Minute))
|
|
fmt.Println("9:", limit.Add(net.ParseIP("127.0.0.1"), tm.Add(3*time.Minute), 1)) // Success.
|
|
|
|
// Output:
|
|
// 1: true
|
|
// 2: true
|
|
// 3: false
|
|
// 4: true
|
|
// 5: false
|
|
// 6: true
|
|
// 7: true
|
|
// 8: false
|
|
// 9: true
|
|
}
|