                               (warn-failure e events)
                               ::exception))]
                 (when (#{::exception} result)
-                  (swap! timeout-count inc)
-                  (recur (next-sleep-time sleep)))))))))))
+                  (recur (inc timeout-count) (next-sleep-time sleep)))))))))))

(defn newrelic-sender [{:keys [newrelic/batch-size newrelic/batch-timeout newrelic/license-key]} ch shutdown?]
  (let [batch-size (or batch-size 50)
        batch-timeout (or batch-timeout 500)
        timeout-count (atom 0)
        license-key (or (System/getenv "NEW_RELIC_LICENSE_KEY") 
                        license-key
                        (throw (Exception. "NewRelic license key must be supplied via the NEW_RELIC_LICENSE_KEY environment variable or :newrelic/license-key")))] 

                  (not @shutdown?)) 
        (let [events (read-batch ch batch-size batch-timeout)
              events-by-period (group-by (juxt :job-name :period) events)]
          (if (empty? events) 
            (Thread/sleep batch-timeout)
            (loop [timeout-count 0 
                   sleep 0]
              ;; Exponential backoff to rate limit errors
              (when-not (zero? sleep) 
                (info (format "Message send timeout count %s. Backing off %s." timeout-count sleep))
                (Thread/sleep sleep))

              (let [result (try
