Good evening everybody,
this is the code of a worker that is supposed to create requests limits per a given period for a web app. See the code, and what I do is to set a same type variable in Main to the class instance. When though I try to check the rate limit for the IP, as seen in the picture bellow, the code breaks and the reason is that all of the variables in the class are null although they are initialized in worker initialize. What am I doing wrong and I cannot see it right now?
this is the code of a worker that is supposed to create requests limits per a given period for a web app. See the code, and what I do is to set a same type variable in Main to the class instance. When though I try to check the rate limit for the IP, as seen in the picture bellow, the code breaks and the reason is that all of the variables in the class are null although they are initialized in worker initialize. What am I doing wrong and I cannot see it right now?
B4X:
Sub Class_Globals
Private mRequests As Map
Private tm As Timer
Private iMaxRequests As Int = 60
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
mRequests.Initialize
tm.Initialize("tm", 60000)
tm.Enabled = True
Main.wrkLimmiter = Me
StartMessageLoop
End Sub
Public Sub tm_Tick
Log(mRequests)
mRequests.Clear
End Sub
Public Sub GetWorker As wrkRateLimitter
Return Me
End Sub
Public Sub IsRequestAboveRateLimit(sIP As String) As Boolean
Dim su As StringUtils
sIP = su.EncodeUrl(sIP, "UTF8")
If mRequests.ContainsKey(sIP) Then
mRequests.Put(sIP, mRequests.Get(sIP).As(Int) + 1)
Else
mRequests.Put(sIP, 1)
End If
Log(mRequests.Get(sIP))
Return mRequests.Get(sIP).As(Int) > iMaxRequests
End Sub
Public Sub setMaxRequests(iMax As Int)
iMaxRequests = iMax
End Sub
Public Sub setTimeIntervalInSeconds(iSecs As Int)
tm.Interval = iSecs * 1000
tm.Enabled = False
tm.Enabled = True
End Sub
Last edited: