Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something about NSURLSessionConfiguration.HTTPMaximumConnectionsPerHost I am missing?

I am experimenting with replacing some ancient networking code with NSUrlSession, but setting HTTPMaximumConnectionsPerHost to 1 is not having any effect. This request code is called 170 times but it makes 170 connections to the host (watching in CharlesProxy) before anything comes back, which is slamming the server. Am I missing something here?

All requests go to the same domain and url with only differences in parameters. Of course I can do something different but HTTPMaximumConnectionsPerHost seems like it should limit the connections.

At the moment I am compiling versus SDK 7 (due to having to support iOS 6 still) but if I can get this to work I can abandon iOS 6 and just support 7/8 and build vs 8. This is in an enterprise app BTW.

+ (NSURLSession*) sharedSession
{
    static NSURLSession* session;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        NSURLSessionConfiguration * sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];

        sessionConfig.timeoutIntervalForRequest = 30.0;
        sessionConfig.HTTPMaximumConnectionsPerHost = 1;
        sessionConfig.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
        sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

        session = [NSURLSession sessionWithConfiguration:sessionConfig
                                                               delegate:nil
                                                          delegateQueue:nil];
    });
    return session;
}

+ (void) createRequestWithPayload2:(HttpRequestPayload *)payload
    success:(void (^)(CommunicationResponse * response))success
    failure:(void (^)(NSError * error))failure
    progress:(void (^)(TaskStatus status))progressStatus
{
    NSURLSession* session = [RequestSender sharedSession];

    NSString * url = [NSString stringWithFormat:@"%@/%@", payload.baseURL, payload.urlParams];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:payload.method];
    [request setAllHTTPHeaderFields:payload.headers];
    if ( payload.body )
    {
        [request setHTTPBody:payload.body];
    }

    //NSLog(@"Request:\n%@",request);

    NSURLSessionDataTask * task =
        [session dataTaskWithRequest:request
                   completionHandler:^(NSData *data, NSURLResponse *resp, NSError *error)
         {
             dispatch_async(dispatch_get_main_queue(),
                            ^{
                                if ( error )
                                {
                                    NSLog(@"%@",error);
                                    failure(error);                                }
                                else
                                {
                                    NSHTTPURLResponse *response = (NSHTTPURLResponse*) resp;
                                    //NSLog(@"%@",response);
                                    //NSLog(@"%@",data);
                                    CommunicationResponse* cr = [CommunicationResponse new];
                                    [cr set_commStatus:response.statusCode];
                                    [cr set_response:data];
                                    success(cr);
                                }
                            });
         }];

    [task resume];
}
like image 806
ahwulf Avatar asked Nov 10 '22 22:11

ahwulf


1 Answers

Seems like you're not sharing, but creating new session each time. HTTPMaximumConnectionsPerHost only limit connections on the current session.

From the documentation

This limit is per session, so if you use multiple sessions, your app as a whole may exceed this limit.

NSURLSessionConfiguration : HTTPMaximumConnectionsPerHost

As alternative you can set discretionary property to YES (TRUE). Where it will limit all connections across all sessions to a reasonable number.

NSURLSessionConfiguration : discretionary

like image 123
Dimitar Atanasov Avatar answered Nov 14 '22 23:11

Dimitar Atanasov