httpswift / swifter

Tiny http server engine written in Swift programming language.
BSD 3-Clause "New" or "Revised" License
3.9k stars 539 forks source link

usleep degrades when using Swifter #521

Open monkekode opened 2 years ago

monkekode commented 2 years ago

I have the following server:

import SwiftUI
import Swifter

@main
struct MacInputServer: App {
    let server: HttpServer;

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }

    func sleep(req: HttpRequest) -> HttpResponse {
        usleep(100_000);
        return .ok(.text(""))
    }

    init() {
        server = HttpServer()
        server["/sleep"] = sleep
        try! server.start(999, forceIPv4: true, priority: DispatchQoS.QoSClass.userInteractive);
    }
}

I'm running while true; do time curl localhost:999/sleep; done to test it. The first ~100 requests finish in about 120ms as expected. But after that it starts hanging for up to 10 seconds:

curl localhost:999/sleep  0.01s user 0.01s system 11% cpu 0.118 total
curl localhost:999/sleep  0.01s user 0.01s system 0% cpu 10.121 total
curl localhost:999/sleep  0.01s user 0.01s system 0% cpu 5.134 total
curl localhost:999/sleep  0.00s user 0.01s system 0% cpu 5.902 total

If I just sleep in a loop, the performance stays consistent, so this is definitely the fault of Swifter. How can this be fixed?