Mixpanel's Buffered Consumer in Fast API

45 Views Asked by At

I was having issues with the “Connection reset by peer” error when implementing Mixpanel's event tracking. I used the same structure as displayed in the documentation (Server-side best practices).

The support team told us that to solve this issue, the following code would be a viable solution:

mp.track("","test0")
mp.track("","test1")
mp.track("","test2")
mp.track("","test3")

def send_data(mpconsumer, tries=0):
    try:
        mpconsumer.flush()
    except mixpanel.MixpanelException as mpe:
        tries+= 1
        if(tries >= 3):
            print('failed sending; not trying anymore')
            print(mpe.message) # print queue of events
            return
        else:
            print(f'retrying {tries}')
            send_data(mpconsumer,tries)

send_data(mpconsumer)

However, due to the fact we are using FastAPI, I was not able to think of a way to keep a counter on the number of calls to the Mixpanel.track() function which would determine when to call the send_data function.

What flushing strategy should I use? Counter-based, time-based, hybrid of both?

0

There are 0 best solutions below