feat: change rate limit to per hour instead of per second

This commit is contained in:
Vlad Stan 2023-02-16 14:37:31 +02:00
parent 6e67443ea4
commit 09707151c5
3 changed files with 11 additions and 11 deletions

View file

@ -98,7 +98,7 @@ class NostrClientConnection:
self._auth_challenge: Optional[str] = None
self._auth_challenge_created_at = 0
self._last_event_timestamp = 0 # in seconds
self._last_event_timestamp = 0 # in hours
self._event_count_per_timestamp = 0
self.broadcast_event: Optional[
@ -296,8 +296,8 @@ class NostrClientConnection:
return True, ""
def _validate_event(self, e: NostrEvent) -> Tuple[bool, str]:
if self._exceeded_max_events_per_second():
return False, f"Exceeded max events per second limit'!"
if self._exceeded_max_events_per_hour():
return False, f"Exceeded max events per hour limit'!"
try:
e.check_signature()
@ -349,11 +349,11 @@ class NostrClientConnection:
return True, ""
def _exceeded_max_events_per_second(self) -> bool:
if self.client_config.max_events_per_second == 0:
def _exceeded_max_events_per_hour(self) -> bool:
if self.client_config.max_events_per_hour == 0:
return False
current_time = round(time.time())
current_time = round(time.time() / 3600)
if self._last_event_timestamp == current_time:
self._event_count_per_timestamp += 1
else:
@ -361,7 +361,7 @@ class NostrClientConnection:
self._event_count_per_timestamp = 0
return (
self._event_count_per_timestamp > self.client_config.max_events_per_second
self._event_count_per_timestamp > self.client_config.max_events_per_hour
)
def _created_at_in_range(self, created_at: int) -> Tuple[bool, str]:

View file

@ -19,7 +19,7 @@ class FilterSpec(Spec):
class EventSpec(Spec):
max_events_per_second = Field(0, alias="maxEventsPerSecond")
max_events_per_hour = Field(0, alias="maxEventsPerHour")
created_at_days_past = Field(0, alias="createdAtDaysPast")
created_at_hours_past = Field(0, alias="createdAtHoursPast")

View file

@ -497,12 +497,12 @@
</div>
</div>
<div class="row items-center no-wrap q-mb-md">
<div class="col-3 q-pr-lg">Max events per second:</div>
<div class="col-3 q-pr-lg">Max events per hour:</div>
<div class="col-3 col-sm-4 q-pr-lg">
<q-input
filled
dense
v-model.trim="relay.config.maxEventsPerSecond"
v-model.trim="relay.config.maxEventsPerHour"
type="number"
min="0"
></q-input>
@ -515,7 +515,7 @@
</q-tooltip></q-icon
>
<q-badge
v-if="relay.config.maxEventsPerSecond == 0"
v-if="relay.config.maxEventsPerHour == 0"
color="green"
class="float-right"
><span>No Limit</span>