Skip to content

Commit a854e4d

Browse files
authored
Merge pull request #36 from peppy/add-async-connect-method
Add `async` version of database connect method
2 parents e26c865 + d921a29 commit a854e4d

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

osu.Server.QueueProcessor/DatabaseAccess.cs

+40-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using MySqlConnector;
68

79
namespace osu.Server.QueueProcessor
@@ -15,6 +17,41 @@ public static class DatabaseAccess
1517
/// Retrieve a fresh MySQL connection. Should be disposed after use.
1618
/// </summary>
1719
public static MySqlConnection GetConnection()
20+
{
21+
var connection = new MySqlConnection(getConnectionString());
22+
23+
connection.Open();
24+
25+
// TODO: remove this when we have set a saner time zone server-side.
26+
using (var cmd = connection.CreateCommand())
27+
{
28+
cmd.CommandText = "SET time_zone = '+00:00';";
29+
cmd.ExecuteNonQuery();
30+
}
31+
32+
return connection;
33+
}
34+
35+
/// <summary>
36+
/// Retrieve a fresh MySQL connection. Should be disposed after use.
37+
/// </summary>
38+
public static async Task<MySqlConnection> GetConnectionAsync(CancellationToken cancellationToken)
39+
{
40+
var connection = new MySqlConnection(getConnectionString());
41+
42+
await connection.OpenAsync(cancellationToken);
43+
44+
// TODO: remove this when we have set a saner time zone server-side.
45+
using (var cmd = connection.CreateCommand())
46+
{
47+
cmd.CommandText = "SET time_zone = '+00:00';";
48+
await cmd.ExecuteNonQueryAsync(cancellationToken);
49+
}
50+
51+
return connection;
52+
}
53+
54+
private static string getConnectionString()
1855
{
1956
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") ?? String.Empty;
2057

@@ -31,20 +68,11 @@ public static MySqlConnection GetConnection()
3168
string passwordString = string.IsNullOrEmpty(password) ? string.Empty : $"Password={password};";
3269

3370
// Pipelining disabled because ProxySQL no like.
34-
connectionString = $"Server={host};Port={port};Database={name};User ID={user};{passwordString}ConnectionTimeout=5;ConnectionReset=false;Pooling={pooling};Max Pool Size={maxPoolSize}; Pipelining=false";
71+
connectionString =
72+
$"Server={host};Port={port};Database={name};User ID={user};{passwordString}ConnectionTimeout=5;ConnectionReset=false;Pooling={pooling};Max Pool Size={maxPoolSize}; Pipelining=false";
3573
}
3674

37-
var connection = new MySqlConnection(connectionString);
38-
connection.Open();
39-
40-
// TODO: remove this when we have set a saner time zone server-side.
41-
using (var cmd = connection.CreateCommand())
42-
{
43-
cmd.CommandText = "SET time_zone = '+00:00';";
44-
cmd.ExecuteNonQuery();
45-
}
46-
47-
return connection;
75+
return connectionString;
4876
}
4977
}
5078
}

0 commit comments

Comments
 (0)