-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add new config option for email rate limiting
- Loading branch information
Showing
5 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/didip/tollbooth/v5" | ||
"github.com/didip/tollbooth/v5/limiter" | ||
) | ||
|
||
type Rate struct { | ||
Events float64 `json:"events,omitempty"` | ||
OverTime time.Duration `json:"over_time,omitempty"` | ||
} | ||
|
||
func (r *Rate) EventsPerSecond() float64 { | ||
if int64(r.OverTime) == 0 { | ||
return r.Events | ||
} | ||
|
||
return r.Events / r.OverTime.Seconds() | ||
} | ||
|
||
func (r *Rate) DivideIfDefaultDuration(div float64) *Rate { | ||
if r.OverTime == time.Duration(0) { | ||
return &Rate{ | ||
Events: r.Events / div, | ||
} | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (r *Rate) CreateLimiter() *limiter.Limiter { | ||
overTime := r.OverTime | ||
if int64(overTime) == 0 { | ||
// if r.OverTime is not specified, i.e. the configuration specified just a single float64 number, the | ||
overTime = time.Hour | ||
} | ||
|
||
return tollbooth.NewLimiter(r.EventsPerSecond(), &limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: overTime, | ||
}) | ||
} | ||
|
||
func (r *Rate) Decode(value string) error { | ||
if f, err := strconv.ParseFloat(value, 64); err == nil { | ||
r.Events = f | ||
// r.OverTime remains 0 in this case | ||
return nil | ||
} | ||
parts := strings.Split(value, "/") | ||
if len(parts) != 2 { | ||
return fmt.Errorf("rate: value does not match rate syntax %q", value) | ||
} | ||
|
||
f, err := strconv.ParseFloat(parts[0], 64) | ||
if err != nil { | ||
return fmt.Errorf("rate: events part of rate value %q failed to parse as float64: %w", value, err) | ||
} | ||
|
||
d, err := time.ParseDuration(parts[1]) | ||
if err != nil { | ||
return fmt.Errorf("rate: over-time part of rate value %q failed to parse as duration: %w", value, err) | ||
} | ||
|
||
r.Events = f | ||
r.OverTime = d | ||
|
||
return nil | ||
} | ||
|
||
func (r *Rate) String() string { | ||
if r.OverTime == 0 { | ||
return fmt.Sprintf("%f", r.Events) | ||
} | ||
|
||
return fmt.Sprintf("%f/%s", r.Events, r.OverTime.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package conf | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRateDecode(t *testing.T) { | ||
r := Rate{} | ||
|
||
r = Rate{} | ||
require.NoError(t, r.Decode("123.0")) | ||
require.Equal(t, r, Rate{Events: 123.0, OverTime: 0}) | ||
|
||
r = Rate{} | ||
require.NoError(t, r.Decode("123.0/24h")) | ||
require.Equal(t, r, Rate{Events: 123.0, OverTime: 24 * time.Hour}) | ||
|
||
r = Rate{} | ||
require.Error(t, r.Decode("not a number")) | ||
|
||
r = Rate{} | ||
require.Error(t, r.Decode("123/456/789")) | ||
|
||
r = Rate{} | ||
require.Error(t, r.Decode("123/text")) | ||
} |