Ajax request speed issue
I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.
def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end
The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.
-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------
But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.
-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------
I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?
ruby-on-rails ruby-on-rails-5
add a comment |
I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.
def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end
The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.
-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------
But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.
-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------
I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?
ruby-on-rails ruby-on-rails-5
1
Try settingrequest.session_options[:skip] = true
– razvans
Nov 21 at 13:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
Did you add it beforeif session[:expires_at].nil?
?
– razvans
Nov 22 at 8:52
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28
add a comment |
I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.
def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end
The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.
-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------
But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.
-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------
I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?
ruby-on-rails ruby-on-rails-5
I want to prevent from user sending requests from ajax too fast. I wrote a validation in controller as bellow.
def before_action
if session[:expires_at].nil?
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
elsif Time.current > session[:expires_at].to_time
logger.info "Operation is legal"
session[:expires_at] = Time.current + 3.seconds
else
logger.info "Error: Operation too fast"
render json: { msg: "Operation too fast",res: "Err"}
return
end
sleep 2 <-- The issue happends here
end
The sleep 2 seconds means my program excution time. If I remove sleep 2 seconds, everything works fine.The result would like below when I send the requests really fast.
-----------Logs-------------
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
----------------------------
But, if the second or more resquests been sended before the first one excutes finished. The result would be unexpected. If I add sleep 2 in the validation code.
-----------Logs-------------
Operation is legal
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Error: Operation too fast
Operation is legal
Operation is legal
Error: Operation too fast
Error: Operation too fast
----------------------------
I am totally confused for this situation. Would the session value not being saved before the request finished? How should I fix this issue?
ruby-on-rails ruby-on-rails-5
ruby-on-rails ruby-on-rails-5
edited Nov 21 at 8:09
asked Nov 21 at 7:01
david0116
298
298
1
Try settingrequest.session_options[:skip] = true
– razvans
Nov 21 at 13:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
Did you add it beforeif session[:expires_at].nil?
?
– razvans
Nov 22 at 8:52
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28
add a comment |
1
Try settingrequest.session_options[:skip] = true
– razvans
Nov 21 at 13:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
Did you add it beforeif session[:expires_at].nil?
?
– razvans
Nov 22 at 8:52
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28
1
1
Try setting
request.session_options[:skip] = true
– razvans
Nov 21 at 13:03
Try setting
request.session_options[:skip] = true
– razvans
Nov 21 at 13:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
Did you add it before
if session[:expires_at].nil?
?– razvans
Nov 22 at 8:52
Did you add it before
if session[:expires_at].nil?
?– razvans
Nov 22 at 8:52
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28
add a comment |
1 Answer
1
active
oldest
votes
it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?
you should find a way to be sure a variable you are using is persistent over more requests in parallel.
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406793%2fajax-request-speed-issue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?
you should find a way to be sure a variable you are using is persistent over more requests in parallel.
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
add a comment |
it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?
you should find a way to be sure a variable you are using is persistent over more requests in parallel.
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
add a comment |
it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?
you should find a way to be sure a variable you are using is persistent over more requests in parallel.
it looks like the session variables will be stored later in processing a controller lifecycle? a workaround could be to use redis for this case?
you should find a way to be sure a variable you are using is persistent over more requests in parallel.
answered Nov 21 at 10:18
devanand
3,3271318
3,3271318
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
add a comment |
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
Yeah, session variables is stored after processing finished. I have no idea how to solve this issue.
– david0116
Nov 21 at 18:06
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
I guest I have to send a validating request first everytime before doing real request to see if the opreation is valid.
– david0116
Nov 22 at 6:28
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
Two steps validation works fine for now. Thanks for the help.
– david0116
Nov 22 at 7:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406793%2fajax-request-speed-issue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try setting
request.session_options[:skip] = true
– razvans
Nov 21 at 13:03
It's not working for me. The results are always "Operation is legal" after added request.session_options[:skip] = true.
– david0116
Nov 21 at 18:03
Did you add it before
if session[:expires_at].nil?
?– razvans
Nov 22 at 8:52
Yes, I added at the first line of the function. Nvm I've solved it by using two steps validation. A validating request will be sent before user's request.
– david0116
Nov 22 at 9:28