Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
notifications-bot
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
archives
notifications-bot
Commits
4f536665
Commit
4f536665
authored
Dec 21, 2021
by
Jonah Husson
Browse files
Options
Downloads
Patches
Plain Diff
Add basic rate limiting
parent
418c8430
No related branches found
No related tags found
1 merge request
!17
Add basic rate limiting
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
notifications/notifications.go
+9
-0
9 additions, 0 deletions
notifications/notifications.go
storage/storage.go
+16
-1
16 additions, 1 deletion
storage/storage.go
with
25 additions
and
1 deletion
notifications/notifications.go
+
9
−
0
View file @
4f536665
...
@@ -151,6 +151,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
...
@@ -151,6 +151,7 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
i
.
SetGatewayAuthentication
()
i
.
SetGatewayAuthentication
()
impl
.
inst
=
i
impl
.
inst
=
i
impl
.
Storage
.
StartLastSentCleaner
()
go
impl
.
Cleaner
()
go
impl
.
Cleaner
()
return
impl
,
nil
return
impl
,
nil
...
@@ -192,6 +193,13 @@ func notifyUser(data *pb.NotificationData, apnsClient *apns.ApnsComm, fc *fireba
...
@@ -192,6 +193,13 @@ func notifyUser(data *pb.NotificationData, apnsClient *apns.ApnsComm, fc *fireba
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
WithMessagef
(
err
,
"Failed to lookup user with tRSA hash %+v"
,
e
.
TransmissionRSAHash
)
return
errors
.
WithMessagef
(
err
,
"Failed to lookup user with tRSA hash %+v"
,
e
.
TransmissionRSAHash
)
}
}
if
t
,
ok
:=
db
.
LastSent
.
Load
(
u
.
Token
);
ok
{
lastSent
:=
t
.
(
time
.
Time
)
if
time
.
Since
(
lastSent
)
>
3
*
time
.
Second
{
jww
.
DEBUG
.
Printf
(
"Rate limiting token %s [last sent at %+v]"
,
u
.
Token
,
lastSent
)
continue
}
}
isAPNS
:=
!
strings
.
Contains
(
u
.
Token
,
":"
)
isAPNS
:=
!
strings
.
Contains
(
u
.
Token
,
":"
)
// mutableContent := 1
// mutableContent := 1
...
@@ -219,6 +227,7 @@ func notifyUser(data *pb.NotificationData, apnsClient *apns.ApnsComm, fc *fireba
...
@@ -219,6 +227,7 @@ func notifyUser(data *pb.NotificationData, apnsClient *apns.ApnsComm, fc *fireba
// return errors.WithMessagef(err, "Failed to remove user registration tRSA hash: %+v", u.TransmissionRSAHash)
// return errors.WithMessagef(err, "Failed to remove user registration tRSA hash: %+v", u.TransmissionRSAHash)
//}
//}
}
else
{
}
else
{
db
.
LastSent
.
Store
(
u
.
Token
,
time
.
Now
())
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
data
.
EphemeralID
,
u
.
Token
,
resp
)
jww
.
INFO
.
Printf
(
"Notified ephemeral ID %+v [%+v] and received response %+v"
,
data
.
EphemeralID
,
u
.
Token
,
resp
)
}
}
}
else
{
}
else
{
...
...
This diff is collapsed.
Click to expand it.
storage/storage.go
+
16
−
1
View file @
4f536665
...
@@ -6,18 +6,20 @@ import (
...
@@ -6,18 +6,20 @@ import (
jww
"github.com/spf13/jwalterweatherman"
jww
"github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/crypto/hash"
"gitlab.com/elixxir/crypto/hash"
"gitlab.com/xx_network/primitives/id/ephemeral"
"gitlab.com/xx_network/primitives/id/ephemeral"
"sync"
"time"
"time"
)
)
type
Storage
struct
{
type
Storage
struct
{
database
database
LastSent
sync
.
Map
}
}
// Create a new Storage object wrapping a database interface
// Create a new Storage object wrapping a database interface
// Returns a Storage object and error
// Returns a Storage object and error
func
NewStorage
(
username
,
password
,
dbName
,
address
,
port
string
)
(
*
Storage
,
error
)
{
func
NewStorage
(
username
,
password
,
dbName
,
address
,
port
string
)
(
*
Storage
,
error
)
{
db
,
err
:=
newDatabase
(
username
,
password
,
dbName
,
address
,
port
)
db
,
err
:=
newDatabase
(
username
,
password
,
dbName
,
address
,
port
)
storage
:=
&
Storage
{
db
}
storage
:=
&
Storage
{
db
,
sync
.
Map
{}
}
return
storage
,
err
return
storage
,
err
}
}
...
@@ -117,3 +119,16 @@ func (s *Storage) AddEphemeralsForOffset(offset int64, epoch int32, size uint, t
...
@@ -117,3 +119,16 @@ func (s *Storage) AddEphemeralsForOffset(offset int64, epoch int32, size uint, t
}
}
return
nil
return
nil
}
}
func
(
s
*
Storage
)
StartLastSentCleaner
()
{
go
func
()
{
time
.
Sleep
(
5
*
time
.
Second
)
s
.
LastSent
.
Range
(
func
(
key
,
value
interface
{})
bool
{
lastSent
:=
value
.
(
time
.
Time
)
if
time
.
Since
(
lastSent
)
>
3
*
time
.
Second
{
s
.
LastSent
.
Delete
(
key
)
}
return
true
})
}()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment