Create method for obtaining UTC-prepared Postgres client.
authorArik Sosman <git@arik.io>
Thu, 17 Aug 2023 04:35:55 +0000 (21:35 -0700)
committerArik Sosman <git@arik.io>
Thu, 17 Aug 2023 20:21:57 +0000 (13:21 -0700)
src/lib.rs
src/lookup.rs
src/persistence.rs

index b6281bcb6836b58f7ecff75664ba6075bde8df13..550ed79ddcdac4d56f65e93e5ac94f7fa030bfa7 100644 (file)
@@ -20,6 +20,7 @@ use lightning::routing::gossip::{NetworkGraph, NodeId};
 use lightning::util::logger::Logger;
 use lightning::util::ser::{ReadableArgs, Writeable};
 use tokio::sync::mpsc;
+use tokio_postgres::{Client, NoTls};
 use crate::lookup::DeltaSet;
 
 use crate::persistence::GossipPersister;
@@ -110,6 +111,20 @@ impl<L: Deref + Clone + Send + Sync + 'static> RapidSyncProcessor<L> where L::Ta
        }
 }
 
+pub(crate) async fn connect_to_db() -> Client {
+       let connection_config = config::db_connection_config();
+       let (client, connection) = connection_config.connect(NoTls).await.unwrap();
+
+       tokio::spawn(async move {
+               if let Err(e) = connection.await {
+                       panic!("connection error: {}", e);
+               }
+       });
+
+       client.execute("set time zone UTC", &[]).await.unwrap();
+       client
+}
+
 /// This method generates a no-op blob that can be used as a delta where none exists.
 ///
 /// 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> {
 }
 
 async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, logger: L) -> SerializedResponse where L::Target: Logger {
-       let (client, connection) = lookup::connect_to_db().await;
+       let client = connect_to_db().await;
 
        network_graph.remove_stale_channels_and_tracking();
 
-       tokio::spawn(async move {
-               if let Err(e) = connection.await {
-                       panic!("connection error: {}", e);
-               }
-       });
-
        let mut output: Vec<u8> = vec![];
 
        // set a flag if the chain hash is prepended
index eb0c8eed688bf7dc0690387a6a35d70795c3b32d..696b4d041ab508b53c7074884a63b89047d4957a 100644 (file)
@@ -7,8 +7,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};
 use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
 use lightning::routing::gossip::NetworkGraph;
 use lightning::util::ser::Readable;
-use tokio_postgres::{Client, Connection, NoTls, Socket};
-use tokio_postgres::tls::NoTlsStream;
+use tokio_postgres::Client;
 
 use futures::StreamExt;
 use lightning::log_info;
@@ -68,11 +67,6 @@ impl Default for DirectedUpdateDelta {
        }
 }
 
-pub(super) async fn connect_to_db() -> (Client, Connection<Socket, NoTlsStream>) {
-       let connection_config = config::db_connection_config();
-       connection_config.connect(NoTls).await.unwrap()
-}
-
 /// Fetch all the channel announcements that are presently in the network graph, regardless of
 /// whether they had been seen before.
 /// Also include all announcements for which the first update was announced
index f638894dd3590c9d0c583452c7e03aba8275cb15..8bb7b188af72fb692aa218a20b1ede9bd24c116a 100644 (file)
@@ -8,7 +8,6 @@ use lightning::routing::gossip::NetworkGraph;
 use lightning::util::logger::Logger;
 use lightning::util::ser::Writeable;
 use tokio::sync::mpsc;
-use tokio_postgres::NoTls;
 
 use crate::config;
 use crate::types::GossipMessage;
@@ -33,15 +32,7 @@ impl<L: Deref> GossipPersister<L> where L::Target: Logger {
        }
 
        pub(crate) async fn persist_gossip(&mut self) {
-               let connection_config = config::db_connection_config();
-               let (mut client, connection) =
-                       connection_config.connect(NoTls).await.unwrap();
-
-               tokio::spawn(async move {
-                       if let Err(e) = connection.await {
-                               panic!("connection error: {}", e);
-                       }
-               });
+               let mut client = crate::connect_to_db().await;
 
                {
                        // initialize the database
@@ -57,6 +48,11 @@ impl<L: Deref> GossipPersister<L> where L::Target: Logger {
                                config::upgrade_db(cur_schema[0].get(0), &mut client).await;
                        }
 
+                       let preparation = client.execute("set time zone UTC", &[]).await;
+                       if let Err(preparation_error) = preparation {
+                               panic!("db preparation error: {}", preparation_error);
+                       }
+
                        let initialization = client
                                .execute(
                                        // TODO: figure out a way to fix the id value without Postgres complaining about