Hi all :wave: I hope that's the right place to ask...
# spring
y
Hi all 👋 I hope that's the right place to ask this - and if not feel free to redirect me... My manager asked me a question and I had no idea how to answer it as I'd be happy if someone can explain it... He tried to compare 2 endpoints in spring boot, one using coroutines and
delay()
for a very long time and the other not using coroutines and using
Thread.sleep()
, as you can see here:
Copy code
@RestController()
@RequestMapping("users")
class UserController {

    data class User(val name: String)

    @GetMapping(
       value = ["/delay"],
       produces = ["application/json"]
    )
    suspend fun getDelayedUser(@RequestParam(value="name", defaultValue="Jane" ) name: String): User {
        delay(10000)
        return User(name)
    }


    @GetMapping(
        value = ["/sleep"],
        produces = ["application/json"]
    )
    fun getSleepyUser(@RequestParam(value="name", defaultValue="Jane" ) name: String): User {
        Thread.sleep(10000)
        return User(name)
    }
}
The application is configured to use only 1 thread in the thread poll:
Copy code
server.tomcat.accept-count=1
server.tomcat.threads.max=1
server.tomcat.threads.min-spare=0
server.tomcat.connection-timeout=100000
server.tomcat.keep-alive-timeout=100000
And now we're running a benchmark on the suspended endpoint:
Copy code
ab -n 20 -c 10 <http://localhost:8080/users/delay>
The problem we're facing here is that the above command timing out, which is not very expected to me, as the code should be suspended, and the thread should be able to pick up a new request... so... what are we doing wrong here? 🤔
k
I am faaar from expert, but I doubt that tomcat thread pool is used for dispatching coroutines. Did you try using netty? Do you use webflux?
🙏 1
y
Very good catch! I’ve missed it - now it works 😉
k
which one?:)
y
His demo project did not use webflux 🙈