forked from CDAGaming/DiscordIPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Change] Implement re-connecting to IPC Pipe if disconnected
- This change integrates the Backoff logic from the Official RPC SDK, so that in the event of a disconnection, the Client will attempt to reconnect in subsequent `connect` executions, with increasing delays between `0.5 seconds` and `one minute` - This only changes the behavior for an unexpected disconnection (Such as sending a Presence update while not connected to the RPC), it does not impact a manual close (`onClose`)
- Loading branch information
Showing
2 changed files
with
66 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.jagrosh.discordipc.impl; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Backoff { | ||
private final long minAmount; | ||
private final long maxAmount; | ||
private long current; | ||
private int fails; | ||
private final Random randGenerator; | ||
|
||
public Backoff(long min, long max) { | ||
this.minAmount = min; | ||
this.maxAmount = max; | ||
this.current = min; | ||
this.fails = 0; | ||
this.randGenerator = new Random(); | ||
} | ||
|
||
public void reset() { | ||
fails = 0; | ||
current = minAmount; | ||
} | ||
|
||
public long nextDelay() { | ||
fails++; | ||
double delay = current * 2.0 * rand01(); | ||
current = Math.min(current + (long) delay, maxAmount); | ||
return current; | ||
} | ||
|
||
private double rand01() { | ||
return randGenerator.nextDouble(); | ||
} | ||
} |