This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathReportUploader.cs
More file actions
182 lines (155 loc) · 5.85 KB
/
Copy pathReportUploader.cs
File metadata and controls
182 lines (155 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace RustServerMetrics
{
class ReportUploader : MonoBehaviour
{
const int _sendBufferCapacity = 100000;
readonly Action _notifySubsequentNetworkFailuresAction;
readonly Action _notifySubsequentHttpFailuresAction;
readonly List<string> _sendBuffer = new List<string>(_sendBufferCapacity);
readonly StringBuilder _payloadBuilder = new StringBuilder();
bool _isRunning = false;
ushort _attempt = 0;
byte[] _data = null;
Uri _uri = null;
MetricsLogger _metricsLogger;
private char[] charBuffer = new char[8192 * 4];
bool _throttleNetworkErrorMessages = false;
uint _accumulatedNetworkErrors = 0;
bool _throttleHttpErrorMessages = false;
uint _accumulatedHttpErrors = 0;
public ushort BatchSize
{
get
{
var configVal = _metricsLogger.Configuration?.batchSize ?? 1000;
if (configVal < 1000) return 1000;
return configVal;
}
}
public bool IsRunning => _isRunning;
public int BufferSize => _sendBuffer.Count;
public ReportUploader()
{
_notifySubsequentNetworkFailuresAction = new Action(NotifySubsequentNetworkFailures);
_notifySubsequentHttpFailuresAction = new Action(NotifySubsequentHttpFailures);
}
void Awake()
{
_metricsLogger = GetComponent<MetricsLogger>();
if (_metricsLogger == null)
{
Debug.LogError("[ServerMetrics] ReportUploader failed to find the MetricsLogger component");
Destroy(this);
}
}
public void AddToSendBuffer(string payload)
{
if (_sendBuffer.Count == _sendBufferCapacity)
_sendBuffer.RemoveAt(0);
_sendBuffer.Add(payload);
if (!_isRunning)
StartCoroutine(SendBufferLoop());
}
IEnumerator SendBufferLoop()
{
_isRunning = true;
yield return null;
while (_sendBuffer.Count > 0 && _isRunning)
{
int amountToTake = Mathf.Min(_sendBuffer.Count, BatchSize);
for (int i = 0; i < amountToTake; i++)
{
_payloadBuilder.Append(_sendBuffer[i]);
_payloadBuilder.Append("\n");
}
_sendBuffer.RemoveRange(0, amountToTake);
_attempt = 0;
// more GC friendly GetBytes implementation
if (_payloadBuilder.Length > charBuffer.Length)
charBuffer = new char[_payloadBuilder.Length + 1024];
_payloadBuilder.CopyTo(0, charBuffer, 0, _payloadBuilder.Length);
_data = Encoding.UTF8.GetBytes(charBuffer, 0, _payloadBuilder.Length);
_uri = _metricsLogger.BaseUri;
_payloadBuilder.Clear();
yield return SendRequest();
}
_isRunning = false;
}
IEnumerator SendRequest()
{
var request = new UnityWebRequest(_uri, UnityWebRequest.kHttpVerbPOST)
{
uploadHandler = new UploadHandlerRaw(_data),
downloadHandler = new DownloadHandlerBuffer(),
timeout = 15,
useHttpContinue = true,
redirectLimit = 5
};
yield return request.SendWebRequest();
if (request.isNetworkError)
{
if (_attempt >= 2)
{
if (_throttleNetworkErrorMessages)
{
_accumulatedNetworkErrors += 1;
}
else
{
Debug.LogError($"Two consecutive network failures occurred while submitting a batch of metrics");
InvokeHandler.Invoke(this, _notifySubsequentNetworkFailuresAction, 5);
_throttleNetworkErrorMessages = true;
}
yield break;
}
_attempt++;
yield return SendRequest();
yield break;
}
if (request.isHttpError)
{
if (_throttleHttpErrorMessages)
{
_accumulatedHttpErrors += 1;
}
else
{
Debug.LogError($"A HTTP error occurred while submitting batch of metrics: {request.error}");
if (_metricsLogger.Configuration?.debugLogging == true) Debug.LogError(request.downloadHandler.text);
InvokeHandler.Invoke(this, _notifySubsequentHttpFailuresAction, 5);
_throttleHttpErrorMessages = true;
}
yield break;
}
}
void NotifySubsequentNetworkFailures()
{
_throttleNetworkErrorMessages = false;
if (_accumulatedNetworkErrors == 0) return;
Debug.LogError($"{_accumulatedNetworkErrors} subsequent network errors occurred in the last 5 seconds");
_accumulatedNetworkErrors = 0;
}
void NotifySubsequentHttpFailures()
{
_throttleHttpErrorMessages = false;
if (_accumulatedHttpErrors == 0) return;
Debug.LogError($"{_accumulatedHttpErrors} subsequent HTTP errors occurred in the last 5 seconds");
_accumulatedHttpErrors = 0;
}
void OnDestroy()
{
Stop();
}
public void Stop()
{
_isRunning = false;
StopAllCoroutines();
}
}
}