Skip to content

Commit c928246

Browse files
committed
Create method for obtaining UTC-prepared Postgres client.
1 parent 0ac9345 commit c928246

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use lightning::routing::gossip::{NetworkGraph, NodeId};
2020
use lightning::util::logger::Logger;
2121
use lightning::util::ser::{ReadableArgs, Writeable};
2222
use tokio::sync::mpsc;
23+
use tokio_postgres::{Client, NoTls};
2324
use crate::lookup::DeltaSet;
2425

2526
use crate::persistence::GossipPersister;
@@ -110,6 +111,20 @@ impl<L: Deref + Clone + Send + Sync + 'static> RapidSyncProcessor<L> where L::Ta
110111
}
111112
}
112113

114+
pub(crate) async fn connect_to_db() -> Client {
115+
let connection_config = config::db_connection_config();
116+
let (client, connection) = connection_config.connect(NoTls).await.unwrap();
117+
118+
tokio::spawn(async move {
119+
if let Err(e) = connection.await {
120+
panic!("connection error: {}", e);
121+
}
122+
});
123+
124+
client.execute("set time zone UTC", &[]).await.unwrap();
125+
client
126+
}
127+
113128
/// This method generates a no-op blob that can be used as a delta where none exists.
114129
///
115130
/// The primary purpose of this method is the scenario of a client retrieving and processing a
@@ -142,16 +157,10 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
142157
}
143158

144159
async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, logger: L) -> SerializedResponse where L::Target: Logger {
145-
let (client, connection) = lookup::connect_to_db().await;
160+
let client = connect_to_db().await;
146161

147162
network_graph.remove_stale_channels_and_tracking();
148163

149-
tokio::spawn(async move {
150-
if let Err(e) = connection.await {
151-
panic!("connection error: {}", e);
152-
}
153-
});
154-
155164
let mut output: Vec<u8> = vec![];
156165

157166
// set a flag if the chain hash is prepended

src/lookup.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};
77
use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
88
use lightning::routing::gossip::NetworkGraph;
99
use lightning::util::ser::Readable;
10-
use tokio_postgres::{Client, Connection, NoTls, Socket};
11-
use tokio_postgres::tls::NoTlsStream;
10+
use tokio_postgres::Client;
1211

1312
use futures::StreamExt;
1413
use lightning::log_info;
@@ -68,11 +67,6 @@ impl Default for DirectedUpdateDelta {
6867
}
6968
}
7069

71-
pub(super) async fn connect_to_db() -> (Client, Connection<Socket, NoTlsStream>) {
72-
let connection_config = config::db_connection_config();
73-
connection_config.connect(NoTls).await.unwrap()
74-
}
75-
7670
/// Fetch all the channel announcements that are presently in the network graph, regardless of
7771
/// whether they had been seen before.
7872
/// Also include all announcements for which the first update was announced

src/persistence.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use lightning::routing::gossip::NetworkGraph;
88
use lightning::util::logger::Logger;
99
use lightning::util::ser::Writeable;
1010
use tokio::sync::mpsc;
11-
use tokio_postgres::NoTls;
1211

1312
use crate::config;
1413
use crate::types::GossipMessage;
@@ -33,15 +32,7 @@ impl<L: Deref> GossipPersister<L> where L::Target: Logger {
3332
}
3433

3534
pub(crate) async fn persist_gossip(&mut self) {
36-
let connection_config = config::db_connection_config();
37-
let (mut client, connection) =
38-
connection_config.connect(NoTls).await.unwrap();
39-
40-
tokio::spawn(async move {
41-
if let Err(e) = connection.await {
42-
panic!("connection error: {}", e);
43-
}
44-
});
35+
let mut client = crate::connect_to_db().await;
4536

4637
{
4738
// initialize the database
@@ -57,6 +48,11 @@ impl<L: Deref> GossipPersister<L> where L::Target: Logger {
5748
config::upgrade_db(cur_schema[0].get(0), &mut client).await;
5849
}
5950

51+
let preparation = client.execute("set time zone UTC", &[]).await;
52+
if let Err(preparation_error) = preparation {
53+
panic!("db preparation error: {}", preparation_error);
54+
}
55+
6056
let initialization = client
6157
.execute(
6258
// TODO: figure out a way to fix the id value without Postgres complaining about

0 commit comments

Comments
 (0)