chore: rateLimit added

This commit is contained in:
Ankit Nayan 2022-10-11 18:35:05 +05:30
parent 4e38f1dcc0
commit fce4496214

View File

@ -43,6 +43,12 @@ const HEART_BEAT_DURATION = 6 * time.Hour
// const HEART_BEAT_DURATION = 10 * time.Second // const HEART_BEAT_DURATION = 10 * time.Second
const RATE_LIMIT_CHECK_DURATION = 1 * time.Minute
const RATE_LIMIT_VALUE = 60
// const RATE_LIMIT_CHECK_DURATION = 20 * time.Second
// const RATE_LIMIT_VALUE = 5
var telemetry *Telemetry var telemetry *Telemetry
var once sync.Once var once sync.Once
@ -69,6 +75,7 @@ type Telemetry struct {
companyDomain string companyDomain string
minRandInt int minRandInt int
maxRandInt int maxRandInt int
rateLimits map[string]int8
} }
func createTelemetry() { func createTelemetry() {
@ -77,6 +84,7 @@ func createTelemetry() {
operator: analytics.New(api_key), operator: analytics.New(api_key),
phOperator: ph.New(ph_api_key), phOperator: ph.New(ph_api_key),
ipAddress: getOutboundIP(), ipAddress: getOutboundIP(),
rateLimits: make(map[string]int8),
} }
telemetry.minRandInt = 0 telemetry.minRandInt = 0
telemetry.maxRandInt = int(1 / DEFAULT_SAMPLING) telemetry.maxRandInt = int(1 / DEFAULT_SAMPLING)
@ -87,7 +95,18 @@ func createTelemetry() {
telemetry.SetTelemetryEnabled(constants.IsTelemetryEnabled()) telemetry.SetTelemetryEnabled(constants.IsTelemetryEnabled())
telemetry.SendEvent(TELEMETRY_EVENT_HEART_BEAT, data) telemetry.SendEvent(TELEMETRY_EVENT_HEART_BEAT, data)
ticker := time.NewTicker(HEART_BEAT_DURATION) ticker := time.NewTicker(HEART_BEAT_DURATION)
rateLimitTicker := time.NewTicker(RATE_LIMIT_CHECK_DURATION)
go func() {
for {
select {
case <-rateLimitTicker.C:
telemetry.rateLimits = make(map[string]int8)
}
}
}()
go func() { go func() {
for { for {
select { select {
@ -199,6 +218,12 @@ func (a *Telemetry) SendEvent(event string, data map[string]interface{}) {
return return
} }
if a.rateLimits[event] < RATE_LIMIT_VALUE {
a.rateLimits[event] += 1
} else {
return
}
// zap.S().Info(data) // zap.S().Info(data)
properties := analytics.NewProperties() properties := analytics.NewProperties()
properties.Set("version", version.GetVersion()) properties.Set("version", version.GetVersion())