Skip to content

Instantly share code, notes, and snippets.

@georgeOsdDev
Created April 27, 2015 03:33
Show Gist options
  • Save georgeOsdDev/bf42dedf126395f0f272 to your computer and use it in GitHub Desktop.
Save georgeOsdDev/bf42dedf126395f0f272 to your computer and use it in GitHub Desktop.
respond chunl
package quickstart.action
import java.util.concurrent.TimeUnit
import scala.util.Random
import io.netty.channel.{ChannelFuture, ChannelFutureListener}
import io.netty.util.{HashedWheelTimer, TimerTask, Timeout}
import xitrum.{Action, SkipCsrfCheck}
import xitrum.annotation.GET
@GET("/a")
class Chunk1 extends Action with SkipCsrfCheck {
val timer = new HashedWheelTimer
def execute() {
response.headers.set("content-disposition", "attachment; filename='test.dat'")
setChunked()
timer.start()
def xxx(count: Int) {
timer.newTimeout(new TimerTask {
def run(timeout: Timeout) {
if (count == 200) {
respondLastChunk()
} else {
log.debug("xxx " + count)
val buff = new Array[Byte](10*1024*1024)
Random.nextBytes(buff)
respondBinary(buff)
xxx(count + 1)
}
}
}, 10, TimeUnit.MILLISECONDS)
}
xxx(0)
}
}
@GET("/b")
class Chunk2 extends Action with SkipCsrfCheck {
val timer = new HashedWheelTimer
def execute() {
response.headers.set("content-disposition", "attachment; filename='test.dat'")
setChunked()
timer.start()
def xxx(count: Int) {
timer.newTimeout(new TimerTask {
def run(timeout: Timeout) {
if (count == 200) {
if (channel.isOpen) respondLastChunk()
} else {
log.debug("xxx " + count)
val buff = new Array[Byte](10*1024*1024)
Random.nextBytes(buff)
if (channel.isOpen) {
respondBinary(buff)
xxx(count + 1)
}
}
}
}, 10, TimeUnit.MILLISECONDS)
}
xxx(0)
}
}
@GET("/c")
class Chunk3 extends Action with SkipCsrfCheck {
def execute() {
response.headers.set("content-disposition", "attachment; filename='test.dat'")
setChunked()
def xxx(count: Int) {
if (count == 200) {
respondLastChunk()
} else {
log.debug("xxx " + count)
val buff = new Array[Byte](10*1024*1024)
Random.nextBytes(buff)
val f = respondBinary(buff)
f.addListener(new ChannelFutureListener {
def operationComplete(f: ChannelFuture) {
if (f.isCancelled() || !f.isSuccess()) {
log.debug("xxx abort")
} else {
xxx(count + 1)
}
}
})
}
}
xxx(0)
}
}
[WARN] An exception was thrown by TimerTask.
java.lang.OutOfMemoryError: Java heap space
at quickstart.action.Chunk1$$anon$2.run(SiteIndex.scala:36) ~[classes/:na]
at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:581) ~[netty-all-4.0.26.Final.jar:4.0.26.Final]
at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:655) [netty-all-4.0.26.Final.jar:4.0.26.Final]
at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:367) [netty-all-4.0.26.Final.jar:4.0.26.Final]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
[WARN] An exception was thrown by TimerTask.
java.lang.OutOfMemoryError: Java heap space
at quickstart.action.Chunk2$$anon$3.run(SiteIndex.scala:63) ~[classes/:na]
at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:581) ~[netty-all-4.0.26.Final.jar:4.0.26.Final]
at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:655) [netty-all-4.0.26.Final.jar:4.0.26.Final]
at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:367) [netty-all-4.0.26.Final.jar:4.0.26.Final]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
curl -o /dev/null http://localhost:8000/a
curl -o /dev/null http://localhost:8000/b
curl -o /dev/null http://localhost:8000/c
curl --limit-rate 10k -o /dev/null http://localhost:8000/a
curl --limit-rate 10k -o /dev/null http://localhost:8000/b
curl --limit-rate 10k -o /dev/null http://localhost:8000/c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment