Commit 2df2c546 authored by Felipe Andrade's avatar Felipe Andrade

ban / isbanned

parent 06e42cbd
...@@ -203,23 +203,22 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller ...@@ -203,23 +203,22 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller
// UpdateBackend refreshes the consensus state of a single backend // UpdateBackend refreshes the consensus state of a single backend
func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) { func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) {
_, _, _, _, bannedUntil := cp.getBackendState(be) if cp.IsBanned(be) {
if time.Now().Before(bannedUntil) { log.Debug("skipping backend banned", "backend", be.Name)
log.Debug("skipping backend banned", "backend", be.Name, "bannedUntil", bannedUntil)
return return
} }
// if backend it not online or not in a health state we'll only resume checkin it after ban // if backend it not online or not in a health state we'll only resume checkin it after ban
if !be.Online() || !be.IsHealthy() { if !be.Online() || !be.IsHealthy() {
log.Warn("backend banned - not online or not healthy", "backend", be.Name, "bannedUntil", bannedUntil) log.Warn("backend banned - not online or not healthy", "backend", be.Name)
bannedUntil = time.Now().Add(cp.banPeriod) cp.Ban(be)
} }
// if backend it not in sync we'll check again after ban // if backend it not in sync we'll check again after ban
inSync, err := cp.isInSync(ctx, be) inSync, err := cp.isInSync(ctx, be)
if err != nil || !inSync { if err != nil || !inSync {
log.Warn("backend banned - not in sync", "backend", be.Name, "bannedUntil", bannedUntil) log.Warn("backend banned - not in sync", "backend", be.Name)
bannedUntil = time.Now().Add(cp.banPeriod) cp.Ban(be)
} }
// if backend exhausted rate limit we'll skip it for now // if backend exhausted rate limit we'll skip it for now
...@@ -363,6 +362,22 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) { ...@@ -363,6 +362,22 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) {
log.Debug("group state", "proposedBlock", proposedBlock, "consensusBackends", strings.Join(consensusBackendsNames, ", "), "filteredBackends", strings.Join(filteredBackendsNames, ", ")) log.Debug("group state", "proposedBlock", proposedBlock, "consensusBackends", strings.Join(consensusBackendsNames, ", "), "filteredBackends", strings.Join(filteredBackendsNames, ", "))
} }
// IsBanned checks if a specific backend is banned
func (cp *ConsensusPoller) IsBanned(be *Backend) bool {
bs := cp.backendState[be]
defer bs.backendStateMux.Unlock()
bs.backendStateMux.Lock()
return time.Now().Before(bs.bannedUntil)
}
// Ban bans a specific backend
func (cp *ConsensusPoller) Ban(be *Backend) {
bs := cp.backendState[be]
defer bs.backendStateMux.Unlock()
bs.backendStateMux.Lock()
bs.bannedUntil = time.Now().Add(cp.banPeriod)
}
// Unban remove any bans from the backends // Unban remove any bans from the backends
func (cp *ConsensusPoller) Unban() { func (cp *ConsensusPoller) Unban() {
for _, be := range cp.backendGroup.Backends { for _, be := range cp.backendGroup.Backends {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment