@@ -16,6 +16,7 @@ import Data.List.NonEmpty (NonEmpty)
16
16
import Data.Map.Strict (Map )
17
17
import qualified Data.Map.Strict as M
18
18
import Data.Time.Clock (getCurrentTime )
19
+ import Data.Time.Clock.POSIX (getPOSIXTime )
19
20
import Data.Time.Clock.System (SystemTime )
20
21
import Data.X509.Validation (Fingerprint (.. ))
21
22
import Network.Socket (ServiceName )
@@ -33,7 +34,7 @@ import Simplex.Messaging.Server.Stats
33
34
import Simplex.Messaging.Server.StoreLog
34
35
import Simplex.Messaging.TMap (TMap )
35
36
import qualified Simplex.Messaging.TMap as TM
36
- import Simplex.Messaging.Transport (ATransport , VersionRangeSMP , VersionSMP )
37
+ import Simplex.Messaging.Transport (ATransport , PeerId , VersionRangeSMP , VersionSMP )
37
38
import Simplex.Messaging.Transport.Server (SocketState , TransportServerConfig , alpn , loadFingerprint , loadTLSServerParams , newSocketState )
38
39
import System.IO (IOMode (.. ))
39
40
import System.Mem.Weak (Weak )
@@ -70,6 +71,10 @@ data ServerConfig = ServerConfig
70
71
serverStatsLogFile :: FilePath ,
71
72
-- | file to save and restore stats
72
73
serverStatsBackupFile :: Maybe FilePath ,
74
+ -- | rate limit monitoring interval / bucket width, seconds
75
+ rateStatsInterval :: Maybe Int64 ,
76
+ rateStatsLogFile :: FilePath ,
77
+ rateStatsBackupFile :: Maybe FilePath ,
73
78
-- | CA certificate private key is not needed for initialization
74
79
caCertificateFile :: FilePath ,
75
80
privateKeyFile :: FilePath ,
@@ -109,6 +114,8 @@ data Env = Env
109
114
storeLog :: Maybe (StoreLog 'WriteMode),
110
115
tlsServerParams :: T. ServerParams ,
111
116
serverStats :: ServerStats ,
117
+ qCreatedByIp :: Timeline ,
118
+ msgSentByIp :: Timeline ,
112
119
sockets :: SocketState ,
113
120
clientSeq :: TVar Int ,
114
121
clients :: TVar (IntMap Client )
@@ -124,6 +131,8 @@ data Server = Server
124
131
125
132
data Client = Client
126
133
{ clientId :: Int ,
134
+ peerId :: PeerId , -- send updates for this Id to time series
135
+ clientStats :: ClientStats , -- capture final values on disconnect
127
136
subscriptions :: TMap RecipientId (TVar Sub ),
128
137
ntfSubscriptions :: TMap NotifierId () ,
129
138
rcvQ :: TBQueue (NonEmpty (Maybe QueueRec , Transmission Cmd )),
@@ -155,8 +164,8 @@ newServer = do
155
164
savingLock <- createLock
156
165
return Server {subscribedQ, subscribers, ntfSubscribedQ, notifiers, savingLock}
157
166
158
- newClient :: TVar Int -> Natural -> VersionSMP -> ByteString -> SystemTime -> STM Client
159
- newClient nextClientId qSize thVersion sessionId createdAt = do
167
+ newClient :: PeerId -> TVar Int -> Natural -> VersionSMP -> ByteString -> SystemTime -> STM Client
168
+ newClient peerId nextClientId qSize thVersion sessionId createdAt = do
160
169
clientId <- stateTVar nextClientId $ \ next -> (next, next + 1 )
161
170
subscriptions <- TM. empty
162
171
ntfSubscriptions <- TM. empty
@@ -168,7 +177,8 @@ newClient nextClientId qSize thVersion sessionId createdAt = do
168
177
connected <- newTVar True
169
178
rcvActiveAt <- newTVar createdAt
170
179
sndActiveAt <- newTVar createdAt
171
- return Client {clientId, subscriptions, ntfSubscriptions, rcvQ, sndQ, msgQ, endThreads, endThreadSeq, thVersion, sessionId, connected, createdAt, rcvActiveAt, sndActiveAt}
180
+ clientStats <- ClientStats <$> newTVar 0 <*> newTVar 0
181
+ return Client {clientId, subscriptions, ntfSubscriptions, rcvQ, sndQ, msgQ, endThreads, endThreadSeq, thVersion, sessionId, connected, createdAt, rcvActiveAt, sndActiveAt, peerId, clientStats}
172
182
173
183
newSubscription :: SubscriptionThread -> STM Sub
174
184
newSubscription subThread = do
@@ -189,7 +199,10 @@ newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile,
189
199
sockets <- atomically newSocketState
190
200
clientSeq <- newTVarIO 0
191
201
clients <- newTVarIO mempty
192
- return Env {config, server, serverIdentity, queueStore, msgStore, random, storeLog, tlsServerParams, serverStats, sockets, clientSeq, clients}
202
+ now <- getPOSIXTime
203
+ qCreatedByIp <- atomically $ newTimeline perMinute now
204
+ msgSentByIp <- atomically $ newTimeline perMinute now
205
+ return Env {config, server, serverIdentity, queueStore, msgStore, random, storeLog, tlsServerParams, serverStats, qCreatedByIp, msgSentByIp, sockets, clientSeq, clients}
193
206
where
194
207
restoreQueues :: QueueStore -> FilePath -> IO (StoreLog 'WriteMode)
195
208
restoreQueues QueueStore {queues, senders, notifiers} f = do
0 commit comments