Is there something in NSURLSessionConfiguration.HTTPMaximumConnectionsPerHost I'm missing?

I am experimenting with replacing some ancient networking code with NSUrlSession, but setting HTTPMaximumConnectionsPerHost to 1 has no effect. This request code is called 170 times, but it makes 170 connections to the host (watching in CharlesProxy) before anything comes back, which shuts down the server. Did I miss something?

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

I am currently compiling over SDK 7 (due to the need to support iOS 6), but if I can get this to work, I can ditch iOS 6 and just support 7/8 and build vs 8. It's into 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];
}

      

+3


source to share


2 answers


The maximum connection per host means that the currently running connections will be added to the queue and will wait for the older one to finish. It does not limit all accounts, but the account at one time.



0


source


It looks like you are not sharing, but creating a new session each time. HTTPMaximumConnectionsPerHost restricts connections for the current session only.

From the documentation

This is a per session limit, so if you are using multiple sessions, your application as a whole may exceed this limit.



NSURLSessionConfiguration: HTTPMaximumConnectionsPerHost

Alternatively, you can set the property discretionary

to YES

( TRUE

). If it limits all connections in all sessions to a reasonable number.

NSURLSessionConfiguration: discretionary

0


source







All Articles