mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-05 18:56:24 +00:00
Adphi
c7096975b1
health: set services serving on start and not available on close Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package ban
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jaredfolkins/badactor"
|
|
"google.golang.org/grpc/codes"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
type action struct {
|
|
whenJailed func(actor string, r *Rule) error
|
|
whenTimeServed func(actor string, r *Rule) error
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
return nil
|
|
}
|