internal static void EnqueueMixpanelQueue(PersistentQueue persistentQueue, Value data) { using (PersistentQueueSession session = persistentQueue.OpenSession()) { session.Enqueue(Encoding.UTF8.GetBytes(JsonUtility.ToJson(data))); Mixpanel.Put(data); session.Flush(); } }
private IEnumerator DoRequest(string url, PersistentQueue queue, int retryCount = 0) { int count = 0; Value batch = Mixpanel.ArrayPool.Get(); using (PersistentQueueSession session = queue.OpenSession()) { while (count < BatchSize) { byte[] data = session.Dequeue(); if (data == null) { break; } batch.Add(JsonUtility.FromJson <Value>(Encoding.UTF8.GetString(data))); ++count; } // If the batch is empty don't send the request if (count == 0) { yield break; } string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(batch.ToString())); if (MixpanelSettings.Instance.ShowDebug) { Debug.Log($"[Mixpanel] Sending Request - '{url}' with payload '{payload}'"); } WWWForm form = new WWWForm(); form.AddField("data", payload); UnityWebRequest request = UnityWebRequest.Post(url, form); yield return(request.SendWebRequest()); while (!request.isDone) { yield return(new WaitForEndOfFrame()); } if (!request.isNetworkError && !request.isHttpError) { session.Flush(); Mixpanel.Put(batch); yield break; } if (retryCount > RetryMaxTries) { yield break; } } retryCount += 1; // 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 = 2046 seconds total yield return(new WaitForSecondsRealtime((float)Math.Pow(2, retryCount))); StartCoroutine(DoRequest(url, queue, retryCount)); }
public void OnRecycle() { _string = ""; _bool = false; _number = 0; foreach (Value item in _array) { Mixpanel.Put(item); } _array.Clear(); foreach (Value value in _container.Values) { Mixpanel.Put(value); } _container.Clear(); }
private void LateUpdateEngageQueue() { if (EngageQueue.Count == 0) { return; } using (PersistentQueueSession session = Mixpanel.EngageQueue.OpenSession()) { foreach (Value item in EngageQueue) { session.Enqueue(Encoding.UTF8.GetBytes(JsonUtility.ToJson(item))); Mixpanel.Put(item); } session.Flush(); } EngageQueue.Clear(); }
internal static IEnumerator SendData(PersistentQueue persistentQueue, string url) { if (persistentQueue.CurrentCountOfItemsInQueue == 0) { yield break; } int depth = persistentQueue.CurrentCountOfItemsInQueue; int numBatches = (depth / Config.BatchSize) + (depth % Config.BatchSize != 0 ? 1 : 0); for (int i = 0; i < numBatches; i++) { if (_stopThread) { yield break; } Value batch = Mixpanel.ArrayPool.Get(); using (PersistentQueueSession session = persistentQueue.OpenSession()) { int count = 0; while (count < Config.BatchSize) { byte[] data = session.Dequeue(); if (data == null) { break; } batch.Add(JsonUtility.FromJson <Value>(Encoding.UTF8.GetString(data))); ++count; } if (count == 0) { yield break; } string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(batch.ToString())); Mixpanel.Log($"Sending Request - '{url}' with payload '{payload}'"); bool successful = false; int responseCode = -1; string response = null; if (_isBgThreadRunning) { try { var content = new StringContent("data=" + payload, Encoding.UTF8, "application/json"); var responseRequest = _client.PostAsync(url, content).Result; responseCode = (int)responseRequest.StatusCode; response = responseRequest.Content.ReadAsStringAsync().Result; } catch (Exception e) { Mixpanel.LogError("There was an error sending the request: " + e); } } else { WWWForm form = new WWWForm(); form.AddField("data", payload); UnityWebRequest request = UnityWebRequest.Post(url, form); yield return(request.SendWebRequest()); while (!request.isDone) { yield return(new WaitForEndOfFrame()); } responseCode = (int)request.responseCode; response = request.downloadHandler.text; } Mixpanel.Log($"Response - '{url}' was '{response}'"); successful = responseCode == (int)HttpStatusCode.OK; if (successful) { _retryCount = 0; session.Flush(); Mixpanel.Put(batch); } else { _retryCount += 1; double retryIn = Math.Pow(2, _retryCount) * 60000; retryIn = Math.Min(retryIn, 10 * 60 * 1000); // limit 10 min Mixpanel.Log("Retrying request in " + retryIn / 1000 + " seconds (retryCount=" + _retryCount + ")"); _retryTimer = new System.Threading.Timer((obj) => { ForceFlushOp(); _retryTimer.Dispose(); }, null, (int)retryIn, System.Threading.Timeout.Infinite); yield break; } } } }