- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 akka http doc 中拿了这个简单的例子:
http://doc.akka.io/docs/akka-http/current/scala/http/client-side/request-level.html
我稍微修改了一下以请求一百个请求。应用程序在 32 个请求后阻塞(默认 max-open-requests
配置)。
为什么?
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
import akka.util.ByteString
import scala.io.StdIn
object AkkaClientExample extends App {
val system: ActorSystem = ActorSystem("BatchAkka")
try {
val unformattedAddresses = (1 to 100).map(i => s"Rue de la Gracieuse $i, Préverenges, Switzerland")
val googleGeocoder = system.actorOf(GoogleGeocoder.props, "GoogleGeocoder")
unformattedAddresses.foreach(e => googleGeocoder ! GoogleGeocoder.GeoCode(e))
println(">>> Press ENTER to exit <<<")
StdIn.readLine()
} finally {
system.terminate()
}
}
object GoogleGeocoder {
def props: Props = Props[GoogleGeocoder]
final case class GeoCode(unformattedAddress: String)
}
class GoogleGeocoder extends Actor with ActorLogging {
import GoogleGeocoder._
import akka.pattern.pipe
import context.dispatcher
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
val http = Http(context.system)
def receive = {
case GeoCode(unformattedAddress) =>
log.info(s"GeoCode $unformattedAddress")
http
.singleRequest(HttpRequest(uri = url(unformattedAddress)))
.map(r => (unformattedAddress, r))
.pipeTo(self)
case (unformattedAddress: String, resp @ HttpResponse(StatusCodes.OK, headers, entity, _)) =>
log.info(s"Success response comming for $unformattedAddress")
entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
val response = body.utf8String.replaceAll("\\s+", " ").take(50)
log.info(s"Success response for $unformattedAddress: $response")
}
case (unformattedAddress: String, resp @ HttpResponse(code, _, _, _)) =>
log.info(s"Request failed, response code: $code for $unformattedAddress")
resp.discardEntityBytes()
}
def url(unformattedAddress: String): String =
//s"https://maps.googleapis.com/maps/api/geocode/json?address=${URLEncoder.encode(unformattedAddress, "UTF-8")}&key=${URLEncoder.encode(googleApiKey, "UTF-8")}"
s"https://www.epfl.ch/"
}
[INFO] [07/28/2017 20:08:26.977] [BatchAkka-akka.actor.default-dispatcher-4] [akka://BatchAkka/user/GoogleGeocoder] GeoCode Rue de la Gracieuse 1, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.080] [BatchAkka-akka.actor.default-dispatcher-4] [akka://BatchAkka/user/GoogleGeocoder] GeoCode Rue de la Gracieuse 2, Préverenges, Switzerland
...
[INFO] [07/28/2017 20:08:27.098] [BatchAkka-akka.actor.default-dispatcher-13] [akka://BatchAkka/user/GoogleGeocoder] GeoCode Rue de la Gracieuse 99, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.098] [BatchAkka-akka.actor.default-dispatcher-13] [akka://BatchAkka/user/GoogleGeocoder] GeoCode Rue de la Gracieuse 100, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.615] [BatchAkka-akka.actor.default-dispatcher-11] [akka://BatchAkka/user/GoogleGeocoder] Success response comming for Rue de la Gracieuse 1, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.620] [BatchAkka-akka.actor.default-dispatcher-11] [akka://BatchAkka/user/GoogleGeocoder] Success response comming for Rue de la Gracieuse 4, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.668] [BatchAkka-akka.actor.default-dispatcher-17] [akka://BatchAkka/user/GoogleGeocoder] Success response for Rue de la Gracieuse 4, Préverenges, Switzerland: <!doctype html><html lang="fr" class="no-js"><head
[INFO] [07/28/2017 20:08:27.668] [BatchAkka-akka.actor.default-dispatcher-21] [akka://BatchAkka/user/GoogleGeocoder] Success response for Rue de la Gracieuse 1, Préverenges, Switzerland: <!doctype html><html lang="fr" class="no-js"><head
...
[INFO] [07/28/2017 20:08:27.787] [BatchAkka-akka.actor.default-dispatcher-5] [akka://BatchAkka/user/GoogleGeocoder] Success response comming for Rue de la Gracieuse 31, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.795] [BatchAkka-akka.actor.default-dispatcher-15] [akka://BatchAkka/user/GoogleGeocoder] Success response comming for Rue de la Gracieuse 32, Préverenges, Switzerland
[INFO] [07/28/2017 20:08:27.802] [BatchAkka-akka.actor.default-dispatcher-16] [akka://BatchAkka/user/GoogleGeocoder] Success response for Rue de la Gracieuse 31, Préverenges, Switzerland: <!doctype html><html lang="fr" class="no-js"><head
[INFO] [07/28/2017 20:08:27.806] [BatchAkka-akka.actor.default-dispatcher-17] [akka://BatchAkka/user/GoogleGeocoder] Success response for Rue de la Gracieuse 32, Préverenges, Switzerland: <!doctype html><html lang="fr" class="no-js"><head
class GoogleGeocoder extends Actor with ActorLogging {
import GoogleGeocoder._
import akka.pattern.pipe
import context.dispatcher
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
val http = Http(context.system)
val queue = new scala.collection.mutable.Queue[String]
var currentRequests = 0
val MaxCurrentRequest = 10
def receive = {
case GeoCode(unformattedAddress) =>
if (currentRequests < MaxCurrentRequest)
query(unformattedAddress)
else
queue += unformattedAddress
case (unformattedAddress: String, resp @ HttpResponse(StatusCodes.OK, headers, entity, _)) =>
log.info(s"Success response comming for $unformattedAddress")
entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
currentRequests = currentRequests - 1
queryNext()
val response = body.utf8String.replaceAll("\\s+", " ").take(50)
log.info(s"Success response for $unformattedAddress: $response")
}
case (unformattedAddress: String, resp @ HttpResponse(code, _, _, _)) =>
log.info(s"Request failed, response code: $code for $unformattedAddress")
resp.discardEntityBytes()
currentRequests = currentRequests - 1
queryNext()
case f: Status.Failure =>
log.info("failure" + textSample(f))
case m =>
log.info("unexpected message: " + textSample(m))
}
def query(unformattedAddress: String) {
log.info(s"GeoCode $unformattedAddress")
http
.singleRequest(HttpRequest(uri = url(unformattedAddress)))
.map(r => (unformattedAddress, r))
.pipeTo(self)
}
def queryNext() {
if (queue.nonEmpty) {
query(queue.dequeue)
}
}
def url(unformattedAddress: String): String =
//s"https://maps.googleapis.com/maps/api/geocode/json?address=${URLEncoder.encode(unformattedAddress, "UTF-8")}&key=${URLEncoder.encode(googleApiKey, "UTF-8")}"
s"https://www.epfl.ch/"
}
http.singleRequest
产生一个失败的 future ,
currentRequests
不会减少。我可以在
case f: Status.Failure
上处理这个问题,但是,这个解决方案看起来很容易出错。
AkkaClientExample
:
unformattedAddresses.foreach(e => googleGeocoder ! GoogleGeocoder.GeoCode(e))
在到达
MaxCurrentRequest
时被阻塞)?
最佳答案
如果您使用 akka.logging = DEBUG
运行您的示例,您会注意到以下输出:InputBuffer (max-open-requests = 32) now filled with 31 request after enqueuing GET / Empty
InputBuffer (max-open-requests = 32) now filled with 32 request after enqueuing GET / Empty
InputBuffer (max-open-requests = 32) exhausted when trying to enqueue GET / Empty
InputBuffer (max-open-requests = 32) exhausted when trying to enqueue GET / Empty
InputBuffer (max-open-requests = 32) exhausted when trying to enqueue GET / Empty
有相当a comprehensive description akka-http 如何处理客户端请求的池化,但简而言之,如果你用超过 max-open-requests 来重载池,它将开始丢弃请求:
http
.singleRequest(HttpRequest(uri = url(unformattedAddress)))
.map(r => (unformattedAddress, r)) // <- HERE
.pipeTo(self)
http
.singleRequest(HttpRequest(uri = url(unformattedAddress)))
.onComplete {
case Success(r) =>
self ! (unformattedAddress, r)
case Failure(ex) =>
log.error(ex, "pool overflow")
}
def url(addr: String) = "http://httpbin.org/headers"
implicit val system: ActorSystem = ActorSystem("BatchAkka")
implicit val mat: ActorMaterializer = ActorMaterializer()
import system.dispatcher
val http = Http()
val addresses = (1 to 100).map(i => s"Rue de la Gracieuse $i, Préverenges, Switzerland")
Source(addresses)
.mapAsync(4)(addr => http.singleRequest(HttpRequest(uri = url(addr))))
.map(response => println(response.status))
.runWith(Sink.seq)
.map(_ => println("done"))
关于max-open-requests 后 akka 客户端 http 被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379930/
OpenAL.org && 创意开发网站已关闭。我选择替代版本 OpenAL Soft .我很担心,因为在 OpenAL Soft 的二进制安装中我找不到 alut.h header 。 alut.h
我使用 Android Studio 已经有一段时间了,但有一天应用程序突然出错了。当我尝试单击我的目录以查找要导入或打开的文件时,应用程序变得异常缓慢并且根本没有响应。当我最终成功切换到存储我的文件
自 Firefox 4 以来,这似乎是一个奇怪的功能变化。在使用 window.open() 打开一个窗口后,当用鼠标中键单击打开的窗口中的链接时(或右键单击并选择“在新窗口中打开”选项卡') 导致链
我无法从 Open::URI 的 rdoc 中得知当我这样做时返回的是什么: result = open(url) URL 返回 XML,但我如何查看/解析 XML? 最佳答案 open 返回一个 I
经常开发asp但对于细致的说法,真实不太清楚,这里简单的介绍下。 一般情况下 读取数据都是用rs.open sql,conn,1,1 修改数据:rs.open sql,conn,1,3 删除
关于 pathlib 标准库中的模块,是 path.open() 方法只是内置 open() 的“包装器”功能? 最佳答案 如果您阅读了 source code的 pathlib.Path.open你
我想将 Open Liberty 运行时的语言更改为 en_US从 Eclipse IDE 中,但我不知道如何。 也尝试使用 JVM 参数的首选项来设置它,但它没有用。 -Duser.language
这是我所拥有的: 参数“opener”未在可能的函数调用参数中列出。这是 PyCharm 错误还是其他原因? PyCharm 2018.3.5 社区版,Windows 7 上的 Python 3.6.
我正在使用 Tinkerpop 的 GraphFactory.open(Configuration 配置) Java 命令来访问 Neo4j 数据库。 一个最低限度的工作示例是: Configurat
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 我没有使用过 with 语句,但
我正在玩 python 3.5 中的 open 函数。我不明白 opener 参数(最后一个参数)在 open 函数中的用法。根据 python 文档:可以通过将可调用对象作为打开器传递来使用自定义打
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 5 年前。 Improve th
我试图用 Python 来做一些模拟 3D 声音的工作。我试图运行此代码(答案中提供):Python openAL 3D sound类似,两次都收到: ModuleNotFoundError: No
我一直认为 open 和 io.open 可以互换。 显然不是,如果我相信这个片段: import ctypes, io class POINT(ctypes.Structure): _fie
这个问题在这里已经有了答案: What's the difference between io.open() and os.open() on Python? (7 个答案) 关闭 9 年前。 我是
我正在尝试更好地了解 WCF 的一些内部工作原理。我已经做了相当多的环顾四周,但我无法找到关于 ChannelFactory.Open() 与 IClientChannel.Open() 相比的明确解
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 我知道有很多关于在 python
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章adodb.recordset.open(rs.open)方法参数详解由
不久前我遇到了一个interesting security hole Link 看起来足够无害,但有一个漏洞,因为默认情况下,正在打开的页面允许打开的页面通过 window.opener 回调到它。有
这在我的应用程序上运行良好,但由于某种原因我无法让它在这里正常工作。无论如何,我的问题是,当我单击列表标题时,我想关闭之前打开的列表标题并仅保留事件的列表标题打开。目前它会打开我点击的所有内容,但也会
我是一名优秀的程序员,十分优秀!