Once and only once Try it

Have you ever wanted to only do something once? Perhaps you've been trying to send an activation event to MixPanel, or perhaps you're trying to ensure that you only ever send an emails one time, whatever the case you probably felt the need to create a new table, build a model for it and store a teeny boolean to mark that you've done the thing. Annoying right?

But of course that's only half the battle. To actually use it you'd need something that reads that table, then depending on your use case you might also want to wrap some caching in there so things don't get slow. Wouldn't it be neat if all this were easy?

Well now it is easy.

limiter = RateLimit::Limiter.new(apikey: "ACCT_ID|APIKEY")

# only need to do this on startup
limiter.upsert_limit("send_email", 1, RateLimIt::INFINITE)

# only pass the check once per user
if limiter.check?("send_email:welcome_email:user12312")
  really_send_the_email()
end
ApiClient apiClient = new ApiClient.Builder()
        .build();

apiClient.limitCreate(RateLimitProtos.LimitDefinition.newBuilder()
        .setGroup("send_email")
        .setLimit(1)
        .setPolicyName(RateLimitProtos.LimitResponse.LimitPolicyNames.INFINITE)
        .build());

if(apiClient.isPass("send_email:welcome_email:user12312")){
        reallySendTheEmail();
}

Say you're interested in only sending the first experiment expsosure event to MixPanel:

limiter = RateLimit::Limiter.new(apikey: "1|MYKEY")
limiter.upsert_limit("event:experiment", 1, RateLimIt::INFINITE) # only send things with the same key once

def track_event(event_props)
   key_parts = ['events',
         event_props[:event_name],
         event_props[:tracking_id],
         props[:experiment_test],
         props[:experiment_result]]

   key = key_parts.join(":")
   if RatelimIt::ApiClient.instance.limiter.pass?(key)
     really_track(event_props)
   end
end
Try it