mixpanel / mixpanel-python

Official Mixpanel Python library.
https://mixpanel.com/help/reference/python
Other
103 stars 85 forks source link

I got `Remote end closed connection without response` error when I use Mixpanel with Slack Bolt SDK #119

Closed 0417taehyun closed 1 year ago

0417taehyun commented 1 year ago

mixpanel.MixpanelException: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

I got the error message like the one above when I use Mixpanel with Slack Bolt SDK. I handle the events from Slack by using Socket Mode and collect theses with Mixpanel. However, when I try to collect all the events, the one above error message was returned every 30 minutes.

Are there anyways to solve this problem, or did I miss something?

illpack commented 1 year ago

Same problem here - any hints?

0417taehyun commented 1 year ago

@illpack

Hi, I solved this issue to change the Connection header argument. The default value is keep-alive, and I change it to close.

illpack commented 1 year ago

Hi @0417taehyun I am glad you managed to solve it! I am using the Mixpanel client for Python, but can't see a clear reference to your solution, could you maybe provide a small example of how you applied it?

This is my implementation:

from mixpanel import Consumer, Mixpanel

class MixpanelClient:
    def __init__(self):
        self.client = Mixpanel(
            MXP_TOKEN,
            consumer=Consumer(api_host=MXP_HOST),
        )
        self.api_key = MXP_API_KEY

    async def track(self, username, event_name, data={}, request=None):
        [...]
        self.client.track(username, event_name=event_name, properties=data)

Thanks a lot!

0417taehyun commented 1 year ago

Hi, @illpack !

I am sorry that I just describe my solution roughly. I recommend to use requests package, and call the API through the URL instead of SDK.

The below one is an example source code.

from requests import Response, Session

class Mixpanel:
    def __init__(self):
        self.session: Sesison = Session()
        self.adapter: HTTPAdapter = HTTPAdapter(max_retries=20)
        self.session.mount("htttps://", adapter=self.adapter)
        self.session.mount("http://", adapter=self.adapter)        

    def track(self, username, event_name, data={}, request=None):
        response: Response = self.session.post(
              url="https://api.mixpanel.com/track",
              headers={
                  "Accept": "text/plain",
                  "Content-Type": "application/json",
                  "Connection": "close".   # This header is my solution.
              },
              json=[
                  {
                      "event": YOUR_EVENT_NAME,
                      "properties": {
                          "token": YOUR_TOKEN,
                          ...
                      },
                  }
              ],
        )

If you want to use asynchronous HTTP requests, you can use aiohttp package instead of requests. I think there are no ways to change the header parameters if you use the SDK.

You can check the all API lists from the documents, and if you want to use the track method -API-, check Track Events

illpack commented 1 year ago

Thanks a lot @0417taehyun that looks reasonable, I will try your approach.

0417taehyun commented 1 year ago

@illpack

I am glad I could help you! Would you mind sharing whether my approach works for your situation or not? I am curious to know if it's successful.

illpack commented 1 year ago

Sure thing @0417taehyun I will give it a try shortly and monitor it and keep you posted!

illpack commented 1 year ago

Hi @0417taehyun I have deployed your solution this morning and it seems to have solved the issue, it has not ocurred again in the last 4 hours. Thank you!! 🎉

0417taehyun commented 1 year ago

Hi, @illpack !

It is glad to hear that you solve the issue! Have a nice day 😄