长时间运行的REST API [英] long running REST API

查看:321
本文介绍了长时间运行的REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连线的后端API大约需要3-4分钟才能传回回应。 iOS客户端似乎超时60秒(这是默认值),我不知道如何延长该时间。



我试图设置NSURLRequest的timeoutInterval为一个大数,并设置Connection:Keep-Alive头,但我没有运气。这里是我使用的代码,省略了一些API的细节:

  NSURL * url = [NSURL URLWithString:@myAPI] ; 

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@POST];

NSString * postString = @key1 = val1& key2 = val2 ...;
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

request.timeoutInterval = 600.0f;

[request setValue:@Keep-AliveforHTTPHeaderField:@Connection];
[request setValue:@application / x-www-form-urlencodedforHTTPHeaderField:@Content-Type];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response,NSData * data,NSError * error){
NSLog(@data =%@数据);
}];

不返回任何数据,并且所有响应头字段都为空,即使后端API仍在处理请求。



我听说我可能需要设置一个名为socketTimout的间隔,所以套接字知道保持连接打开更长,但我不知道在哪里设置

解决方案

而不是试图延长超时,这将是一个更好的主意,修改您的服务器体系结构一点。让服务器在获取所有数据后立即返回200 OK,然后在后台队列中完成处理。创建另一个API调用,您可以使用它来检索数据,并让iOS应用程序定期检查数据是否已被处理。或者,使用推送通知让用户/手机知道数据何时完成处理。



如果你碰巧使用Ruby on Rails,长时间运行的进程在工作线程上发生。看看delayed_job Ruby gem。



如果由于某种原因不能修改服务器的操作方式,请尝试确保您使用的API没有超时设置本身。我不能想到一个原因iOS会忽略你设置的timeoutInterval,除非该属性是只读的。尝试改用此init方法:

   -  initWithURL:cachePolicy:timeoutInterval:
/ pre>

A backend API I need to connect to takes about 3-4 minutes to return a response. The iOS client seems to timeout at exactly 60 seconds (which is the default), and I can't figure out how to extend that time out.

I have tried to set the timeoutInterval for the NSURLRequest to a large number, and set the Connection: Keep-Alive header, but I have no luck. Here is the code I am using, omitting some API details:

NSURL *url = [NSURL URLWithString:@"myAPI"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

NSString *postString = @"key1=val1&key2=val2...";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

request.timeoutInterval = 600.0f;

[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
     NSLog(@"data = %@", data);
}];

No data is returned and all response header fields are empty, even though the backend API is still processing the request.

I heard I might need to set something called a "socketTimout" interval so the socket knows to keep the connection open longer, but I do not know where to set that.

解决方案

Rather than trying to extend the timeout, it would be a better idea to modify your server architecture a bit. Have the server kick back a 200 OK as soon as it gets all the data, then have the processing done in a background queue. Create another API call that you can use to retrieve the data and have the iOS app check that at regular intervals to see if the data has been processed. Alternatively, use a push notification to the let the user/phone know when the data is done processing.

If you happen to be using Ruby on Rails it's extremely easy to set long-running processes to happen on a worker thread. Take a look the delayed_job Ruby gem.

If for some reason you cannot modify how the server operates, try making sure the API you are using doesn't have a timeout set itself. I can't think of a reason iOS would be ignoring the the timeoutInterval you're setting, unless that property is read only. Try using this init method instead:

- initWithURL:cachePolicy:timeoutInterval:

这篇关于长时间运行的REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆