-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for servers to periodically communicate with external services #3446
Draft
amoeba
wants to merge
7
commits into
ACEmulator:master
Choose a base branch
from
amoeba:heartbeatmanager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
99da8fb
Create partial implementation of HeartbeatManager
amoeba b2002cd
Refactor HeartbeatManager to use config
amoeba a36bc82
Tweak heartbeat payload
amoeba a55a2b4
Fix doc on HeartbeatManager.Tick()
amoeba 727bf77
Tweak exception text in HeartbeatManager
amoeba 3e1aae0
Fix HeartbeatManager config not working
amoeba 52fb5cb
Do HeartbeatManager request in a one-off thread
amoeba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace ACE.Common | ||
{ | ||
public struct HeartbeatDefaults | ||
{ | ||
/// <summary> | ||
/// Whether hearbeats are enabled | ||
/// </summary> | ||
[System.ComponentModel.DefaultValue(true)] | ||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
public bool Enabled { get; set; } | ||
|
||
/// <summary> | ||
/// The heartbeat endpoint | ||
/// </summary> | ||
[System.ComponentModel.DefaultValue("https://heartbeat.treestats.net")] | ||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
public string Endpoint { get; set; } | ||
|
||
/// <summary> | ||
/// The heartbeat interval, in seconds | ||
/// </summary> | ||
[System.ComponentModel.DefaultValue(500)] | ||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] | ||
public int Interval { get; set; } | ||
} | ||
} |
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,96 @@ | ||
using System; | ||
|
||
using log4net; | ||
|
||
using ACE.Common.Performance; | ||
using System.Net.Http; | ||
using System.Text; | ||
using ACE.Common; | ||
using Newtonsoft.Json; | ||
|
||
namespace ACE.Server.Managers | ||
{ | ||
public static class HeartbeatManager | ||
{ | ||
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
|
||
/// <summary> | ||
/// The rate at which HeartbeatManager.Tick() executes | ||
/// </summary> | ||
private static RateLimiter updateHeartbeatManagerRateLimiter; | ||
|
||
/// <summary> | ||
/// Endpoint to send heartbeats to | ||
/// </summary> | ||
private static Uri endpoint; | ||
|
||
public static void Initialize() | ||
{ | ||
//if (!ConfigManager.Config.Server.Heartbeat.Enabled) | ||
//{ | ||
// log.Debug("Not starting HeartbeatManager because it's disabled in config"); | ||
|
||
// return; | ||
//} | ||
|
||
// endpoint = new Uri(ConfigManager.Config.Server.Heartbeat.Endpoint); | ||
endpoint = new Uri("https://treestats-servers.herokuapp.com/"); | ||
|
||
// updateHeartbeatManagerRateLimiter = new RateLimiter(1, TimeSpan.FromSeconds(ConfigManager.Config.Server.Heartbeat.Interval)); | ||
updateHeartbeatManagerRateLimiter = new RateLimiter(1, TimeSpan.FromSeconds(10)); | ||
} | ||
|
||
/// <summary> | ||
/// One-off class to help serialize the heartbeat's JSON payload | ||
/// </summary> | ||
private class HeartbeatPayload | ||
{ | ||
public string WorldName; | ||
public int OnlineCount; | ||
} | ||
|
||
/// <summary> | ||
/// Runs at intervals according to config | ||
/// </summary> | ||
public static void Tick() | ||
{ | ||
|
||
if (updateHeartbeatManagerRateLimiter.GetSecondsToWaitBeforeNextEvent() > 0) | ||
return; | ||
|
||
log.Debug($"HeartbeatManager.Tick()"); | ||
|
||
updateHeartbeatManagerRateLimiter.RegisterEvent(); | ||
DoHeartbeat(); | ||
} | ||
|
||
public static void DoHeartbeat() | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
HttpResponseMessage response; | ||
|
||
try | ||
{ | ||
HeartbeatPayload body = new HeartbeatPayload | ||
{ | ||
OnlineCount = PlayerManager.GetOnlineCount(), | ||
WorldName = ConfigManager.Config.Server.WorldName | ||
}; | ||
|
||
HttpContent content = new StringContent(JsonConvert.SerializeObject(body)); | ||
response = client.PostAsync(endpoint, content).Result; | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
log.Debug("Heartbeat Failed: " + response.Content); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
log.Debug("Exception while sending Heartbeat: " + e.Message); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing Result will block. Is it safe to do this on this thread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought this was fine but can change if this is problematic. Thanks for catching this @enknamel