#1 2015-01-23 13:23:58

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Limit the number of client

Is there a way to limit the number of client.

On my server app I have allow connect only for a number of client, for example only 5 clients. If other clients try to connect my server app must reject the connection.
Is there a simple way to do it?

regards

Offline

#2 2015-01-23 15:30:19

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: Limit the number of client

just an idea..
maybe you can check number of active session in a callback procedure?

Offline

#3 2015-01-25 21:58:51

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Limit the number of client

lele9 wrote:

just an idea..
maybe you can check number of active session in a callback procedure?

I don't have idea about I do it. Can you indicate the page of documentation where I can found information about it?

Thanks

Offline

#4 2015-01-26 10:19:42

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: Limit the number of client

in documentation you can search "14.1. Publishing a service on the server" that explain all.
you can see an example in MainDemo (see AuditTrail logging).

Offline

#5 2015-01-26 11:24:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,718
Website

Re: Limit the number of client

Are you talking about active session number limitation?

You can use the TSQLRestServer.OnSessionCreate event to abort if the total count is too much, by returning true.
The number of active session (excluding the current one) is stored in Server.Stats.ClientsCurrent.

Offline

#6 2020-12-29 05:16:16

missionhq
Member
From: Australia
Registered: 2019-06-11
Posts: 33

Re: Limit the number of client

I noticed that if a client crash-stops, the Server.Stats.ClientsCurrent will show an incorrect number (ie it doesn't know the previous connection crashed).
Is there a way to purge old/invalid connections from the Server.Stats ?

Offline

#7 2020-12-29 09:48:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,718
Website

Re: Limit the number of client

There is already a purge of inactive sessions after a timeout.

Offline

#8 2020-12-30 04:05:02

missionhq
Member
From: Australia
Registered: 2019-06-11
Posts: 33

Re: Limit the number of client

I think the problem I am having is that checking for deprecated sessions only occurs if there are any currently active sessions.

Problem:
I am checking ClientsCurrent in the TSQLRestServer.OnSessionCreate method...

function TMyApp.DoOnSessionCreate(Sender: TSQLRestServer; Session: TAuthSession; Ctxt: TSQLRestServerURIContext): boolean;
var
  lClientsCurrent: integer;
begin
  lClientsCurrent:= Sender.Stats.ClientsCurrent;
  result:= lClientsCurrent >= fMyMaxConnections;
end;

but the check for inactive sessions only occurs once a session has been created (note TSQLRestServer.SessionAccess only gets called if Ctxt.URISessionSignaturePos <> 0)...

function TSQLRestServer.SessionAccess(Ctxt: TSQLRestServerURIContext): TAuthSession;
...
      fSessionsDeprecatedTix := tix; // check deprecated sessions every second
      for i := fSessions.Count-1 downto 0 do
        if tix>TAuthSession(fSessions.List[i]).TimeOutTix then
          SessionDelete(i,nil);

So if I limit MyMaxConnections to say 2, and then both of these clients crash-stop, a third connection attempt will get declined (ClientsCurrent >= fMyMaxConnections), but the old crashed sessions will never get deleted because TSQLRestServer.SessionAccess never gets called as I don't have any real active sessions.

Solution:
I am thinking I can create my own descendant of TSQLRestServer to give me access to the protected session methods and then check for deprecated sessions during OnSessionCreate, or to let the client connect, then check the session count and use a callback to force a disconnect if needed, or is there a better way to do this?
Or have I missed something?

Offline

#9 2020-12-30 09:23:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,718
Website

Re: Limit the number of client

Please check https://synopse.info/fossil/info/b4c6b71914

Don't forget to call Sessions.Lock/Unlock between the method call.

Offline

#10 2020-12-31 01:13:51

missionhq
Member
From: Australia
Registered: 2019-06-11
Posts: 33

Re: Limit the number of client

Thanks ab - working well.
(I noted a comment of the "inc(result)" line in SessionDeleteDeprecated and wasn't sure why too - I'll keep an eye on that thread....)

Offline

Board footer

Powered by FluxBB