Created
March 12, 2018 22:51
-
-
Save vhutov/c751538ea5b1ea15cbd70a803bfd2b57 to your computer and use it in GitHub Desktop.
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
class GenericAction[V <: Message]( | |
val coreComponents: CoreComponents, | |
val attributes: Attributes[V], | |
val publisher: Publisher[V], | |
val consumer: Consumer, | |
val throttled: Boolean, | |
val next: Action | |
) extends ExitableAction with NameGen { | |
val statsEngine = coreComponents.statsEngine | |
def name: String = genName(s"${publisher.name}->${consumer.name}") | |
def execute(session: Session): Unit = { | |
attributes.requestName(session).flatMap { | |
requestName => | |
logger.trace("Executing request {}", requestName) | |
val outcome = sendRequest(session, requestName) | |
outcome.onFailure { errorMessage => | |
statsEngine.reportUnbuildableRequest(session, requestName, errorMessage) | |
} | |
outcome | |
} | |
} | |
private def sendRequest(session: Session, requestName: String): Validation[Unit] = { | |
val value = attributes.payload(session) | |
value.map {value => | |
logger.trace("Sending record id={}", value.id) | |
val requestStartDate = nowMillis | |
publisher.send(value) | |
.onComplete(awaitResult(value, session, requestName, requestStartDate)) | |
} | |
} | |
private def awaitResult(value: V, session: Session, requestName: String, startDate: Long): PartialFunction[Try[Unit], Unit] = { | |
case f: Failure[Unit] => logResponse(session, requestName, startDate)(f) | |
case Success(_) => | |
consumer.await(value) | |
.andThen(logResponse(session, requestName, startDate)) | |
.onComplete { _ => | |
if (throttled) { | |
coreComponents.throttler.throttle(session.scenario, () => next ! session) | |
} else { | |
next ! session | |
} | |
} | |
} | |
private def logResponse(session: Session, requestName: String, startDate: Long): PartialFunction[Try[Unit], Unit] = { | |
case result: Try[Unit] => | |
val responseEndDate = nowMillis | |
statsEngine.logResponse( | |
session, | |
requestName, | |
ResponseTimings(startDate, responseEndDate), | |
if (result.isSuccess) OK else KO, | |
None, | |
if (result.isSuccess) None else Some(result.failed.get.getMessage) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment