mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 17:22:26 +00:00
ban: more defaults options, simpler callback
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
@ -7,46 +7,60 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
type ActionCallback func(action Action, actor string, rule *Rule) error
|
||||
|
||||
type Action int
|
||||
|
||||
const (
|
||||
Jailed Action = iota
|
||||
Released
|
||||
)
|
||||
|
||||
func (a Action) String() string {
|
||||
switch a {
|
||||
case Jailed:
|
||||
return "Jailed"
|
||||
case Released:
|
||||
return "Released"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
type Rule struct {
|
||||
Name string
|
||||
Message string
|
||||
Code codes.Code
|
||||
StrikeLimit int
|
||||
ExpireBase time.Duration
|
||||
Sentence time.Duration
|
||||
// WhenJailed is an optional function to call when an Actor isJailed
|
||||
WhenJailed func(actor string, r *Rule) error
|
||||
// WhenTimeServed is an optional function to call when an Actor is released because of timeServed
|
||||
WhenTimeServed func(actor string, r *Rule) error
|
||||
Name string
|
||||
Message string
|
||||
Code codes.Code
|
||||
StrikeLimit int
|
||||
JailDuration time.Duration
|
||||
// Callback is an optional function to call when an Actor isJailed or released because of timeServed
|
||||
Callback ActionCallback
|
||||
}
|
||||
|
||||
type action struct {
|
||||
whenJailed func(actor string, r *Rule) error
|
||||
whenTimeServed func(actor string, r *Rule) error
|
||||
fn ActionCallback
|
||||
}
|
||||
|
||||
func (a2 *action) WhenJailed(a *badactor.Actor, r *badactor.Rule) error {
|
||||
if a2.whenJailed != nil {
|
||||
return a2.whenJailed(a.Name(), &Rule{
|
||||
Name: r.Name,
|
||||
Message: r.Message,
|
||||
StrikeLimit: r.StrikeLimit,
|
||||
ExpireBase: r.ExpireBase,
|
||||
Sentence: r.Sentence,
|
||||
})
|
||||
if a2.fn == nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return a2.fn(Jailed, a.Name(), &Rule{
|
||||
Name: r.Name,
|
||||
Message: r.Message,
|
||||
StrikeLimit: r.StrikeLimit,
|
||||
JailDuration: r.ExpireBase,
|
||||
})
|
||||
}
|
||||
|
||||
func (a2 *action) WhenTimeServed(a *badactor.Actor, r *badactor.Rule) error {
|
||||
if a2.whenTimeServed != nil {
|
||||
return a2.whenTimeServed(a.Name(), &Rule{
|
||||
Name: r.Name,
|
||||
Message: r.Message,
|
||||
StrikeLimit: r.StrikeLimit,
|
||||
ExpireBase: r.ExpireBase,
|
||||
Sentence: r.Sentence,
|
||||
})
|
||||
if a2.fn == nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return a2.fn(Released, a.Name(), &Rule{
|
||||
Name: r.Name,
|
||||
Message: r.Message,
|
||||
StrikeLimit: r.StrikeLimit,
|
||||
JailDuration: r.ExpireBase,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user