-
Notifications
You must be signed in to change notification settings - Fork 969
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feat/1.2.0/img' into test
- Loading branch information
Showing
12 changed files
with
137 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package middleware | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/answerdev/answer/internal/base/handler" | ||
"github.com/answerdev/answer/internal/base/reason" | ||
"github.com/answerdev/answer/internal/repo/limit" | ||
"github.com/answerdev/answer/pkg/encryption" | ||
"github.com/gin-gonic/gin" | ||
"github.com/segmentfault/pacman/errors" | ||
"github.com/segmentfault/pacman/log" | ||
) | ||
|
||
type RateLimitMiddleware struct { | ||
limitRepo *limit.LimitRepo | ||
} | ||
|
||
// NewRateLimitMiddleware new rate limit middleware | ||
func NewRateLimitMiddleware(limitRepo *limit.LimitRepo) *RateLimitMiddleware { | ||
return &RateLimitMiddleware{ | ||
limitRepo: limitRepo, | ||
} | ||
} | ||
|
||
// DuplicateRequestRejection detects and rejects duplicate requests | ||
// It only works for the requests that post content. Such as add question, add answer, comment etc. | ||
func (rm *RateLimitMiddleware) DuplicateRequestRejection(ctx *gin.Context, req any) bool { | ||
userID := GetLoginUserIDFromContext(ctx) | ||
fullPath := ctx.FullPath() | ||
reqJson, _ := json.Marshal(req) | ||
key := encryption.MD5(fmt.Sprintf("%s:%s:%s", userID, fullPath, string(reqJson))) | ||
reject, err := rm.limitRepo.CheckAndRecord(ctx, key) | ||
if err != nil { | ||
log.Errorf("check and record rate limit error: %s", err.Error()) | ||
return false | ||
} | ||
if !reject { | ||
return false | ||
} | ||
log.Debugf("duplicate request: [%s] %s", fullPath, string(reqJson)) | ||
handler.HandleResponse(ctx, errors.BadRequest(reason.DuplicateRequestError), nil) | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package limit | ||
|
||
import ( | ||
"context" | ||
"github.com/answerdev/answer/internal/base/constant" | ||
"github.com/answerdev/answer/internal/base/data" | ||
"github.com/answerdev/answer/internal/base/reason" | ||
"github.com/segmentfault/pacman/errors" | ||
) | ||
|
||
// LimitRepo auth repository | ||
type LimitRepo struct { | ||
data *data.Data | ||
} | ||
|
||
// NewRateLimitRepo new repository | ||
func NewRateLimitRepo(data *data.Data) *LimitRepo { | ||
return &LimitRepo{ | ||
data: data, | ||
} | ||
} | ||
|
||
// CheckAndRecord check | ||
func (lr *LimitRepo) CheckAndRecord(ctx context.Context, key string) (limit bool, err error) { | ||
_, exist, err := lr.data.Cache.GetString(ctx, constant.RateLimitCacheKeyPrefix+key) | ||
if err != nil { | ||
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() | ||
} | ||
if exist { | ||
return true, nil | ||
} | ||
err = lr.data.Cache.SetString(ctx, constant.RateLimitCacheKeyPrefix+key, "1", constant.RateLimitCacheTime) | ||
if err != nil { | ||
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters