B4J Question Timer vs Sleep performance and dos attack

Douglas Farias

Expert
Licensed User
Longtime User
Hi all, i have 2 simple questions about B4J Server.

Question 1
I need to make a get request on a website (time by time), store the result value inside a global variable at (main) and i use this variable to show on handler pages.
the sub is like this.

B4X:
Private Sub Counter_Page
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://website.com/total")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim Total As Int = root.Get("Total")
        totalPlacas = Total
    Else
        Log(j.ErrorMessage)
    End If
    j.Release
    
    'CALL THE SUB AGAIN (EVERY 20 MINUTES)
    Sleep(20 * 60000)
    Counter_Page
End Sub

Is this code correct? it is working perfect, but is the best solution or i need to use a timer (replace sleep for a timer)?
the server will stay online for a lot of days, every time the sub call the same sub again, this is good? or i will have problem with RAM,CPU usage etc??
my question is about performance of the webserver, what is the best here? timer or sleep?



Question 2
How works the DOS Filter on B4J Server?
This is enabled automatic when you start a server or need some configuration?

i have this line on my code, before the server start.
B4X:
srvr.AddDoSFilter("/*", CreateMap("maxRequestMs": 5 * 60 * 1000)) '5 MINUTOS / FILTRO ATAQUE DOS
but this is sufficient?

i made a test, making a lot of requests to a page and i see the log
2020-04-24 14:27:57.851:WARNejs.DoSFilter:qtp1288354730-11: DOS ALERT: Request delayed=100ms, ip=127.0.0.1, session=null, user=null

but i m not blocked, how works the block? how can i block the users making dos atack?
and if blocked how can i check the list of blocks and how unblock a user (ip)?

Obs: the page about DOS (@Erel ) are with the links off (not working)

if anyone has a working example of how to use the dos filter, show logs, block and unblock users and can share I appreciate it.

Thank you!
 

Douglas Farias

Expert
Licensed User
Longtime User
Hi.
Each thread should focus on a single question.
ok.

I will answer question #1:
The Sleep call will be made every 20 minutes. It doesn't matter whether it will take 0.1 milliseconds or 0.3 milliseconds. Small preoptimizations are one of the biggest programming mistakes.
ok.
sorry, i should be more clear and objective in the question.

The question is whether sleep uses more memory or cpu compared to the timer.
How would the logic of sleep be in this case?

B4X:
Sub Example
log ("example")
sleep (20 minutes) 'is VPS memory or CPU reserved here?
Example 'call the same sub again (is the used memory or cpu released?)
End Sub

Does the act of waiting 20 minutes consume ram or cpu in the long run? suppose I leave this sub running forever, as it will be a website, I do not intend to shut down.
If I make 7-10 subs similar to this one, running on the main, can there be problems with long use of VPS resources?

are the resources (ram and cpu) released after sleep? when is the sub called again? or are they accumulating the use of resources?

Ex:
on the first call of the sub use 100kb (ram)
on the second call sum 100kb + 100kb etc ....
later a lot of days it will cause RAM crash...

would it have any advantage to use a timer instead of sleep or vice versa?


Obs: remembering that I'm asking and not making a statement.

Thanks.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Ex:
on the first call of the sub use 100kb (ram)
on the second call sum 100kb + 100kb etc ....
later a lot of days it will cause RAM crash
Why call the sub again? Put sleep in a do while loop. Then you only have one sub call. You can use a condition for the do while that checks if it should continue sleeping / waiting (not necessary, just an idea)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…