-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
198 additions
and
395 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { Bucket, IBucketPair } from './IBucketPair'; | ||
export { IBuckets } from './IBuckets'; | ||
export { BucketSize } from './consts'; | ||
export { IBucketCalc, BucketCalc } from './BucketCalc'; |
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 was deleted.
Oops, something went wrong.
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,51 @@ | ||
import { WatchableBucket } from './WatchableBucket'; | ||
import { Bucket } from '../Buckets'; | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
import { WatchableBucketFactory } from './WatchableBucketFactory'; | ||
import { ILogger, DefaultLogger } from '../Logger'; | ||
|
||
export class BucketsManager { | ||
private buckets: WatchableBucket[] = []; | ||
private watchableBucketFactory: WatchableBucketFactory; | ||
private logger: ILogger; | ||
|
||
constructor( | ||
watchableBucketFactory: WatchableBucketFactory, | ||
logger: ILogger = new DefaultLogger() | ||
) { | ||
this.watchableBucketFactory = watchableBucketFactory; | ||
this.logger = logger; | ||
} | ||
|
||
public async stop() { | ||
await Promise.all(this.buckets.map(b => b.stop())); | ||
this.buckets = []; | ||
return; | ||
} | ||
|
||
public async update(buckets: Bucket[], callback: BucketWatchCallback) { | ||
this.logger.debug(`Buckets: updating with ${buckets}`); | ||
|
||
const toStart = await Promise.all( | ||
buckets | ||
.filter(b => !this.knownBucket(b)) | ||
.map(b => this.watchableBucketFactory.create(b, callback)) | ||
); | ||
const toSkip = this.buckets.filter(b => buckets.indexOf(b.bucket) > -1); | ||
const toStop = this.buckets.filter(b => buckets.indexOf(b.bucket) === -1); | ||
|
||
const starting = toStart.map(b => b.watch()); | ||
const stopping = toStop.map(b => b.stop()); | ||
|
||
await Promise.all(starting); | ||
await Promise.all(stopping); | ||
|
||
this.buckets = toSkip.concat(toStart); | ||
|
||
this.logger.debug(`Buckets: updated ${this.buckets.map(b => b.bucket)}`); | ||
} | ||
|
||
private knownBucket(bucket: Bucket): boolean { | ||
return this.buckets.some(b => b.bucket === bucket); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,73 +1,63 @@ | ||
import { ILogger, DefaultLogger } from '../Logger'; | ||
import { IBucketPair, Bucket } from '../Buckets'; | ||
import { Bucket } from '../Buckets'; | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
import { IBucketWatcher } from './IBucketWatcher'; | ||
|
||
export class WatchableBucket { | ||
private bucket: IBucketPair; | ||
private bucketNumber: Bucket; | ||
private watcher: any; | ||
private requestFactory: IBucketWatcher; | ||
private logger: ILogger; | ||
private callBack: BucketWatchCallback; | ||
private eventWatchers: {} = {}; | ||
|
||
constructor( | ||
bucket: IBucketPair, | ||
bucket: Bucket, | ||
requestFactory: IBucketWatcher, | ||
callBack: BucketWatchCallback, | ||
logger: ILogger = new DefaultLogger() | ||
) { | ||
this.bucket = bucket; | ||
this.bucketNumber = bucket; | ||
this.requestFactory = requestFactory; | ||
this.callBack = callBack; | ||
this.logger = logger; | ||
} | ||
|
||
public async watch() { | ||
await this.stop(); | ||
|
||
await this.startWatcher(this.bucket.blockBucket); | ||
await this.startWatcher(this.bucket.timestampBucket); | ||
if (this.watcher) { | ||
this.logger.debug(`WatchableBucket: Bucket ${this.bucketNumber} already watched.`); | ||
return; | ||
} | ||
await this.start(); | ||
} | ||
|
||
public async stop() { | ||
await this.stopWatcher(this.bucket.blockBucket); | ||
await this.stopWatcher(this.bucket.timestampBucket); | ||
} | ||
|
||
public equals(newBucket: IBucketPair): boolean { | ||
this.logger.debug( | ||
`Buckets: comparing ${JSON.stringify(this.bucket)} to ${JSON.stringify(newBucket)}` | ||
); | ||
return ( | ||
newBucket.blockBucket === this.bucket.blockBucket && | ||
newBucket.timestampBucket === this.bucket.timestampBucket | ||
); | ||
} | ||
|
||
private async startWatcher(bucket: Bucket): Promise<number> { | ||
try { | ||
const watcher = await this.requestFactory.watchRequestsByBucket(bucket, this.callBack); | ||
this.eventWatchers[bucket] = watcher; | ||
if (this.watcher) { | ||
await this.requestFactory.stopWatch(this.watcher); | ||
this.watcher = null; | ||
|
||
this.logger.debug(`Buckets: Watcher for bucket=${bucket} has been started`); | ||
this.logger.debug(`Buckets: Watcher for bucket=${this.bucketNumber} has been stopped`); | ||
} | ||
} catch (err) { | ||
this.logger.error(`Buckets: Starting bucket=${bucket} watching failed!`); | ||
this.logger.error(`Buckets: Stopping bucket=${this.bucketNumber} watching failed!`); | ||
} | ||
} | ||
|
||
return bucket; | ||
public get bucket(): Bucket { | ||
return this.bucketNumber; | ||
} | ||
|
||
private async stopWatcher(bucket: Bucket) { | ||
private async start() { | ||
try { | ||
const watcher = this.eventWatchers[bucket]; | ||
if (watcher !== undefined) { | ||
await this.requestFactory.stopWatch(watcher); | ||
delete this.eventWatchers[bucket]; | ||
const watcher = await this.requestFactory.watchRequestsByBucket( | ||
this.bucketNumber, | ||
this.callBack | ||
); | ||
this.watcher = watcher; | ||
|
||
this.logger.debug(`Buckets: Watcher for bucket=${bucket} has been stopped`); | ||
} | ||
this.logger.debug(`Buckets: Watcher for bucket=${this.bucketNumber} has been started`); | ||
} catch (err) { | ||
this.logger.error(`Buckets: Stopping bucket=${bucket} watching failed!`); | ||
this.logger.error(`Buckets: Starting bucket=${this.bucketNumber} watching failed!`); | ||
} | ||
} | ||
} |
Oops, something went wrong.