Skip to content

Commit

Permalink
Merge pull request #27 from nickbabcock/sync-pg
Browse files Browse the repository at this point in the history
Convert to synchronous npgsql apis
  • Loading branch information
nickbabcock authored Dec 31, 2018
2 parents d7d65a9 + d006c1f commit 1a6970e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OhmGraphite.Test/TimescaleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async void CanSetupTimescale()
const string connStr = "Host=timescale;Username=postgres;Password=123456";
var writer = new TimescaleWriter(connStr, true, "my-pc");
var epoch = new DateTime(2001, 1, 13);
await writer.ReportMetrics(epoch, TestSensorCreator.Values());
writer.ReportMetrics(epoch, TestSensorCreator.Values());
using (var conn = new NpgsqlConnection(connStr))
{
conn.Open();
Expand All @@ -31,7 +31,7 @@ public async void InsertOnlyTimescale()
const string connStr = "Host=timescale;Username=ohm;Password=itsohm;Database=timescale_built";
var writer = new TimescaleWriter(connStr, false, "my-pc");
var epoch = new DateTime(2001, 1, 13);
await writer.ReportMetrics(epoch, TestSensorCreator.Values());
writer.ReportMetrics(epoch, TestSensorCreator.Values());
using (var conn = new NpgsqlConnection(selectStr))
{
conn.Open();
Expand Down
16 changes: 10 additions & 6 deletions OhmGraphite/TimescaleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TimescaleWriter(string connStr, bool setupTable, string localHost)
_conn = new NpgsqlConnection(_connStr);
}

public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
public Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
{
try
{
Expand All @@ -36,7 +36,7 @@ public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue>
_conn.Close();
_conn = new NpgsqlConnection(_connStr);
Logger.Debug("New timescale connection");
await _conn.OpenAsync();
_conn.Open();

// The reason behind unpreparing is a doozy.
//
Expand All @@ -45,7 +45,7 @@ public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue>
// the performance benefits to applications using connection pools" -
// http://www.roji.org/prepared-statements-in-npgsql-3-2. I have found this to
// be the correct behavior in 99% situations when either client or server is
// restart, as the normal flow of exceptions reported on the client when the
// restarted, as the normal flow of exceptions reported on the client when the
// server restarts seems to be:
//
// - System.IO.EndOfStreamException: Attempted to read past the end of the stream
Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue>
var setupSql = Resourcer.Resource.AsString("schema.sql");
using (var cmd = new NpgsqlCommand(setupSql, _conn))
{
await cmd.ExecuteNonQueryAsync();
cmd.ExecuteNonQuery();
}
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue>

// A majority of the time, the same number of sensors will be
// reported on, so it's important to prepare the statement
await cmd.PrepareAsync();
cmd.Prepare();

for (int i = 0; i < values.Count; i++)
{
Expand All @@ -119,10 +119,14 @@ public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue>
cmd.Parameters[$"sensor_index{i}"].Value = sensor.SensorIndex;
}

await cmd.ExecuteNonQueryAsync();
cmd.ExecuteNonQuery();
}

_failure = false;

// The synchronous versions of npgsql are more battle tested than asynchronous:
// https://github.com/npgsql/npgsql/issues/2266
return Task.CompletedTask;
}
catch (Exception)
{
Expand Down

0 comments on commit 1a6970e

Please sign in to comment.