Skip to content

Commit

Permalink
fix(Sentinel): unreachable errors when sentinals are healthy
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 7, 2018
1 parent 27b408e commit 7bf6fea
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 269 deletions.
267 changes: 0 additions & 267 deletions lib/connectors/SentinelConnector.ts

This file was deleted.

43 changes: 43 additions & 0 deletions lib/connectors/SentinelConnector/SentinelIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {ISentinelAddress} from './types'

function isSentinelEql (a: ISentinelAddress, b: ISentinelAddress): boolean {
return ((a.host || '127.0.0.1') === (b.host || '127.0.0.1')) &&
((a.port || 26379) === (b.port || 26379))
}

export default class SentinelIterator {
private cursor: number = 0

constructor (private sentinels: ISentinelAddress[]) {}

hasNext (): boolean {
return this.cursor < this.sentinels.length
}

next (): ISentinelAddress | null {
return this.hasNext() ? this.sentinels[this.cursor++] : null
}

reset (success: boolean): void {
if (success && this.sentinels.length > 1 && this.cursor !== 1) {
const remains = this.sentinels.slice(this.cursor - 1)
this.sentinels = remains.concat(this.sentinels.slice(0, this.cursor - 1))
}
this.cursor = 0
}

add (sentinel: ISentinelAddress): boolean {
for (let i = 0; i < this.sentinels.length; i++) {
if (isSentinelEql(sentinel, this.sentinels[i])) {
return false
}
}

this.sentinels.push(sentinel)
return true
}

toString (): string {
return JSON.stringify(this.sentinels)
}
}
Loading

0 comments on commit 7bf6fea

Please sign in to comment.