Are user requests multi-threaded?

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

pep_castefa
Posts: 45
Joined: 01 Jun 2010, 10:31
Location: Barcelona, Spain

Are user requests multi-threaded?

Postby pep_castefa » 11 Apr 2013, 15:11

I have developed an application which is working but not supporting many users (about 40 users and it's down). I'm trying to find why it's failing and just come to a doubt about how Smartfox manages the requests of users.
In my application, when the user makes a request to an extension (it is a game... the user makes a request to the extension to get some kind of response from it), the extension calls a method from a private object it has instantiated when the extension is started (init() method). This object has its logic, generates the result, and returns it to the extension... the extension returns the response to the user as usual.
This is basically the logic and, when some users are connected, it begins responding very slowly.

I have to say that, in the logic of this private object in the extension, I have a call to a Web Service which takes about 1'5 seconds to get the response.
My question is about how the client requests are treated:
- Is the extension single threaded (I mean... when it gets a request, it doesn't handle the next request until the first one is finished)?
- Is the private class handling requests one by one or is it handling them in parallel?
- Do i have to create a thread for each user request which handles the call to the private's class method?

To make things more clear, I wil try to write the program logic in pseudo-code:

Code: Select all

public class gameExt extends AbstractExtension{

   private MyLogicClass _logicClass;

   public void init(){
     ...
     _logicClass = new MyLogicClass();
     ...
   }

   public void handleRequest(String cmd, String[] params, User user, int fromRoom){
     ...
     if (cmd.equals("xxx")){
        String result = _logicClass.myMethod();
        ...
        //Code to return the result to the client
     }
     ...
   }
}


Code: Select all

public class MyLogicClass(){
   public String myMethod(){
      ...
      //Whatever code for the logic
     [call to a web service] //This takes about 1'5 seconds
      ...
     return result;
   }
}


Thank you in advance for any clue or comment
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Are user requests multi-threaded?

Postby rjgtav » 11 Apr 2013, 18:14

Hello,

By default SFS uses only 1 thread for the ExtensionHandler, which is the responsible for executing the extension's code.
You can change that value on the <ExtHandlerThreads> node of the server's config.xml, and then restart it for the changes to take effect.

Don't forget to make sure that your code works well in a multi threaded environment, otherwise this could break your application's state.

I have to say that, in the logic of this private object in the extension, I have a call to a Web Service which takes about 1'5 seconds to get the response.

One to five seconds is a bit too slow, specially if that method is called quite often. If you could move that code to an extension it would be great, as the execution time would decrease a lot, which would let your threads handle more requests/sec.

Cheers
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
pep_castefa
Posts: 45
Joined: 01 Jun 2010, 10:31
Location: Barcelona, Spain

Re: Are user requests multi-threaded?

Postby pep_castefa » 12 Apr 2013, 08:25

Hi rjgtav, and thank you for your response...
As you said, if all extensions are executed by one thread, I suppose the problem may happen when the extension is handling a user request and it executes the method in the private class (_logicClass in my pseudo-code sample), which, in turn, calls the web service that takes 1'5 seconds... I suppose the extension has to wait until this reuqest is handled and can't handle subsequent requests until the private class '_logicClass' returns the response... is it this way?
I don't think there would be any problem incrementing the number of threads for <ExtHandlerThreads>, but I don't think this would be the solution (don't you think?)... I mean, perhaps incrementing the number of threads to, for example 2, would improve a bit (perhaps would support the double of users?), but I won't to know which is the real solution for this problem...

One solution could be executing the _logicClass method in a separate thread? What would happen then? I mean... the extension can't handle any more user requests until the one it is handling isn't finalized because if it did, wouldn't it mix the responses to the users? How would it know which result goes to which user?
Or do you mean that the thread itself sends the rsponse to the user? Can this be done?

Thank you in advance! :D
pep_castefa
Posts: 45
Joined: 01 Jun 2010, 10:31
Location: Barcelona, Spain

Re: Are user requests multi-threaded?

Postby pep_castefa » 15 Apr 2013, 09:25

Hi again... I have been looking on a solution on how to optimize all the logic, but reading on this article:

http://www.smartfoxserver.com/docs/1x/i ... Safety.htm

It seems the solution is just to increase the number of threads assigned to extension handlers... would this be a solution? What is the way to calculate how many threads would be good to assign to extension handlers?

@rjgtav: I don't quite undersatnd your comment:
One to five seconds is a bit too slow, specially if that method is called quite often. If you could move that code to an extension it would be great, as the execution time would decrease a lot, which would let your threads handle more requests/sec.

I mean... I understand that the response time when calling htis web service is slow... what I don't understand is "move that code to an extension", as it is in an extension... well, not exactly, it is in a method of a class owned by that extension. Could you explain thi s alittle further? Thank you!
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Are user requests multi-threaded?

Postby rjgtav » 23 Apr 2013, 22:23

Hello,

I'm very sorry, I've missed your post, and only realized today that it was unanswered.

Regarding your first post, you should not need to execute your class on a separate thread, as that could also lead to some problems later, so it is better if you just increase the amount of threads for the extension handler.

Regarding your second post, for calculating the amount of threads you would need, you can use this method. If your task takes 5s to execute and you want to, for example, handle 100 requests / second, then you'll need 100/5 = 20 threads in order to accomplish that.
On my comment, I was suggesting to maybe run the code which is being run on the Webservice on the extension, for example if it is a DB call you can use the DBManager instead which is much quicker, but it also depends on the webservice and what you're doing with it. Can you better describe it? Maybe we can find a better way of setting up your logic.

Cheers
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
pep_castefa
Posts: 45
Joined: 01 Jun 2010, 10:31
Location: Barcelona, Spain

Re: Are user requests multi-threaded?

Postby pep_castefa » 24 Apr 2013, 08:20

Hi rjgtav... Now I am a little bit confused... in the Smartfox documentation, one solution is the one your're describing (increasing the number of threads assigned to SF extensions), but, at the end, it says this one is not the recommended:

An alternative to this approach (that we recommend) is to run your own thread(s) in the extension in order to handle the blocking/time-consuming operations: this way you can reduce the amount of synchronization needed in you server side code and you don't affect the other extensions running in the server.


So... by what I understand (correct me if Im wrong), in the documentation, for my specific problem, it is recommending handling the user requests sent to an extension with my own threads (creating one thread per user request... in this thread, I would have the logic and the calls to the costly web service).
But in your post, you are recommending not doing that and, instead, increasing the number of threads assigned to extension handlers...

Which do you think is the correct path? :shock:

Answering to your comment about if I could move the code in the web service, it is not possible. That code is in an external web service because the logic in that web service (and the data in the database which it accesses) is external and not accessible... it would be great if I could access the logic in the web service myself, but it is not possible in this specific case...

Well... thank you again for your support and any help will be greatly appreciated
eloipoch
Posts: 1
Joined: 24 Apr 2013, 12:03

Re: Are user requests multi-threaded?

Postby eloipoch » 24 Apr 2013, 13:41

Hi rjgtav,

But I think that, without take into account that the SmartFox docs says, in a real world you do not know what is quantity of requests per second you would need to handle because it depends on the quantity of users.

You can try if with 5 or 10 or 20 (or 1000) of threads the application works fine but if in a week you have a peak of users, you would need more, and if after that, the quantity of users decrease, you will need to tune it again and again and again.

Thanks in advance!! :)
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Are user requests multi-threaded?

Postby rjgtav » 24 Apr 2013, 15:31

Hello again,

Regarding the multi-thread topic, I wasn't remembering of that documentation section, thanks for reminding. I'm not much experienced on server-side developing for bigger projects on SFS Pro, only for SFS2X. I thought it would not be advisable as you would be creating and destroying threads all the time, maybe you could like create a pool of threads?
I'd go for the way suggested on the documentation, but try using a pool, as that would increase the performance of the server. Don't also forget to prevent any leaks with the code, otherwise sooner or later you'll kill the server.

For your second question, yes I understand, but usually we have to do an estimate of the expected number of Concurrent Users in order to tune the server. If you go with the thread pool option, you won't need to tune the number of threads allocated for the extension handler.

Cheers
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 27 guests