1

I'm having an issue with a Dart server. I'm trying to send multiple clients some information, but I don't have the information available for them right when they request it. That means I am using a Future and when that Future is complete, I will then send the data back to the client.

The problem, is that the server will not allow a second (or more) client to connect to the server while the first Future is still waiting to be completed.

Here's an example:

import "dart:async";
import "dart:io";
import "package:route/server.dart";

void main(List<String> args) {
  HttpServer.bind("localhost", 5000)
    .then((HttpServer server) {
      Router router = new Router(server)
      ..serve("/multi").listen(_multi);
    });
}

void _multi(HttpRequest request) {
  print("Waiting");
  new Timer(new Duration(seconds: 5), () {
    print("Replying");
    request.response.write("Hello There");
    request.response.close();
  });
}

Basically, if you only have one client connect at a time, this works perfectly fine. If, however, you have more than one client, the first client that connects will block the second client until the first client's connection has been closed.

I've also tried to use ..serve("/multi").asBroadcastStream()..., because I thought that would allow for multiple subscribers, but that had the same behavior.

Is there a way to do this?

Thanks.

Michael Peterson
  • 348
  • 6
  • 22

2 Answers2

4

I'm quite positive that it's not blocking any requests. I think it's your client (a browser?) that queues the connections to only use one socket (probably with keep-alive). This is very normal for browsers, to keep the number of sockets in use, to a minimum.

The Dart server is designed to handle multiple requests at the same time, and the router package is not exception to that.

Anders Johnsen
  • 616
  • 3
  • 7
  • 1
    Yeah I should have mentioned my client is a browser. I believe you are correct. I tried connecting using two Chrome tabs and it was queuing things, but with Chrome and Firefox, it works as I had expected. Thanks. – Michael Peterson Jan 10 '14 at 03:11
1

You can dedicate one isolate to gather the data and several isolates each handling a client request (using strategies discussed in the question you referenced in your comment).

All your request handling isolates can connect to the one data gathering isolate and block until the data is available.
The data gathering isolate can cache the data to serve the same data for each successive request.

I haven't used isolates much yet and I don't know if several isolates (request handler) can connect to the same isolate (data gatherer) using ports, and if this works how this behaves.
If that doesn't work I'm sure several isolates (request handler) can connect to one isolate (data gatherer) using TCP.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567