我试图了解Play 2.0框架的反应性I /O概念。为了从一开始就更好地理解,我决定跳过框架的助手来构造不同种类的迭代器,并Iteratee从头开始编写一个自定义,以供aBodyParser解析请求正文使用。
从Iteratees和ScalaBodyParser文档中的可用信息以及关于播放反应式I / O的两个演示开始,这就是我想出的:
import play.api.mvc._ import play.api.mvc.Results._ import play.api.libs.iteratee.{Iteratee, Input} import play.api.libs.concurrent.Promise import play.api.libs.iteratee.Input.{El, EOF, Empty} 01 object Upload extends Controller { 02 def send = Action(BodyParser(rh => new SomeIteratee)) { request => 03 Ok("Done") 04 } 05 } 06 07 case class SomeIteratee(state: Symbol = 'Cont, input: Input[Array[Byte]] = Empty, received: Int = 0) extends Iteratee[Array[Byte], Either[Result, Int]] { 08 println(state + " " + input + " " + received) 09 10 def fold[B]( 11 done: (Either[Result, Int], Input[Array[Byte]]) => Promise[B], 12 cont: (Input[Array[Byte]] => Iteratee[Array[Byte], Either[Result, Int]]) => Promise[B], 13 error: (String, Input[Array[Byte]]) => Promise[B] 14 ): Promise[B] = state match { 15 case 'Done => { println("Done"); done(Right(received), Input.Empty) } 16 case 'Cont => cont(in => in match { 17 case in: El[Array[Byte]] => copy(input = in, received = received + in.e.length) 18 case Empty => copy(input = in) 19 case EOF => copy(state = 'Done, input = in) 20 case _ => copy(state = 'Error, input = in) 21 }) 22 case _ => { println("Error"); error("Some error.", input) } 23 } 24 }
(备注:所有这些东西对我来说都是新事物,因此,如果这完全是废话,请原谅。)Iteratee非常愚蠢,它只读取所有块,对接收到的字节数求和并打印一些消息。当我用一些数据调用控制器动作时,一切都会按预期进行-我可以观察到Iteratee接收到所有块,并且在读取所有数据时,它将切换为完成状态,请求结束。
现在,我开始研究代码,因为我想看看这两种情况的行为:
17 case in: El[Array[Byte]] => copy(state = if(received + in.e.length > 10000) 'Error else 'Cont, input = in, received = received + in.e.length)
所以我只是添加了一个条件,如果接收到超过10000个字节,则会切换到错误状态。我得到的输出是这样的:
'Cont Empty 0 'Cont El([B@38ecece6) 8192 'Error El([B@4ab50d3c) 16384 Error Error Error Error Error Error Error Error Error Error Error
然后,请求将永远挂起并且永远不会结束。我对上述文档的期望是,当我error在foldIteratee中调用函数时,应停止处理。这里发生的是,Iteratee的fold方法在error被调用之后被多次调用-好,然后请求挂起。
当我在读取所有输入之前切换到完成状态时,其行为非常相似。将第15行更改为:
15 case 'Done => { println("Done with " + input); done(if (input == EOF) Right(received) else Left(BadRequest), Input.Empty) }
第17行:
17 case in: El[Array[Byte]] => copy(state = if(received + in.e.length > 10000) 'Done else 'Cont, input = in, received = received + in.e.length)
产生以下输出:
'Cont Empty 0 'Cont El([B@16ce00a8) 8192 'Done El([B@2e8d214a) 16384 Done with El([B@2e8d214a) Done with El([B@2e8d214a) Done with El([B@2e8d214a) Done with El([B@2e8d214a)
然后请求将永远挂起。
我的主要问题是为什么在上述情况下该请求被挂起。如果有人可以阐明这一点,我将不胜感激!
您的理解是完全正确的,我已将修复方法推向高手:
https://github.com/playframework/Play20/commit/ef70e641d9114ff8225332bf18b4dd995bd39bcc
修复了Iteratees中的两种情况以及例外情况。
在案例类中很好地使用副本来进行Iteratee BTW。