Refactor token buckets to meet current coding standards

Our existing token bucket code is a mess. We should clean it up before we tackle legacy/trac#25373 (moved).