From e4ac5f4db1918a1bf935d8eba7a38a4bab77a953 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Thu, 23 Sep 2021 12:01:14 -0600 Subject: [PATCH] Stabalizes loadMany by using NIO whenAllSucceed Previously it could cause race conditions because of concurrent array access --- Sources/DataLoader/DataLoader.swift | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/Sources/DataLoader/DataLoader.swift b/Sources/DataLoader/DataLoader.swift index 5b457b4..d67e3c4 100644 --- a/Sources/DataLoader/DataLoader.swift +++ b/Sources/DataLoader/DataLoader.swift @@ -94,24 +94,8 @@ final public class DataLoader { guard !keys.isEmpty else { return eventLoopGroup.next().makeSucceededFuture([]) } - - let promise: EventLoopPromise<[Value]> = eventLoopGroup.next().makePromise() - - var result = [Value]() - let futures = try keys.map { try load(key: $0, on: eventLoopGroup) } - - for future in futures { - _ = future.map { value in - result.append(value) - - if result.count == keys.count { - promise.succeed(result) - } - } - } - - return promise.futureResult + return EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()) } /// Clears the value at `key` from the cache, if it exists. Returns itself for