Skip to content
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

Adds sharding support #288

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Discord.Net/API/Client/GatewaySocket/Commands/Identify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class IdentifyCommand : IWebSocketMessage
public int LargeThreshold { get; set; }
[JsonProperty("compress")]
public bool UseCompression { get; set; }
[JsonProperty("shard")]
public int[] ShardingParams { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Discord.Net/DiscordConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class DiscordConfigBuilder
/// </summary>
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

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

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

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

private static string GetUserAgent(DiscordConfigBuilder builder)
Expand Down
3 changes: 3 additions & 0 deletions src/Discord.Net/Net/Rest/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public string Token
}
}

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

protected RestClient(DiscordConfig config, string baseUrl, ILogger logger = null)
{
_config = config;
Expand Down
10 changes: 6 additions & 4 deletions src/Discord.Net/Net/WebSockets/GatewaySocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task Connect(RestClient rest, CancellationToken parentCancelToken)
Host = url;
await BeginConnect(parentCancelToken).ConfigureAwait(false);
if (SessionId == null)
SendIdentify(_rest.Token);
SendIdentify(_rest.Token, _rest.ShardId, _rest.TotalShards);
else
SendResume();
}
Expand Down Expand Up @@ -148,7 +148,7 @@ protected override async Task ProcessMessage(string json)
}
}

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

QueueMessage(msg);
}

public void SendResume()
Expand Down