0

It's a simple thing but i have no idea how i can do it in Scala. I want to return a true in a Future. I use the twitter Future. But I want to return it as fast as possible. Like this:

def saveOrUpdate(pageImpression: PageImpressions): com.twitter.util.Future[Boolean] = {
  return com.twitter.util.Future.value(true)
  count += 1
  println(count)
}

But this wouldn't work. How I can return something but also continuing the execution? It's a HTTP service. The return value will returned to the HTTP client.

TeWu
  • 5,928
  • 2
  • 22
  • 36
Citrullin
  • 2,269
  • 14
  • 29
  • What library do you use for building http service? – maks Jun 04 '16 at 10:16
  • It's not directly http but through it makes it a bit shorter to explain :). I use finagle thrift. I hope this solve a bit this issue or make the performance a bit better: http://stackoverflow.com/questions/37626924/finagle-no-asyncronous-executing – Citrullin Jun 04 '16 at 10:19
  • in Finagle you return Future as response to http request. It means that your servicing thread can immediately acccept another request. Was your question about that? – maks Jun 04 '16 at 10:24
  • But it don't work. It looks more like a synchronous execution. It stops, after 2 or 3 seconds it execute the next block. Nodejs executes 10.000 really fast. And the bottleneck isn't the RAM or CPU or Network. – Citrullin Jun 04 '16 at 10:28
  • Have you configured any concurrency limit for the server? – maks Jun 04 '16 at 10:44
  • Also have you looked into this https://github.com/twitter/finagle/issues/332 ? – maks Jun 04 '16 at 10:46
  • i haven't configured a limit – Citrullin Jun 04 '16 at 11:00
  • i don't understand how this could solve my problem. – Citrullin Jun 04 '16 at 11:02
  • I'm not solving your problem, I try to help you to solve it by yourself. – maks Jun 04 '16 at 11:04
  • That's the best way :) but it doesn't help me to solve this problem. – Citrullin Jun 04 '16 at 11:09
  • What is your problem? – Dima Jun 04 '16 at 13:14

1 Answers1

0

I am not sure I understand what exactly you are looking for, but it seems like you want something like this:

def saveOrUpdate(pageImpression: PageImpressions) {
  FuturePool.unboundedPool { 
     count += 1
     println(count)
  }
  Future.value(true)      
}

It puts a job of incrementing the count and printing it out on a background thread, and returns an immediate future with true.

This is a wrong thing to do in more ways then one however. I don't recommend it at all. First of all, it has unsynchronized access to a mutable variable, which will cause big problems.

And secondly, it returns true unconditionally, regardless of how (or whether) the underlying operation actually completed. If you don't care about result of the execution, you should return (future of) Unit, not Boolean.

Dima
  • 39,570
  • 6
  • 44
  • 70