-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(platform): increase broker gateway message throughput
Previously, performance degraded significantly when sending more than 1000 messages simultaneously, since filtering of message acknowledgements per-subscriber scaled worse than linearly. This change introduces a message selector to quickly filter many messages from many subscribers. Instead of a predicate, a key is used to dispatch messages with O(1) complexity to the subscribers. With this optimization the broker gateway is capable of handling 10000 messages under 5 seconds. closes #90
- Loading branch information
1 parent
2f6766e
commit 579c125
Showing
5 changed files
with
322 additions
and
16 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
172 changes: 172 additions & 0 deletions
172
projects/scion/microfrontend-platform/src/lib/client/messaging/message-selector.spec.ts
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,172 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
import {identity, NEVER, Subject} from 'rxjs'; | ||
import {ObserveCaptor} from '@scion/toolkit/testing'; | ||
import {MessageSelector} from './message-selector'; | ||
|
||
describe('Message Selector', () => { | ||
|
||
it('should select message with id `2`', async () => { | ||
// GIVEN | ||
const messages$ = new Subject<{id: string; text: string}>(); | ||
const captor = new ObserveCaptor(); | ||
|
||
// WHEN | ||
const selectMessagesById = new MessageSelector({source$: messages$, keySelector: message => message.id}); | ||
selectMessagesById.select$('2').subscribe(captor); | ||
messages$.next({id: '1', text: 'foo'}); | ||
messages$.next({id: '2', text: 'bar'}); | ||
messages$.next({id: '3', text: 'foobar'}); | ||
|
||
// THEN | ||
expect(captor.getValues()).toEqual([{id: '2', text: 'bar'}]); | ||
expect(captor.hasCompleted()).toBeFalse(); | ||
}); | ||
|
||
it('should select messages with id `2`', async () => { | ||
// GIVEN | ||
const messages$ = new Subject<{id: string; text: string}>(); | ||
const captor = new ObserveCaptor(); | ||
|
||
// WHEN | ||
const selectMessagesById = new MessageSelector({source$: messages$, keySelector: message => message.id}); | ||
selectMessagesById.select$('2').subscribe(captor); | ||
messages$.next({id: '1', text: 'msg-1'}); | ||
messages$.next({id: '2', text: 'msg-2'}); | ||
messages$.next({id: '1', text: 'msg-3'}); | ||
messages$.next({id: '2', text: 'msg-4'}); | ||
|
||
// THEN | ||
expect(captor.getValues()).toEqual([{id: '2', text: 'msg-2'}, {id: '2', text: 'msg-4'}]); | ||
expect(captor.hasCompleted()).toBeFalse(); | ||
}); | ||
|
||
it('should support multiple subscribers on the same key', async () => { | ||
// GIVEN | ||
const messages$ = new Subject<{id: string; text: string}>(); | ||
const captor1 = new ObserveCaptor(); | ||
const captor2 = new ObserveCaptor(); | ||
|
||
// WHEN | ||
const selectMessagesById = new MessageSelector({source$: messages$, keySelector: message => message.id}); | ||
selectMessagesById.select$('1').subscribe(captor1); | ||
selectMessagesById.select$('1').subscribe(captor2); | ||
|
||
messages$.next({id: '1', text: 'foo'}); | ||
messages$.next({id: '2', text: 'bar'}); | ||
|
||
// THEN | ||
expect(captor1.getValues()).toEqual([{id: '1', text: 'foo'}]); | ||
expect(captor1.hasCompleted()).toBeFalse(); | ||
|
||
expect(captor2.getValues()).toEqual([{id: '1', text: 'foo'}]); | ||
expect(captor2.hasCompleted()).toBeFalse(); | ||
}); | ||
|
||
it('should not buffer messages sent before subscription', async () => { | ||
// GIVEN | ||
const messages$ = new Subject<{id: string; text: string}>(); | ||
const captor = new ObserveCaptor(); | ||
|
||
// WHEN | ||
const selectMessagesById = new MessageSelector({source$: messages$, keySelector: message => message.id}); | ||
messages$.next({id: '1', text: 'foo'}); | ||
selectMessagesById.select$('1').subscribe(captor); | ||
|
||
// THEN | ||
expect(captor.getValues()).toEqual([]); | ||
expect(captor.hasCompleted()).toBeFalse(); | ||
}); | ||
|
||
it('should complete when disconnected', async () => { | ||
// GIVEN | ||
const messages$ = new Subject<string>(); | ||
const captor = new ObserveCaptor(); | ||
|
||
// WHEN | ||
const selectMessagesById = new MessageSelector({source$: messages$, keySelector: identity}); | ||
selectMessagesById.select$('foo').subscribe(captor); | ||
selectMessagesById.disconnect(); | ||
messages$.next('foo'); | ||
|
||
// THEN | ||
expect(captor.getValues()).toEqual([]); | ||
expect(captor.hasCompleted()).toBeTrue(); | ||
expect(selectMessagesById.ɵsubscriberCount()).toBe(0); | ||
}); | ||
|
||
it('should complete when the source completes', async () => { | ||
// GIVEN | ||
const source$ = new Subject<string>(); | ||
const captor = new ObserveCaptor(); | ||
|
||
const selectMessagesById = new MessageSelector({source$, keySelector: identity}); | ||
selectMessagesById.select$('1').subscribe(captor); | ||
expect(captor.hasCompleted()).toBeFalse(); | ||
|
||
// WHEN completing the source | ||
source$.complete(); | ||
|
||
// THEN expect source to be completed | ||
expect(captor.hasCompleted()).toBeTrue(); | ||
expect(selectMessagesById.ɵsubscriberCount()).toBe(0); | ||
}); | ||
|
||
it('should error when the source errors', async () => { | ||
// GIVEN | ||
const source$ = new Subject<string>(); | ||
const captor = new ObserveCaptor(); | ||
const selectMessagesById = new MessageSelector({source$, keySelector: identity}); | ||
selectMessagesById.select$('1').subscribe(captor); | ||
|
||
// WHEN erroring the source | ||
source$.error('error'); | ||
|
||
// THEN expect source to be errored | ||
expect(captor.hasErrored()).toBeTrue(); | ||
expect(selectMessagesById.ɵsubscriberCount()).toBe(0); | ||
}); | ||
|
||
it('should unsubscribe when unsubscribing a subscriber', async () => { | ||
// GIVEN | ||
const messageSelector = new MessageSelector({source$: NEVER, keySelector: identity}); | ||
|
||
// WHEN subscribing subscriber 1 | ||
const subscription1 = messageSelector.select$('1').subscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(1); | ||
|
||
// WHEN subscribing subscriber 2 | ||
const subscription2 = messageSelector.select$('1').subscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(2); | ||
|
||
// WHEN subscribing subscriber 3 | ||
const subscription3 = messageSelector.select$('2').subscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(3); | ||
|
||
// WHEN unsubscribing subscriber 1 | ||
subscription1.unsubscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(2); | ||
|
||
// WHEN unsubscribing subscriber 2 | ||
subscription2.unsubscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(1); | ||
|
||
// WHEN unsubscribing subscriber 3 | ||
subscription3.unsubscribe(); | ||
// THEN | ||
expect(messageSelector.ɵsubscriberCount()).toBe(0); | ||
}); | ||
}); |
122 changes: 122 additions & 0 deletions
122
projects/scion/microfrontend-platform/src/lib/client/messaging/message-selector.ts
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,122 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
import {merge, Observable, Subject} from 'rxjs'; | ||
import {takeUntil} from 'rxjs/operators'; | ||
import {Maps} from '@scion/toolkit/util'; | ||
|
||
/** | ||
* Selects items emitted by an Observable according to a static criterion. | ||
* | ||
* This selector was introduced to quickly filter many messages from many subscribers. | ||
* Instead of a predicate, a key is used to dispatch messages with O(1) complexity to the subscribers. | ||
* | ||
* Prior to this selector, performance degraded significantly when sending more than 1000 messages | ||
* simultaneously, since filtering of message acknowledgements per-subscriber scaled worse than linearly. | ||
* | ||
* --- | ||
* ### Usage | ||
* | ||
* ```ts | ||
* // Create message source. | ||
* const messages$ = new Subject<{id: string; text: string}>(); | ||
* | ||
* // Create selector to filter messages by id. | ||
* const selectMessagesById = new MessageSelector({source$: messages$, keySelector: message => message.id}); | ||
* | ||
* // Receive only messages with id '1'. | ||
* selectMessagesById.select$('1').subscribe(msg => { | ||
* // do something | ||
* }); | ||
* | ||
* // Emit messages. | ||
* messages$.next({id: '1', text: 'foo'}); | ||
* messages$.next({id: '2', text: 'bar'}); | ||
* ``` | ||
* | ||
* @internal | ||
*/ | ||
export class MessageSelector<T> { | ||
|
||
private _selectors = new Map<string, Array<Subject<T>>>; | ||
private _sourceError$ = new Subject<never>(); | ||
private _sourceComplete$ = new Subject<void>(); | ||
private _destroy$ = new Subject<void>(); | ||
|
||
/** | ||
* @param config - Controls how to select messages. | ||
*/ | ||
constructor(config: SelectorConfig<T>) { | ||
const {source$, keySelector} = config; | ||
|
||
source$ | ||
.pipe(takeUntil(this._destroy$)) | ||
.subscribe({ | ||
next: item => { | ||
const key = keySelector(item); | ||
this._selectors.get(key)?.forEach(selector => selector.next(item)); | ||
}, | ||
error: error => this._sourceError$.error(error), | ||
complete: () => this._sourceComplete$.next(), | ||
}); | ||
} | ||
|
||
/** | ||
* Selects items emitted by the source Observable that match the given key. | ||
* | ||
* @param key - Specifies the key to select items. | ||
*/ | ||
public select$<R extends T>(key: string): Observable<R> { | ||
return new Observable(observer => { | ||
const selector$ = new Subject<any>(); | ||
Maps.addListValue(this._selectors, key, selector$); | ||
const subscription = merge(selector$, this._sourceError$) | ||
.pipe(takeUntil(this._sourceComplete$)) | ||
.subscribe(observer); | ||
|
||
return () => { | ||
Maps.removeListValue(this._selectors, key, selector$); | ||
subscription.unsubscribe(); | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Returns the current subscriber count. | ||
*/ | ||
public ɵsubscriberCount(): number { | ||
return Array.from(this._selectors.values()).reduce((count, selectors) => count + selectors.length, 0); | ||
} | ||
|
||
/** | ||
* Disconnects this selector from the source Observable. | ||
*/ | ||
public disconnect(): void { | ||
this._destroy$.next(); | ||
} | ||
} | ||
|
||
/** | ||
* Controls how to select items. | ||
* | ||
* @internal | ||
*/ | ||
export interface SelectorConfig<T> { | ||
/** | ||
* Specifies the Observable to select items from. | ||
*/ | ||
source$: Observable<T>; | ||
/** | ||
* Specifies the function to compute the key of an item. | ||
* | ||
* A selector can then subscribe for items matching the key. | ||
*/ | ||
keySelector: (item: T) => string; | ||
} |
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.