Browse Source

Merge pull request #288 from moiph/moiph/sharding

Adds sharding support
pull/303/head
RogueException GitHub 8 years ago
parent
commit
42f2573358
4 changed files with 21 additions and 4 deletions
  1. +2
    -0
      src/Discord.Net/API/Client/GatewaySocket/Commands/Identify.cs
  2. +10
    -0
      src/Discord.Net/DiscordConfig.cs
  3. +3
    -0
      src/Discord.Net/Net/Rest/RestClient.cs
  4. +6
    -4
      src/Discord.Net/Net/WebSockets/GatewaySocket.cs

+ 2
- 0
src/Discord.Net/API/Client/GatewaySocket/Commands/Identify.cs View File

@@ -18,5 +18,7 @@ namespace Discord.API.Client.GatewaySocket
public int LargeThreshold { get; set; } public int LargeThreshold { get; set; }
[JsonProperty("compress")] [JsonProperty("compress")]
public bool UseCompression { get; set; } public bool UseCompression { get; set; }
[JsonProperty("shard")]
public int[] ShardingParams { get; set; }
} }
} }

+ 10
- 0
src/Discord.Net/DiscordConfig.cs View File

@@ -47,6 +47,11 @@ namespace Discord
/// </summary> /// </summary>
public int LargeThreshold { get; set; } = 250; public int LargeThreshold { get; set; } = 250;


/// <summary> Gets or sets the id for this shard. Must be less than TotalShards. </summary>
public int ShardId { get; set; } = 0;
/// <summary> Gets or sets the total number of shards for this application. </summary>
public int TotalShards { get; set; } = 1;

//Events //Events


/// <summary> Gets or sets a handler for all log messages. </summary> /// <summary> Gets or sets a handler for all log messages. </summary>
@@ -89,6 +94,9 @@ namespace Discord
public bool UsePermissionsCache { get; } public bool UsePermissionsCache { get; }
public bool EnablePreUpdateEvents { get; } public bool EnablePreUpdateEvents { get; }


public int ShardId { get; }
public int TotalShards { get; }

internal DiscordConfig(DiscordConfigBuilder builder) internal DiscordConfig(DiscordConfigBuilder builder)
{ {
LogLevel = builder.LogLevel; LogLevel = builder.LogLevel;
@@ -106,6 +114,8 @@ namespace Discord
MessageCacheSize = builder.MessageCacheSize; MessageCacheSize = builder.MessageCacheSize;
UsePermissionsCache = builder.UsePermissionsCache; UsePermissionsCache = builder.UsePermissionsCache;
EnablePreUpdateEvents = builder.EnablePreUpdateEvents; EnablePreUpdateEvents = builder.EnablePreUpdateEvents;
ShardId = builder.ShardId;
TotalShards = builder.TotalShards;
} }


private static string GetUserAgent(DiscordConfigBuilder builder) private static string GetUserAgent(DiscordConfigBuilder builder)


+ 3
- 0
src/Discord.Net/Net/Rest/RestClient.cs View File

@@ -51,6 +51,9 @@ namespace Discord.Net.Rest
} }
} }


public int ShardId => _config.ShardId;
public int TotalShards => _config.TotalShards;

protected RestClient(DiscordConfig config, string baseUrl, ILogger logger = null) protected RestClient(DiscordConfig config, string baseUrl, ILogger logger = null)
{ {
_config = config; _config = config;


+ 6
- 4
src/Discord.Net/Net/WebSockets/GatewaySocket.cs View File

@@ -50,7 +50,7 @@ namespace Discord.Net.WebSockets
Host = url; Host = url;
await BeginConnect(parentCancelToken).ConfigureAwait(false); await BeginConnect(parentCancelToken).ConfigureAwait(false);
if (SessionId == null) if (SessionId == null)
SendIdentify(_rest.Token);
SendIdentify(_rest.Token, _rest.ShardId, _rest.TotalShards);
else else
SendResume(); SendResume();
} }
@@ -148,7 +148,7 @@ namespace Discord.Net.WebSockets
} }
} }


public void SendIdentify(string token)
public void SendIdentify(string token, int shardId = 0, int totalShards = 1)
{ {
var props = new Dictionary<string, string> var props = new Dictionary<string, string>
{ {
@@ -159,9 +159,11 @@ namespace Discord.Net.WebSockets
Token = token, Token = token,
Properties = props, Properties = props,
LargeThreshold = _config.LargeThreshold, LargeThreshold = _config.LargeThreshold,
UseCompression = true
UseCompression = true,
ShardingParams = new int[] { shardId, totalShards },
}; };
QueueMessage(msg);

QueueMessage(msg);
} }


public void SendResume() public void SendResume()


Loading…
Cancel
Save