-
Notifications
You must be signed in to change notification settings - Fork 3.6k
str feat: RestartSourceWithContext #32178
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35b962f
RestartSourceWithContext
leviramsey 5ce23db
doubled line?
leviramsey f6015d1
Update akka-stream/src/main/scala/akka/stream/scaladsl/RestartSourceW…
leviramsey 3a7bad0
javadsl
leviramsey 4073869
Merge branch 'restart-source-with-context' of github.com:leviramsey/a…
leviramsey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
akka-stream/src/main/scala/akka/stream/javadsl/RestartSourceWithContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright (C) 2023 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.stream.javadsl | ||
|
|
||
| import akka.NotUsed | ||
| import akka.japi.function.Creator | ||
| import akka.stream.RestartSettings | ||
| import akka.stream.scaladsl | ||
|
|
||
| /** | ||
| * A RestartSourceWithContext wraps a [[SourceWithContext]] that gets restarted when it completes or fails. | ||
| * | ||
| * They are useful for graphs that need to run for longer than the [[SourceWithContext]] can necessarily guarantee it will, | ||
| * e.g. for [[SourceWithContext]] streams that depend on a remote service to which connectivity may be lost (crash or partition). The RestartSourceWithContext ensures that the graph can continue running while the SourceWithContext restarts. | ||
| */ | ||
| object RestartSourceWithContext { | ||
|
|
||
| /** | ||
| * Wrap the given [[SourceWithContext]] with a SourceWithContext that will restart it when it fails or completes using an exponential backoff. | ||
| * | ||
| * The returned [[SourceWithContext]] will not emit a complete or failure as long as maxRestarts is not reached, since the completion or failure of the wrapped SourceWithContext is handled by restarting it. The wrapped SourceWithContext can however be canceled by canceling the returned SourceWithContext. When that happens, the wrapped SourceWithContext will be canceled and will not be restarted. | ||
| * | ||
| * @param settings [[RestartSettings]] defining the restart configuration | ||
| * @param sourceFactory A factory for producing the SourceWithContext to wrap | ||
| */ | ||
| def withBackoff[T, C]( | ||
| settings: RestartSettings, | ||
| sourceFactory: Creator[SourceWithContext[T, C, _]]): SourceWithContext[T, C, NotUsed] = { | ||
| val underlyingFactory = () => sourceFactory.create().asScala | ||
| new SourceWithContext(scaladsl.RestartSourceWithContext.withBackoff(settings)(underlyingFactory)) | ||
| } | ||
|
|
||
| /** | ||
| * Wrap the given [[SourceWithContext]] with a SourceWithContext that will restart it when it fails using an exponential backoff. | ||
| * | ||
| * The returned [[SourceWithContext]] will not emit a failure as long as maxRestarts is not reached, since the failure of the wrapped SourceWithContext is handled by restarting it. The wrapped SourceWithContext can however be canceled by canceling the returned SourceWithContext. When that happens, the wrapped SourceWithContext if currently running will be canceled and will not be restarted. | ||
| * | ||
| * @param settings [[RestartSettings]] defining the restart configuration | ||
| * @param sourceFactory A factory for producing the SourceWithContext to wrap | ||
| */ | ||
| def onFailuresWithBackoff[T, C]( | ||
| settings: RestartSettings, | ||
| sourceFactory: Creator[SourceWithContext[T, C, _]]): SourceWithContext[T, C, NotUsed] = { | ||
| val underlyingFactory = () => sourceFactory.create().asScala | ||
| new SourceWithContext(scaladsl.RestartSourceWithContext.onFailuresWithBackoff(settings)(underlyingFactory)) | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
akka-stream/src/main/scala/akka/stream/scaladsl/RestartSourceWithContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright (C) 2023 Lightbend Inc. <https://www.lightbend.com> | ||
| */ | ||
|
|
||
| package akka.stream.scaladsl | ||
|
|
||
| import akka.NotUsed | ||
| import akka.stream.RestartSettings | ||
|
|
||
| /** | ||
| * A RestartSourceWithContext wraps a [[SourceWithContext]] that gets restarted when it completes or fails. | ||
| * | ||
| * They are useful for graphs that need to run for longer than the [[SourceWithContext]] can necessarily guarantee it will, | ||
| * e.g. for [[SourceWithContext]] streams that depend on a remote service to which connectivity may be lost (crash or partition). The RestartSourceWithContext ensures that the graph can continue running while the [[SourceWithContext]] restarts. | ||
| */ | ||
| object RestartSourceWithContext { | ||
|
|
||
| /** | ||
| * Wrap the given [[SourceWithContext]] with a [[SourceWithContext]] that will restart it when it fails or completes using an exponential backoff. | ||
| * | ||
| * The returned [[SourceWithContext]] will not emit a complete or failure as long as maxRestarts is not reached, since the completion or failure of the wrapped [[SourceWithContext]] is handled by restarting it. The wrapped [[SourceWithContext]] can however be canceled by canceling the returned [[SourceWithContext]]. When that happens, the wrapped [[SourceWithContext]] if currently running will be canceled and will not be restarted. | ||
| * | ||
| * @param settings [[RestartSettings]] defining restart configuration | ||
| * @param sourceFactory A factory for producing the [[SourceWithContext]] to wrap | ||
| */ | ||
| def withBackoff[T, C](settings: RestartSettings)( | ||
| sourceFactory: () => SourceWithContext[T, C, _]): SourceWithContext[T, C, NotUsed] = { | ||
| val underlyingFactory = () => sourceFactory().asSource | ||
| SourceWithContext.fromTuples( | ||
| Source.fromGraph(new RestartWithBackoffSource(underlyingFactory, settings, onlyOnFailures = false))) | ||
| } | ||
|
|
||
| /** | ||
| * Wrap the given [[SourceWithContext]] with a [[SourceWithContext]] that will restart it when it fails using an exponential backoff. | ||
| * | ||
| * The returned [[SourceWithContext]] will not emit a failure as long as maxRestarts is not reached, since the failure of the wrapped [[SourceWithContext]] is handled by restarting it. The wrapped [[SourceWithContext]] can however be canceled by canceling the returned [[SourceWithContext]]. When that happens, the wrapped [[SourceWithContext]] if currently running will be canceled and will not be restarted. | ||
| * | ||
| * @param settings [[RestartSettings]] defining restart configuration | ||
| * @param sourceFactory A factory for producing the [[SourceWithContext]] to wrap | ||
| */ | ||
| def onFailuresWithBackoff[T, C](settings: RestartSettings)( | ||
| sourceFactory: () => SourceWithContext[T, C, _]): SourceWithContext[T, C, NotUsed] = { | ||
| val underlyingFactory = () => sourceFactory().asSource | ||
| SourceWithContext.fromTuples( | ||
| Source.fromGraph(new RestartWithBackoffSource(underlyingFactory, settings, onlyOnFailures = true))) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
akka.japi.functioninterfaces are parallel with the JDK stdlib ones but add the capability to throw checked, for example for usage in actor message handling. That's not important here, so let's go withjava.util.function.Supplierinstead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only used
Creatorto mirror the otherRestart*factories... suppose there should be an issue to move those toSupplier(which would be binary incompat, but source compat) in 2.10?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, let's keep it aligned then 👍