Skip to content

Cleanup combinators - #1102

Merged
fwbrasil merged 9 commits into
getkyo:mainfrom
johnhungerford:cleanup-combinators
Apr 3, 2025
Merged

fwbrasil merged 9 commits into
getkyo:mainfrom
johnhungerford:cleanup-combinators

Conversation

@johnhungerford

Copy link
Copy Markdown
Collaborator

Problem

  1. Kyo-combinators is getting a bit bloated
  2. Retries currently only work for Abort[Throwable]
  3. Some of the repeat logic can be improved (using Loop, e.g.)

Solution

  1. Break apart Combinators.scala into separate files according to effect type
  2. Integrate retry methods into abort-combinators
    • retry(s: Schedule), retry(i: Int), and retryForever for the entire Abort union
    • forAbort[E].retry(s: Schedule), forAbort[E].retry(i: Int), and forAbort[E].retryForever for a specific error type
    • retryForever (new) handles Abort completely
  3. Using Loop for all repeat logic

Notes

  • Also added Kyo.sleepForever which I've found useful in a couple of cases

* @return
* A computation that produces the result of this computation with Async and Resource effects
*/
inline def forkScoped(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a similar method in Async

* Asynchronous computation with handled Emit that can fail with Abort[Closed]
*/
def emitToChannel(channel: Channel[A])(using Tag[A], Flat[B], Frame): B < (S & Async & Abort[Closed]) =
effect.foreachEmit(a => channel.put(a))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could use a batched channel put

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for an unchunked emit, though

* @return
* A computation that produces the result of this computation
*/
def debugTrace(using Frame): A < S = Debug.trace(effect)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! :)

* @return
* A computation that produces the result of this computation with Async effect
*/
def repeat(backoff: Int => Duration, limit: Int)(using Flat[A], Frame): A < (S & Async) =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have similar methods in Clock.repeat* now. It'd be nice to follow the same semantics and naming.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some updates:

  • Got rid of some of the more complicated methods, as these can be accomplished with Clock and clutter the namespace
  • Changed the names of the two remaining repeat overloads that use Duration and return Async with repeatAtInterval
  • Kept the simpler repeat and repeatWhile/repeatUntil, since these don't use Duration/Schedule/Async and so don't really reflect the Clock API

* @return
* Stream representation of original effect
*/
def emitToStream: Stream[A, S] = Stream(effect)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be inline? Should help make sure it's suspended.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I don't understand how that ensures it's suspended

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream.apply is inlined and takes a by-name function. That'd require a separate group of extension methods where effect is also declared as inline. I'm not sure it's worth it, especially given that "someValue.doSomething" typically implies the value being evaluated before the method call. We have pipe that breaks this assumption but I think it's more justifiable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're correct. Extension methods do break this assumption.

* @return
* A computation that produces the result of this computation
*/
def debugValue(using Frame): A < S = Debug(effect)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just debug?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That creates a name class with the debug package, which I'd like to avoid.

* A computation that produces the result of the last successful execution before the condition becomes false
*/
def repeatWhile[S1](fn: A => Boolean < S1)(using Flat[A], Frame): A < (S & S1) =
Loop(()): _ =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to think of a signature for this common pattern...

Comment on lines +242 to +243
def ensuring(finalizer: => Any < Async)(using Frame): A < (S & Resource & IO) =
Resource.ensure(finalizer).andThen(effect)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if ensure should have an ensureWith as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add an issue for this

@johnhungerford

Copy link
Copy Markdown
Collaborator Author

I think latest updates address all PR review comments

@fwbrasil
fwbrasil merged commit 7bfdbf9 into getkyo:main Apr 3, 2025
fwbrasil added a commit that referenced this pull request Jun 25, 2026
A STARTTLS/TLS client on the io_uring backend could place its handshake
Finished flight and its first post-upgrade application record on two
concurrent send SQEs for the same fd. io_uring can reap those out of
order (liburing #1102), reordering the bytes on the wire, so the peer
read a byte-shifted record and aborted the connection with a
bad_record_mac fatal alert. This made a kyo-net STARTTLS client fail
against a standard TLS server (reproduced deterministically against
openssl) and flaked the cross-backend STARTTLS test under load.

The handshake's final flight is sent through the raw send tail
(rawPending/rawSendInFlight, while handle.tls is still Absent) while
post-upgrade application data uses the TLS send tail
(pendingCipher/sendInFlight); the two single-in-flight guards were
independent, so a TLS send could be submitted while a raw send was still
in flight. The raw->TLS transition is one-way (handle.tls becomes
Present at completion and every later write routes through writeTls), so
flushTls now defers while the raw tail is in flight or has unsent bytes,
and onRawSendComplete flushes the TLS tail once the raw tail drains. At
most one send SQE per fd is in flight across the transition.

Also fail, rather than strand, a TLS engine read op that throws on both
posix drivers: the op held a pending read promise, so a bare escape hung
the connection to the leaf timeout instead of tearing it down with the
engine error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants