Alamofire与NSURLSession的并发请求

73

我的测试应用程序出现了一些奇怪的行为。我向同一台服务器发送了大约50个同时进行的GET请求。该服务器是嵌入式服务器,位于资源非常有限的小型硬件上。为了优化每个单独请求的性能,我按如下方式配置了一个Alamofire.Manager实例:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPMaximumConnectionsPerHost = 2
configuration.timeoutIntervalForRequest = 30
let manager = Alamofire.Manager(configuration: configuration)

当我使用manager.request(...)发送请求时,它们会以成对的形式分派(如预期的一样,在Charles HTTP代理中进行了检查)。然而奇怪的是,所有在第一个请求发送后30秒内未完成的请求都会因为超时而被取消(即使它们尚未被发送)。这里有一张展示这种行为的图示:

concurrent request illustration

这是预期的行为吗?我如何确保请求在发送之前不会超时?

非常感谢!


也许你真正想设置的是 timeoutIntervalForResource 而不是 timeoutIntervalForRequest - mattt
谢谢,但我尝试了两种方法,问题仍然存在。 - Hannes
你的方法在Alamofire 4中不再起作用,请更新它。 - famfamfam
你用什么程序制作了这个图表? - Nike Kov
@NikKov 我用过Photoshop - Hannes
1
点赞这个漂亮的图表,使问题变得非常清晰 - 我一直在到处寻找答案,但很难用语言解释发生了什么。 - Inti
1个回答

132

是的,这是预期的行为。一种解决方法是将您的请求封装在自定义的异步NSOperation子类中,然后使用操作队列的maxConcurrentOperationCount来控制并发请求的数量,而不是使用HTTPMaximumConnectionsPerHost参数。

原始的AFNetworking很好地将请求封装在操作中,这使得这个问题变得微不足道。但AFNetworking的NSURLSession实现从未这样做过,Alamofire也是如此。


您可以轻松地将Request封装在NSOperation子类中。例如:

class NetworkOperation: AsynchronousOperation {

    // define properties to hold everything that you'll supply when you instantiate
    // this object and will be used when the request finally starts
    //
    // in this example, I'll keep track of (a) URL; and (b) closure to call when request is done

    private let urlString: String
    private var networkOperationCompletionHandler: ((_ responseObject: Any?, _ error: Error?) -> Void)?

    // we'll also keep track of the resulting request operation in case we need to cancel it later

    weak var request: Alamofire.Request?

    // define init method that captures all of the properties to be used when issuing the request

    init(urlString: String, networkOperationCompletionHandler: ((_ responseObject: Any?, _ error: Error?) -> Void)? = nil) {
        self.urlString = urlString
        self.networkOperationCompletionHandler = networkOperationCompletionHandler
        super.init()
    }

    // when the operation actually starts, this is the method that will be called

    override func main() {
        request = Alamofire.request(urlString, method: .get, parameters: ["foo" : "bar"])
            .responseJSON { response in
                // do whatever you want here; personally, I'll just all the completion handler that was passed to me in `init`

                self.networkOperationCompletionHandler?(response.result.value, response.result.error)
                self.networkOperationCompletionHandler = nil

                // now that I'm done, complete this operation

                self.completeOperation()
        }
    }

    // we'll also support canceling the request, in case we need it

    override func cancel() {
        request?.cancel()
        super.cancel()
    }
}

然后,当我想发起50个请求时,我会这样做:
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 2

for i in 0 ..< 50 {
    let operation = NetworkOperation(urlString: "http://example.com/request.php?value=\(i)") { responseObject, error in
        guard let responseObject = responseObject else {
            // handle error here

            print("failed: \(error?.localizedDescription ?? "Unknown error")")
            return
        }

        // update UI to reflect the `responseObject` finished successfully

        print("responseObject=\(responseObject)")
    }
    queue.addOperation(operation)
}

那样一来,这些请求将受到“maxConcurrentOperationCount”的限制,我们就不必担心任何请求超时的问题了。
这是一个示例的异步操作基类,可以处理与异步/并发NSOperation子类相关的KVN。
//
//  AsynchronousOperation.swift
//
//  Created by Robert Ryan on 9/20/14.
//  Copyright (c) 2014 Robert Ryan. All rights reserved.
//

import Foundation

/// Asynchronous Operation base class
///
/// This class performs all of the necessary KVN of `isFinished` and
/// `isExecuting` for a concurrent `NSOperation` subclass. So, to developer
/// a concurrent NSOperation subclass, you instead subclass this class which:
///
/// - must override `main()` with the tasks that initiate the asynchronous task;
///
/// - must call `completeOperation()` function when the asynchronous task is done;
///
/// - optionally, periodically check `self.cancelled` status, performing any clean-up
///   necessary and then ensuring that `completeOperation()` is called; or
///   override `cancel` method, calling `super.cancel()` and then cleaning-up
///   and ensuring `completeOperation()` is called.

public class AsynchronousOperation : Operation {

    private let stateLock = NSLock()

    private var _executing: Bool = false
    override private(set) public var isExecuting: Bool {
        get {
            return stateLock.withCriticalScope { _executing }
        }
        set {
            willChangeValue(forKey: "isExecuting")
            stateLock.withCriticalScope { _executing = newValue }
            didChangeValue(forKey: "isExecuting")
        }
    }

    private var _finished: Bool = false
    override private(set) public var isFinished: Bool {
        get {
            return stateLock.withCriticalScope { _finished }
        }
        set {
            willChangeValue(forKey: "isFinished")
            stateLock.withCriticalScope { _finished = newValue }
            didChangeValue(forKey: "isFinished")
        }
    }

    /// Complete the operation
    ///
    /// This will result in the appropriate KVN of isFinished and isExecuting

    public func completeOperation() {
        if isExecuting {
            isExecuting = false
        }

        if !isFinished {
            isFinished = true
        }
    }

    override public func start() {
        if isCancelled {
            isFinished = true
            return
        }

        isExecuting = true

        main()
    }

    override public func main() {
        fatalError("subclasses must override `main`")
    }
}

/*
 Abstract:
 An extension to `NSLocking` to simplify executing critical code.

 Adapted from Advanced NSOperations sample code in WWDC 2015 https://developer.apple.com/videos/play/wwdc2015/226/
 Adapted from https://developer.apple.com/sample-code/wwdc/2015/downloads/Advanced-NSOperations.zip
 */

import Foundation

extension NSLocking {

    /// Perform closure within lock.
    ///
    /// An extension to `NSLocking` to simplify executing critical code.
    ///
    /// - parameter block: The closure to be performed.

    func withCriticalScope<T>(block: () throws -> T) rethrows -> T {
        lock()
        defer { unlock() }
        return try block()
    }
}

这种模式还有其他可能的变化,但请确保你 (a) 返回 true 以表示异步执行;(b) 根据《并发编程指南:操作队列》中“配置操作以进行并发执行”部分的要求发布必要的isFinishedisExecuting KVN。


1
哇,非常感谢Rob,很少能得到这么好的答案!运行得非常顺利。 - Hannes
1
所有在Stack Overflow上的用户贡献都是使用cc by-sa 3.0进行贡献的,需要署名。请参见本网页底部的链接。总之,作者保留对其在Stack Overflow上做出的贡献的版权,但我们也授予永久许可,以便将这些特定的贡献用于任何目的,包括商业用途,唯一的要求是(a)需要署名和(b)需要共享。简而言之,是的,可以免费使用。 - Rob
1
@JAHelia - 不需要,因为我的AsynchronousOperation在执行完闭包后将其设置为nil,从而解决了任何强引用循环。 - Rob
1
@famfamfam - 在这种异步操作模式中,您可以创建一个新的操作,在此操作完成后执行所需操作,并使其依赖于各个网络请求的各个操作。 - Rob
1
@famfamfam:“我看到你设置了maxConcurrentOperationCount = 2,但你调用了50次请求…”这正是重点:OP想要排队50个请求,但从未同时运行超过两个。 maxConcurrentOperationCount只是规定了任何给定时间可以运行的数量。(您不希望太多同时运行,因为(a)URLSession一次只能运行那么多,因此您可能会冒着后来的请求超时的风险;和(b)内存影响。)以上实现了在排队许多请求时控制并发的程度。 - Rob
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接