private Directory addMergeRateLimiters(Directory in) {
    return new FilterDirectory(in) {
      @Override
      public IndexOutput createOutput(String name, IOContext context) throws IOException {
        ensureOpen();

        // Paranoia defense: if this trips we have a bug somewhere...
        IndexWriter.this.ensureOpen(false);

        // This Directory is only supposed to be used during merging,
        // so all writes should have MERGE context, else there is a bug 
        // somewhere that is failing to pass down the right IOContext:
        assert context.context == IOContext.Context.MERGE: "got context=" + context.context;

        MergeRateLimiter rateLimiter = rateLimiters.get();
        assert rateLimiter != null;

        return new RateLimitedIndexOutput(rateLimiter, in.createOutput(name, context));
      }
    };
  }