https://github.com/dflemstr/tokamak
https://github.com/dflemstr/tokamak
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/dflemstr/tokamak
- Owner: dflemstr
- License: mit
- Created: 2018-07-16T15:25:43.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-18T11:29:11.000Z (almost 8 years ago)
- Last Synced: 2025-01-26T10:45:51.972Z (over 1 year ago)
- Language: Java
- Size: 75.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `tokamak` - An experimental async runtime library
The Tokamak library offers asynchronous execution of synchronous-looking code (similar to
`async/await` syntax in other programming languages). It does so by intelligently retrying the
code when asynchronous operations complete. The core idea is that async operations are expensive
and synchronous code is cheap.
The main entry point is `Tokamak.run()`:
```java
Tokamak.run(() -> {
// synchronous code here
});
```
Within the Tokamak context, you can write synchronous-looking code as usual, and use
`Tokamak.await()` when you want to execute asynchronous code. For example:
```java
final int userId = 42;
// User.fetchName returns a CompletionStage
final String userName = await(User.fetchName(userId));
// User.fetchAge returns a CompletionStage
final int userAge = await(User.fetchAge(userId));
System.out.printf("User %s has age %d%n", userName, userAge);
```
Since the code is retried until all async operations succeed, you need to mark all side-effects
that should only be performed once using `Tokamak.once()`:
```java
final int userId = 42;
final String userName = await(User.fetchName(userId));
once(this::incrementFetchNameMetric);
final int userAge = await(User.fetchAge(userId));
once(this::incrementFetchAgeMetric);
System.out.printf("User %s has age %d%n", userName, userAge);
```