Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve WebSocket health monitoring.
- Loading branch information
1 parent
fc6db45
commit 712b0c1
Showing
20 changed files
with
558 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/org/thoughtcrime/securesms/net/HttpErrorTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.thoughtcrime.securesms.net; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Track error occurrences by time and indicate if too many occur within the | ||
* time limit. | ||
*/ | ||
public final class HttpErrorTracker { | ||
|
||
private final long[] timestamps; | ||
private final long errorTimeRange; | ||
|
||
public HttpErrorTracker(int samples, long errorTimeRange) { | ||
this.timestamps = new long[samples]; | ||
this.errorTimeRange = errorTimeRange; | ||
} | ||
|
||
public synchronized boolean addSample(long now) { | ||
long errorsMustBeAfter = now - errorTimeRange; | ||
int count = 1; | ||
int minIndex = 0; | ||
|
||
for (int i = 0; i < timestamps.length; i++) { | ||
if (timestamps[i] < errorsMustBeAfter) { | ||
timestamps[i] = 0; | ||
} else if (timestamps[i] != 0) { | ||
count++; | ||
} | ||
|
||
if (timestamps[i] < timestamps[minIndex]) { | ||
minIndex = i; | ||
} | ||
} | ||
|
||
timestamps[minIndex] = now; | ||
|
||
if (count >= timestamps.length) { | ||
Arrays.fill(timestamps, 0); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.