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

ZIO resource management built on the core API. #230

Closed
wants to merge 5 commits into from

Conversation

quelgar
Copy link
Contributor

@quelgar quelgar commented Jul 8, 2020

This is a proposal, based on the observation that the only significant difference between the core module and the "high level" module is that core exposes the low-level close methods whereas the high level module offers a ZManaged API. The vast majority of code in the high level module is an exact duplicate of code in core.

This PR attempts to offer the low-level NIO close method—adhering to our unopinionated goal—while allowing the user to opt-in to a bracketed or ZManaged API instead. Any closeable resource from core can be used via bracketing/ZManaged without needing the duplication into a higher level module:

FileChannel.open(path).bracketNio { fileChannel =>
}

val managed: ZManaged[Blocking, IOException, FileChannel] = FileChannel.open(path).toManagedNio

If we were to adopt this approach, we'd follow up by removing the /nio module and renaming /nio-core to /nio.

jczuchnowski
jczuchnowski previously approved these changes Jul 12, 2020
@jczuchnowski jczuchnowski marked this pull request as ready for review July 12, 2020 02:01
@jczuchnowski jczuchnowski dismissed their stale review July 12, 2020 02:02

mistake, haven't checked all changes

@jczuchnowski jczuchnowski marked this pull request as draft July 12, 2020 02:06
* Change most APIs to use `IOException` as the error type instead
  of `Exception`. Non-IO exceptions are now considered defects as
  they only occur due to misuse of the API.
* Similarly change incorrect `Buffer` usage errors to be defects
  instead of failures.
* The `Async*` classes still use `Exception` for the error type
  for now, as the NIO Javadocs are vague as to what kind of
  exceptions to expect from these.
* Rename various read and write methods so they match the
  underlying NIO methods. Seems clearer.
* Add various Scaladocs (more still needed).
* Use `Option` to indicate end-of-stream when reading,
  instead of `-1` magic value.
End-of-stream is now handled in a consistent way across all read APIs.
If a read encounters end-of-stream, the effect will fail with a
`java.io.EOFException`.
All closeable or releasable NIO resources now extend `IOCloseable`.
Extension methods are provided by `CloseableResourceOps` for convenient
access to ZIO bracketing or `ZManaged`.
@jdegoes
Copy link
Member

jdegoes commented Sep 3, 2020

@quelgar There are a lot of changes here, and I heartily approve of most of them. Can you fix the conflicts and we can get this merged in?

@quelgar
Copy link
Contributor Author

quelgar commented Sep 3, 2020

@jdegoes I put this up as a draft just to see what people thought of this approach to resource management, but I haven't kept it up to date. It looks like most of the changes showing here are actually from #228, which this was originally based on. Sorry for the confusion!

#228 has a description of the API cleanup changes, and no conflicts. I can make a new clean PR focused on just resource management after that's in.

Base automatically changed from quelgar/api-cleanup to master September 3, 2020 12:40
@quelgar
Copy link
Contributor Author

quelgar commented Sep 3, 2020

I'll do a new PR that applies cleanly to master soon.

@quelgar quelgar closed this Sep 3, 2020
@quelgar quelgar deleted the quelgar/managed-close branch September 23, 2020 04:48
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