Skip to content

Commit

Permalink
introduce fluent observable and per minute rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ronny1982 committed Aug 17, 2024
1 parent bcaa00b commit 67446a7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions web/src/engine/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ export class Observable<TValue, TOwner = null> implements IObservable<TValue, TO
/**
* Register a {@link callback} that gets invoked whenever the {@link Value} is changed or a notification is forced via {@link Dispatch}.
*/
public Subscribe(callback: SubscriberCallback<TValue, TOwner>): void {
public Subscribe(callback: SubscriberCallback<TValue, TOwner>): typeof this {
this.subscribers.add(callback);
return this;
}

/**
* Unregister the {@link callback} from any further notifications.
*/
public Unsubscribe(callback: SubscriberCallback<TValue, TOwner>): void {
public Unsubscribe(callback: SubscriberCallback<TValue, TOwner>): typeof this {
this.subscribers.delete(callback);
return this;
}
}

Expand Down
8 changes: 8 additions & 0 deletions web/src/engine/taskpool/RateLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export class RateLimit {
public get Throttle() {
return this.amount > 0 && this.interval > 0 ? this.interval * 1000 / this.amount : 0;
}

/**
* Create a new {@link RateLimit} instance.
* @param amount - Maximum number of units within an interval of 60 seconds.
*/
public static PerMinute(amount: number) {
return new RateLimit(amount, 60);
}
}

export const Unlimited = new RateLimit(0, 0);
5 changes: 0 additions & 5 deletions web/src/engine/websites/MangaDex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { MangaScraper, type MangaPlugin, Manga, Chapter, Page } from '../provide
import { TaskPool, Priority } from '../taskpool/TaskPool';
import { RateLimit } from '../taskpool/RateLimit';
import * as Common from './decorators/Common';
import { Numeric } from '../SettingsManager';
import { WebsiteResourceKey as R } from '../../i18n/ILocale';

type CachedManga = {
id: string,
Expand Down Expand Up @@ -101,13 +99,10 @@ const chapterLanguageMap = new Map([
export default class extends MangaScraper {

private readonly api = 'https://api.mangadex.org';
private readonly mangasTaskPool = new TaskPool(1, new RateLimit(2, 1));
private readonly chaptersTaskPool = new TaskPool(1, new RateLimit(4, 1));

public constructor() {
super('mangadex', 'MangaDex', 'https://mangadex.org', Tags.Media.Manga, Tags.Media.Manhwa, Tags.Media.Manhua, Tags.Language.Multilingual, Tags.Source.Aggregator, Tags.Source.Scanlator);
this.Settings.throttle = new Numeric('throttle.mangas', R.Plugin_Settings_ThrottlingInteraction, R.Plugin_Settings_ThrottlingInteractionInfo, 60, 6, 240);
(this.Settings.throttle as Numeric).Subscribe(value => this.mangasTaskPool.RateLimit = new RateLimit(value, 60));
}

public override get Icon() {
Expand Down
5 changes: 2 additions & 3 deletions web/src/engine/websites/ReaperScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ type LaravelLivewireMessage = {
@Common.ImageAjax()
export default class extends DecoratableMangaScraper {

private readonly interactionTaskPool = new TaskPool(1, new RateLimit(15, 60));
private readonly interactionTaskPool = new TaskPool(1, RateLimit.PerMinute(15));

public constructor() {
super('reaperscans', 'Reaper Scans', 'https://reaperscans.com', Tags.Media.Manhwa, Tags.Media.Manhua, Tags.Language.English);
this.Settings.throttle = new Numeric('throttle.interactive', R.Plugin_Settings_ThrottlingInteraction, R.Plugin_Settings_ThrottlingInteractionInfo, 15, 1, 60);
(this.Settings.throttle as Numeric).Subscribe(value => this.interactionTaskPool.RateLimit = new RateLimit(value, 60));
this.Settings.throttle = new Numeric('throttle.interactive', R.Plugin_Settings_ThrottlingInteraction, R.Plugin_Settings_ThrottlingInteractionInfo, 15, 1, 60).Subscribe(value => this.interactionTaskPool.RateLimit = RateLimit.PerMinute(value));
}

public override get Icon() {
Expand Down
6 changes: 2 additions & 4 deletions web/src/engine/websites/TruyenQQ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ function PageExtractor(element: HTMLElement): string {
@Common.ImageAjax()
export default class extends DecoratableMangaScraper {

private readonly interactionTaskPool = new TaskPool(1, new RateLimit(30, 60));
private readonly interactionTaskPool = new TaskPool(1, RateLimit.PerMinute(30));

public constructor() {
super('truyenqq', 'TruyenQQ', 'https://truyenqqviet.com', Tags.Media.Manhwa, Tags.Media.Manhua, Tags.Language.Vietnamese, Tags.Source.Aggregator);
const throttle = new Numeric('throttle.interactive', R.Plugin_Settings_ThrottlingInteraction, R.Plugin_Settings_ThrottlingInteractionInfo, 30, 1, 60);
throttle.Subscribe(value => this.interactionTaskPool.RateLimit = new RateLimit(value, 60));
this.Settings.throttle = throttle;
this.Settings.throttle = new Numeric('throttle.interactive', R.Plugin_Settings_ThrottlingInteraction, R.Plugin_Settings_ThrottlingInteractionInfo, 30, 1, 60).Subscribe(value => this.interactionTaskPool.RateLimit = RateLimit.PerMinute(value));
}

public override get Icon() {
Expand Down

0 comments on commit 67446a7

Please sign in to comment.