Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.5.0 #13

Merged
merged 12 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## 2.0.0

A complete rewritten version of Hamok with a new API and a new architecture.
Expand All @@ -10,4 +9,30 @@ Another accidental publishing of this version before it was ready due to a worki

## 2.2.0

A ready version to use (hopefully)
A ready version to use (hopefully)

### 2.3.0

- Add `JOIN_NOTIFICATION` message type to the message protocol
- Introucing `join()` method

### 2.4.0

- skipped, indicating a significant change applied to the library in the coming version.

### 2.5.0

- Added `STORAGE_HELLO_NOTIFICATION` message type to the message protocol.
- Added `STORAGE_STATE_NOTIFICATION` message type to the message protocol.
- Removed `import` method from `Hamok` in favor of the unified `join` method.
- Removed `export` method from `Hamok` in favor of the unified `join` method.
- Removed `start` method from `Hamok` in favor of the unified `join` method.
- Removed `stop` method from `Hamok` in favor of the `leave` method.
- Added `close` method to `Hamok` to close all resources.
- Added an `ready` promise to all storage components, allowing for awaiting until the storage is ready and initialized.
- Updated `HamokConnection` to join before sending any message except `StorageHello` and `StorageState`.
- Removed the restriction of log gap errors between connected peers. The follower now picks up whatever it can, with the storage responsible for providing up-to-date snapshots to a remote peer through `StorageState` notifications.
- Fixed a bug where a follower would vote for a candidate even if it had a leader.
- Added auto-rejoin functionality for `Hamok` when the connection is lost, provided it was in the joined state.
- Fixed a bug where detached remote peers were not detected if the peer was a follower and lost all connections. (Added `_checkRemotePeers` method for this).
- Added `ready` promise to `Hamok` to await until the `Hamok` is ready and initialized.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ import { Hamok } from 'hamok';
(async () => {
const server_1 = new Hamok();
const server_2 = new Hamok();
const storage_1 = server_1.createMap<string, number>({
mapId: 'my-replicated-storage',
});
const storage_2 = server_2.createMap<string, number>({
mapId: 'my-replicated-storage',
});

server_1.on('message', server_2.accept.bind(server_2));
server_2.on('message', server_1.accept.bind(server_1));

await Promise.all([
server_1.join(),
server_2.join()
server_2.join(),
]);

const storage_1 = server_1.createMap<string, number>({
mapId: 'my-replicated-storage',
});
const storage_2 = server_2.createMap<string, number>({
mapId: 'my-replicated-storage',
});

console.log('Setting value in storage on server_1 for key-1 to 1');
console.log('Setting value in storage on server_2 for key-2 to 2');

await Promise.all([
storage_1.set('key-1', 1),
storage_2.set('key-2', 2),
]);

await Promise.all([
server_1.waitUntilCommitHead(),
server_2.waitUntilCommitHead(),
Expand All @@ -71,8 +71,17 @@ import { Hamok } from 'hamok';
console.log('value for key-2 by server_1:', storage_1.get('key-2'));
console.log('value for key-1 by server_2:', storage_1.get('key-1'));

server_1.stop();
server_2.stop();
await Promise.all([
server_1.leave(),
server_2.leave(),
]);

console.log('Servers left');

// if you want to close resources
server_1.close();
server_2.close();

})();
```

Expand Down
11 changes: 3 additions & 8 deletions docs/emitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ const emitter = hamok.createEmitter<MyEventMap>({
*/
requestTimeoutInMs: 5000,

/**
* Optional. The maximum waiting time in milliseconds for a message to be sent.
* The storage holds back the message sending if Hamok is not connected to a grid or not part of a network.
*
* DEFAULT: 10x requestTimeoutInMs
*/
maxMessageWaitingTimeInMs: 50000,

/**
* Optional. The maximum number of keys allowed in request or response messages.
*
Expand Down Expand Up @@ -96,6 +88,7 @@ A class for managing events and subscriptions in a distributed system.
- `closed`: `boolean` - Indicates whether the emitter is closed.
- `connection`: `HamokConnection<string, string>` - The connection used by the emitter.
- `payloadsCodec`: `Map<keyof T, { encode: (...args: unknown[]) => string, decode: (data: string) => unknown[] }>` - Optional codec for encoding and decoding payloads.
- `ready: Promise<void>` - A promise that resolves when the emitter is initialized and ready to use.

#### Methods

Expand All @@ -107,6 +100,7 @@ A class for managing events and subscriptions in a distributed system.
- **notify**<K extends keyof T>(`event: K`, `...args: T[K]`): `void` - Notifies all subscribed listeners of an event.
- **export**(): `HamokEmitterSnapshot` - Exports the current state of the emitter.
- **import**(`snapshot: HamokEmitterSnapshot`): `void` - Imports the state from a snapshot.
- **sync**(): `Promise<void>` - Synchronizes the emitter state with the remote peers (wait for the commitHead in hamok).

### Events

Expand Down Expand Up @@ -141,6 +135,7 @@ emitter.close();
## Examples

- [simple distributed emitter](https://github.com/balazskreith/hamok-ts/blob/main/examples/src/emitter-example.ts)
- [emitter catchup](https://github.com/balazskreith/hamok-ts/blob/main/examples/src/emitter-catchup-example.ts)

## FAQ

Expand Down
Loading
Loading