Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-53558

Make variadic whenAny implementation

    • Fully Compatible
    • Service Arch 2021-02-08, Service Arch 2021-02-22

      whenAny is a function that takes in a vector of futures and returns a future that is resolved as soon as one of its inputs resolves. Because it takes a vector, users sometimes have to first construct a vector prior to using the function, which causes additional lines of code and also can be a hassle since the vector will likely contain ExecutorFutures, which are not copyable and so an initializer list cannot be used for vector construction (and leads to complicated compiler errors).

      So currently you have to write something like:

      // Does not compile:
      // auto futures = {std::move(deadlineReachedFuture), std::move(someOtherFuture)};
      // Does not compile for same reason:
      //whenAny({std::move(deadlineReachedFuture), std::move(someOtherFuture)}).thenRunOn(executor).then(...);
      
      std::vector<ExecutorFuture<void>> futures;
      futures.emplace_back(std::move(deadlineReachedFuture));
      futures.emplace_back(std::move(someOtherFuture));
      whenAny(futures).thenRunOn(executor).then(...);
      

      I think it would be worth making a variadic version of whenAny, so that you could instead do:

      whenAny(std::move(deadlineReachedFuture), std::move(someOtherFuture))
          .thenRunOn(executor)
          .then(...);
      

      Since a two-future usage of whenAny seems like it will be a common case (especially with deadlines), we could also choose to just have a single special case that just takes two parameters. I think if we chose the latter, however, it might make sense to make it something different entirely and have it support different input types for the different futures.

            Assignee:
            tyler.seip@mongodb.com Tyler Seip (Inactive)
            Reporter:
            matthew.saltz@mongodb.com Matthew Saltz (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: