-
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
2,261 additions
and
2,041 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ITxRequestRaw } from '../Types/ITxRequest'; | ||
|
||
export type BucketWatchCallback = (request: ITxRequestRaw) => void; |
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,59 @@ | ||
import { WatchableBucket } from './WatchableBucket'; | ||
import { IBuckets, IBucketPair } from '../Buckets'; | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
import { WatchableBucketFactory } from './WatchableBucketFactory'; | ||
|
||
export class Buckets { | ||
private buckets: WatchableBucket[] = []; | ||
private watchableBucketFactory: WatchableBucketFactory; | ||
|
||
constructor(watchableBucketFactory: WatchableBucketFactory) { | ||
this.watchableBucketFactory = watchableBucketFactory; | ||
} | ||
|
||
public async update(newBuckets: IBuckets, callback: BucketWatchCallback) { | ||
if (this.stopped) { | ||
await this.addBucket(newBuckets.currentBuckets, callback); | ||
await this.addBucket(newBuckets.nextBuckets, callback); | ||
await this.addBucket(newBuckets.afterNextBuckets, callback); | ||
} else { | ||
if (this.currentBucket.equals(newBuckets.currentBuckets)) { | ||
return; | ||
} | ||
|
||
await this.shift(); | ||
await this.addBucket(newBuckets.afterNextBuckets, callback); | ||
} | ||
} | ||
|
||
public async stop() { | ||
while (!this.stopped) { | ||
await this.shift(); | ||
} | ||
} | ||
|
||
private async push(bucket: WatchableBucket) { | ||
await bucket.watch(); | ||
this.buckets.push(bucket); | ||
} | ||
|
||
private async shift() { | ||
const bucket = this.buckets.shift(); | ||
if (bucket) { | ||
await bucket.stop(); | ||
} | ||
} | ||
|
||
private async addBucket(bucketPair: IBucketPair, callback: BucketWatchCallback) { | ||
const bucket = await this.watchableBucketFactory.create(bucketPair, callback); | ||
await this.push(bucket); | ||
} | ||
|
||
private get stopped() { | ||
return this.buckets.length === 0; | ||
} | ||
|
||
private get currentBucket(): WatchableBucket { | ||
return this.buckets[0]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
|
||
export interface IBucketWatcher { | ||
watchRequestsByBucket(bucket: number, callBack: BucketWatchCallback): Promise<any>; | ||
stopWatch(watcher: any): Promise<void>; | ||
} |
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,70 @@ | ||
import { ILogger, DefaultLogger } from '../Logger'; | ||
import { IBucketPair, Bucket } from '../Buckets'; | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
import { IBucketWatcher } from './IBucketWatcher'; | ||
|
||
export class WatchableBucket { | ||
private bucket: IBucketPair; | ||
private requestFactory: IBucketWatcher; | ||
private logger: ILogger; | ||
private callBack: BucketWatchCallback; | ||
private eventWatchers: {} = {}; | ||
|
||
constructor( | ||
bucket: IBucketPair, | ||
requestFactory: IBucketWatcher, | ||
callBack: BucketWatchCallback, | ||
logger: ILogger = new DefaultLogger() | ||
) { | ||
this.bucket = 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); | ||
} | ||
|
||
public async stop() { | ||
await this.stopWatcher(this.bucket.blockBucket); | ||
await this.stopWatcher(this.bucket.timestampBucket); | ||
} | ||
|
||
public equals(newBucket: IBucketPair): boolean { | ||
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; | ||
|
||
this.logger.debug(`Buckets: Watcher for bucket=${bucket} has been started`); | ||
} catch (err) { | ||
this.logger.error(`Buckets: Starting bucket=${bucket} watching failed!`); | ||
} | ||
|
||
return bucket; | ||
} | ||
|
||
private async stopWatcher(bucket: Bucket) { | ||
try { | ||
const watcher = this.eventWatchers[bucket]; | ||
if (watcher !== undefined) { | ||
await this.requestFactory.stopWatch(watcher); | ||
delete this.eventWatchers[bucket]; | ||
|
||
this.logger.debug(`Buckets: Watcher for bucket=${bucket} has been stopped`); | ||
} | ||
} catch (err) { | ||
this.logger.error(`Buckets: Stopping bucket=${bucket} watching failed!`); | ||
} | ||
} | ||
} |
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,22 @@ | ||
import { WatchableBucket } from './WatchableBucket'; | ||
import { IBucketWatcher } from './IBucketWatcher'; | ||
import { IBucketPair } from '../Buckets'; | ||
import { BucketWatchCallback } from './BucketWatchCallback'; | ||
import { ILogger } from '../Logger'; | ||
|
||
export class WatchableBucketFactory { | ||
private requestFactory: Promise<IBucketWatcher>; | ||
private logger: ILogger; | ||
|
||
constructor(requestFactory: Promise<IBucketWatcher>, logger: ILogger) { | ||
this.requestFactory = requestFactory; | ||
this.logger = logger; | ||
} | ||
|
||
public async create( | ||
bucketPair: IBucketPair, | ||
callback: BucketWatchCallback | ||
): Promise<WatchableBucket> { | ||
return new WatchableBucket(bucketPair, await this.requestFactory, callback, this.logger); | ||
} | ||
} |
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
Oops, something went wrong.