Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/griffio/kotlin-coroutine-racing
kotlin coroutine racing and fail fast happy eyeballs
https://github.com/griffio/kotlin-coroutine-racing
coroutines happyeyeballs kotlin ktor-client okhttp
Last synced: 8 days ago
JSON representation
kotlin coroutine racing and fail fast happy eyeballs
- Host: GitHub
- URL: https://github.com/griffio/kotlin-coroutine-racing
- Owner: griffio
- Created: 2023-10-30T16:56:27.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-18T08:41:46.000Z (12 months ago)
- Last Synced: 2024-04-16T18:27:01.563Z (7 months ago)
- Topics: coroutines, happyeyeballs, kotlin, ktor-client, okhttp
- Language: Kotlin
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## More Kotlin Racing Ambiguous Coroutines
https://griffio.github.io/programming/2023/03/18/More-Kotlin-Racing-Ambiguous-Coroutines/
**Make staggered requests** to http resources with delayed responses and return the first coroutine to respond, cancelling the rest if already running
This is similar to [HappyEyeBalls](https://www.rfc-editor.org/rfc/rfc8305) where finding the quickest connection can be made with concurrency
Use [Ktor http client](https://ktor.io/docs/create-client.html) with OkHttp engine or [Jdk async HttpClient](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html) using coroutine await extension
Send http requests to https://httpbin.org/ or run locally `docker run -p 80:80 kennethreitz/httpbin`
Requests will be run concurrently in random order with a staggered start until the first response
The next request will be run if the previous task fails or exceeds the delay (e.g. 800ms)
Each response is delayed (in seconds) before returning body from server
```
http://localhost/delay/7
800ms -> http://localhost/delay/9
800ms -> http://localhost/delay/3
win <------------ 800ms -> http://localhost/delay/1
800ms -> http://localhost/delay/5
``````
http://localhost/delay/7
http://localhost/delay/9
http://localhost/delay/3
http://localhost/delay/1
http://localhost/delay/5
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Charset": "UTF-8",
"Accept-Encoding": "gzip",
"Connection": "Keep-Alive",
"Host": "localhost",
"User-Agent": "Ktor client"
},
"origin": "192.168.65.1",
"url": "http://localhost/delay/1"
}...in 3 second/s
```