Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Adjust upload and preview rate limits

Manav Rathid83ec5aa7afc
diff --git a/server/pkg/api/file.go b/server/pkg/api/file.go
index f8a535e2c3..61edaf5f45 100644
--- a/server/pkg/api/file.go
+++ b/server/pkg/api/file.go
@@ -56,7 +56,6 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
- h.Controller.AddOutstandingURLs(userID, -2)
c.JSON(http.StatusOK, file)
return
}
@@ -65,7 +64,6 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
- h.Controller.AddOutstandingURLs(userID, -2)
c.JSON(http.StatusOK, response)
}
@@ -133,7 +131,6 @@ func (h *FileHandler) Update(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
- h.Controller.AddOutstandingURLs(userID, -2)
c.JSON(http.StatusOK, response)
}
@@ -178,10 +175,6 @@ func (h *FileHandler) GetUploadURLV2(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
- if !h.Controller.AddOutstandingURLs(userID, 1) {
- handler.Error(c, stacktrace.Propagate(ente.ErrTooManyBadRequest, "too many outstanding upload URLs"))
- return
- }
c.JSON(http.StatusOK, url)
}
@@ -215,10 +208,6 @@ func (h *FileHandler) GetMultipartUploadURLV2(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
- if !h.Controller.AddOutstandingURLs(userID, 1) {
- handler.Error(c, stacktrace.Propagate(ente.ErrTooManyBadRequest, "too many outstanding upload URLs"))
- return
- }
c.JSON(http.StatusOK, upload)
}
diff --git a/server/pkg/controller/file.go b/server/pkg/controller/file.go
index 56e1772bd8..3f6fdd8520 100644
--- a/server/pkg/controller/file.go
+++ b/server/pkg/controller/file.go
@@ -40,26 +40,23 @@ import (
// FileController exposes functions to retrieve and access encrypted files
type FileController struct {
- FileRepo *repo.FileRepository
- ObjectRepo *repo.ObjectRepository
- ObjectCleanupRepo *repo.ObjectCleanupRepository
- TrashRepository *repo.TrashRepository
- UserRepo *repo.UserRepository
- UsageCtrl *UsageController
- CollectionRepo *repo.CollectionRepository
- TaskLockingRepo *repo.TaskLockRepository
- QueueRepo *repo.QueueRepository
- AccessCtrl access.Controller
- S3Config *s3config.S3Config
- ObjectCleanupCtrl *ObjectCleanupController
- LockController *lock.LockController
- EmailNotificationCtrl *email.EmailNotificationController
- DiscordController *discord.DiscordController
- HostName string
- cleanupCronRunning bool
- outstandingURLsMu sync.Mutex
- outstandingURLs map[int64]int // userID -> minted-but-uncommitted upload URLs
- outstandingURLsResetAt gTime.Time
+ FileRepo *repo.FileRepository
+ ObjectRepo *repo.ObjectRepository
+ ObjectCleanupRepo *repo.ObjectCleanupRepository
+ TrashRepository *repo.TrashRepository
+ UserRepo *repo.UserRepository
+ UsageCtrl *UsageController
+ CollectionRepo *repo.CollectionRepository
+ TaskLockingRepo *repo.TaskLockRepository
+ QueueRepo *repo.QueueRepository
+ AccessCtrl access.Controller
+ S3Config *s3config.S3Config
+ ObjectCleanupCtrl *ObjectCleanupController
+ LockController *lock.LockController
+ EmailNotificationCtrl *email.EmailNotificationController
+ DiscordController *discord.DiscordController
+ HostName string
+ cleanupCronRunning bool
}
// StorageOverflowAboveSubscriptionLimit is the amount (50 MB) by which user can go beyond their storage limit
@@ -323,26 +320,6 @@ func (c *FileController) Update(ctx context.Context, userID int64, file ente.Fil
return response, nil
}
-// AddOutstandingURLs adjusts the count of upload URLs the user has minted
-// but not yet committed via /files. It will return false if the user has
-// too many outstanding URLs.
-func (c *FileController) AddOutstandingURLs(userID int64, n int) bool {
- c.outstandingURLsMu.Lock()
- defer c.outstandingURLsMu.Unlock()
- if now := gTime.Now(); now.After(c.outstandingURLsResetAt) {
- c.outstandingURLs = map[int64]int{} // periodically discard stranded counts
- c.outstandingURLsResetAt = now.Add(gTime.Hour)
- }
- total := max(c.outstandingURLs[userID]+n, 0)
- ok := total <= 250
- if !ok {
- go c.DiscordController.NotifyPotentialAbuse(fmt.Sprintf("Too many outstanding upload URLs for user %d", userID))
- total = 0 // so that stranded counts don't lock the user out
- }
- c.outstandingURLs[userID] = total
- return ok
-}
-
// GetUploadURLs returns a bunch of presigned URLs for uploading files
func (c *FileController) GetUploadURLs(ctx context.Context, userID int64, count int, app ente.App, ignoreLimit bool) ([]ente.UploadURL, error) {
err := c.UsageCtrl.CanUploadFile(ctx, userID, nil, app)
diff --git a/server/pkg/middleware/rate_limit.go b/server/pkg/middleware/rate_limit.go
index f53dc77a91..8ef3d617d4 100644
--- a/server/pkg/middleware/rate_limit.go
+++ b/server/pkg/middleware/rate_limit.go
@@ -28,7 +28,7 @@ type RateLimitMiddleware struct {
limit250ReqPerMin *limiter.Limiter
limit300ReqPerMin *limiter.Limiter
limit500ReqPerMin *limiter.Limiter
- limit500ReqPerSec *limiter.Limiter
+ limit700ReqPerSec *limiter.Limiter
discordCtrl *discord.DiscordController
}
@@ -39,7 +39,7 @@ func NewRateLimitMiddleware(discordCtrl *discord.DiscordController, limit int64,
limit250ReqPerMin: util.NewRateLimiter("250-M"),
limit300ReqPerMin: util.NewRateLimiter("300-M"),
limit500ReqPerMin: util.NewRateLimiter("500-M"),
- limit500ReqPerSec: util.NewRateLimiter("500-S"),
+ limit700ReqPerSec: util.NewRateLimiter("700-S"),
discordCtrl: discordCtrl,
limit: limit,
reset: reset,
@@ -225,7 +225,7 @@ func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limi
return r.limit10ReqPerMin
}
if strings.HasPrefix(reqPath, "/files/preview/") {
- return r.limit500ReqPerSec
+ return r.limit700ReqPerSec
}
if reqPath == "/public-collection/anon-identity" {
return r.limit10ReqPerMin