This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Exit JVM if an error is thrown or the main loop exits #119
Open
ANeumann82
wants to merge
3
commits into
master
Choose a base branch
from
an/clean-exit-keepalive-framework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -4,14 +4,16 @@ import java.net.URL | |
import akka.NotUsed | ||
import akka.actor.{Actor, ActorRef, ActorSystem, Props, Stash, Status} | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.model.headers.{Authorization, HttpCredentials} | ||
import akka.http.scaladsl.model._ | ||
import akka.http.scaladsl.model.headers.{Authorization, HttpCredentials} | ||
import akka.http.scaladsl.settings.ConnectionPoolSettings | ||
import akka.stream.Materializer | ||
import akka.stream.scaladsl.Flow | ||
import akka.util.Timeout | ||
import com.typesafe.scalalogging.StrictLogging | ||
|
||
import scala.concurrent.duration._ | ||
import scala.concurrent.{Await, ExecutionContext, Future, TimeoutException} | ||
import scala.util.{Failure, Success} | ||
|
||
/** | ||
|
@@ -25,7 +27,7 @@ import scala.util.{Failure, Success} | |
* @param authorization A [[CredentialsProvider]] if the connection is secured. | ||
*/ | ||
case class Session(url: URL, streamId: String, authorization: Option[CredentialsProvider] = None)( | ||
implicit askTimout: Timeout) { | ||
implicit askTimout: Timeout) extends StrictLogging { | ||
|
||
/** | ||
* Construct a new [[HttpRequest]] for a serialized Mesos call and a set of authorization, ie session token. | ||
|
@@ -52,6 +54,28 @@ case class Session(url: URL, streamId: String, authorization: Option[Credentials | |
Flow[Array[Byte]].map(createPostRequest(_, None)).via(connection) | ||
} | ||
|
||
private def poolSettings():Future[ConnectionPoolSettings] = Future { | ||
// Constructs the connection pool settings with defaults and overrides the max connections and pipelining limit so | ||
// that only one request at a time is processed. See https://doc.akka.io/docs/akka-http/current/configuration.html | ||
// for details. | ||
// *IMPORTANT*: DO NOT CHANGE maxConnections OR pipeliningLimit! Otherwise, USI won't guarantee request order to Mesos! | ||
ConnectionPoolSettings("").withMaxConnections(1).withPipeliningLimit(1) | ||
}(ExecutionContext.global) | ||
|
||
private def poolSettingsWithTimeout():ConnectionPoolSettings = { | ||
// We create the pool settings with a timeout: It seems by default, the creation of the settings requires a reverse | ||
// dns lookup, which may take more than 5 seconds which triggers the akka streams subscription timeout. | ||
// If we let the creation here take the full time, we won't start without any reasonable logging message | ||
try { | ||
Await.result(poolSettings(), 2.seconds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get this. I'm sorry. Why can we not document the fact that apps should increase the Akka stream subscription timeout? It feels we are just introducing complexity here. |
||
} catch { | ||
case e: TimeoutException => { | ||
logger.error("Failed to create ConnectionPoolSettings. Is your reverse DNS lookup slow?", e) | ||
throw e | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A connection flow factory that will create a flow that only processes one request at a time. | ||
* | ||
|
@@ -60,19 +84,15 @@ case class Session(url: URL, streamId: String, authorization: Option[Credentials | |
* @return A connection flow for single requests. | ||
*/ | ||
private def connection(implicit system: ActorSystem, mat: Materializer): Flow[HttpRequest, HttpResponse, NotUsed] = { | ||
// Constructs the connection pool settings with defaults and overrides the max connections and pipelining limit so | ||
// that only one request at a time is processed. See https://doc.akka.io/docs/akka-http/current/configuration.html | ||
// for details. | ||
// *IMPORTANT*: DO NOT CHANGE maxConnections OR pipeliningLimit! Otherwise, USI won't guarantee request order to Mesos! | ||
val poolSettings = ConnectionPoolSettings("").withMaxConnections(1).withPipeliningLimit(1) | ||
val settings = poolSettingsWithTimeout() | ||
|
||
Flow[HttpRequest] | ||
.map(_ -> NotUsed) | ||
.via(if (Session.isSecured(url)) { | ||
Http() | ||
.newHostConnectionPoolHttps(host = url.getHost, port = Session.effectivePort(url), settings = poolSettings) | ||
.newHostConnectionPoolHttps(host = url.getHost, port = Session.effectivePort(url), settings = settings) | ||
} else { | ||
Http().newHostConnectionPool(host = url.getHost, port = Session.effectivePort(url), settings = poolSettings) | ||
Http().newHostConnectionPool(host = url.getHost, port = Session.effectivePort(url), settings = settings) | ||
}) | ||
.map { | ||
case (Success(response), NotUsed) => response | ||
|
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.
What is the reason for this being a future?