gpt4 book ai didi

scala - Spray.can.Http$ConnectionException : Premature connection close

转载 作者:行者123 更新时间:2023-12-04 19:05:33 26 4
gpt4 key购买 nike

在下面的测试中,我尝试模拟超时,然后发送正常请求。但是,我得到了 Spray.can.Http$ConnectionException: Premature connection close (服务器似乎不支持请求流水线)

class SprayCanTest extends ModuleTestKit("/SprayCanTest.conf") with FlatSpecLike with Matchers {

import system.dispatcher

var app = Actor.noSender

protected override def beforeAll(): Unit = {
super.beforeAll()
app = system.actorOf(Props(new MockServer))
}

override protected def afterAll(): Unit = {
system.stop(app)
super.afterAll()
}


"response time out" should "work" in {
val setup = Http.HostConnectorSetup("localhost", 9101, false)

connect(setup).onComplete {
case Success(conn) => {
conn ! HttpRequest(HttpMethods.GET, "/timeout")
}
}

expectMsgPF() {
case Status.Failure(t) =>
t shouldBe a[RequestTimeoutException]
}


}

"normal http response" should "work" in {

//Thread.sleep(5000)
val setup = Http.HostConnectorSetup("localhost", 9101, false)

connect(setup).onComplete {
case Success(conn) => {
conn ! HttpRequest(HttpMethods.GET, "/hello")
}
}

expectMsgPF() {
case HttpResponse(status, entity, _, _) =>
status should be(StatusCodes.OK)
entity should be(HttpEntity("Helloworld"))
}
}

def connect(setup: HostConnectorSetup)(implicit system: ActorSystem) = {
// for the actor 'asks'
import system.dispatcher
implicit val timeout: Timeout = Timeout(1 second)
(IO(Http) ? setup) map {
case Http.HostConnectorInfo(connector, _) => connector
}
}

class MockServer extends Actor {
//implicit val timeout: Timeout = 1.second
implicit val system = context.system

// Register connection service
IO(Http) ! Http.Bind(self, interface = "localhost", port = 9101)

def receive: Actor.Receive = {
case _: Http.Connected => sender ! Http.Register(self)

case HttpRequest(GET, Uri.Path("/timeout"), _, _, _) => {
Thread.sleep(3000)
sender ! HttpResponse(entity = HttpEntity("ok"))
}

case HttpRequest(GET, Uri.Path("/hello"), _, _, _) => {
sender ! HttpResponse(entity = HttpEntity("Helloworld"))
}
}
}


}

和我的测试配置:
spray {
can {
client {
response-chunk-aggregation-limit = 0
connecting-timeout = 1s
request-timeout = 1s
}
host-connector {
max-retries = 0
}
}
}

我发现在这两种情况下,“conn”对象是相同的。
所以我猜当 RequestTimeoutException 发生时,spray 把 conn 放回池中(默认情况下是 4?),下一个案例将使用相同的 conn 但此时,这个 conn 保持事件状态,因此服务器会将其视为分块请求.

如果我在第二种情况下睡一会儿,它就会过去。
所以我想我必须在收到 RequestTimeoutException 时关闭 conn 并确保第二种情况使用全新的连接,对吗?

我应该怎么做?有什么配置吗?

谢谢

莱昂

最佳答案

你不应该在 Actor(你的 MockServer)内部阻塞。当它被阻止时,它无法回复任何消息。您可以将 Thread.sleep 和 response 包装在 Future 中。甚至更好:使用 Akka Scheduler .请务必将发送者分配给 val,因为当您异步响应请求时,它可能会更改。这应该可以解决问题:

val savedSender = sender()
context.system.scheduler.scheduleOnce(3 seconds){
savedSender ! HttpResponse(entity = HttpEntity("ok"))
}

关于scala - Spray.can.Http$ConnectionException : Premature connection close,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com