From 45fd1b2ff18aacc7b8fe3919d17dce2d1d1e0b98 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 30 May 2022 18:51:19 +0000 Subject: [PATCH] automatic subscription id --- web3-proxy/src/app.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/web3-proxy/src/app.rs b/web3-proxy/src/app.rs index 9212ee6c..81a9018e 100644 --- a/web3-proxy/src/app.rs +++ b/web3-proxy/src/app.rs @@ -15,6 +15,7 @@ use parking_lot::RwLock; use serde_json::json; use serde_json::value::RawValue; use std::fmt; +use std::sync::atomic::{self, AtomicUsize}; use std::sync::Arc; use std::time::Duration; use tokio::sync::watch; @@ -52,6 +53,7 @@ pub struct Web3ProxyApp { response_cache: ResponseLrcCache, // don't drop this or the sender will stop working head_block_receiver: watch::Receiver>, + next_subscription_id: AtomicUsize, } impl fmt::Debug for Web3ProxyApp { @@ -133,6 +135,7 @@ impl Web3ProxyApp { incoming_requests: Default::default(), response_cache: Default::default(), head_block_receiver, + next_subscription_id: 1.into(), }) } @@ -145,8 +148,11 @@ impl Web3ProxyApp { ) -> anyhow::Result<(AbortHandle, JsonRpcForwardedResponse)> { let (subscription_handle, subscription_registration) = AbortHandle::new_pair(); - // TODO: generate subscription_id as needed. atomic u16? - let subscription_id = "0xcd0c3e8af590364c09d0fa6a1210faf5".to_string(); + let subscription_id = self + .next_subscription_id + .fetch_add(1, atomic::Ordering::SeqCst); + + let subscription_id = format!("{:#x}", subscription_id); let f = { let head_block_receiver = self.head_block_receiver.clone();