on master help sign in
Use STM retry for instant poll notifications instead of sleep-based polling loop
4543779iko
client/ApiClient.fs+1−1
@@ -55,7 +55,7 @@ module ApiClient =
55
55
56 let private pollClient =
56 let private pollClient =
57 let c = new HttpClient()
57 let c = new HttpClient()
58− c.Timeout <- TimeSpan.FromSeconds(120.)
58+ c.Timeout <- TimeSpan.FromSeconds(30.)
59 c
59 c
60
60
61 let private sendRequest (httpClient: HttpClient) (url: string) (body: string) (token: string option) =
61 let private sendRequest (httpClient: HttpClient) (url: string) (body: string) (token: string option) =
server.knot+13−28
@@ -44,8 +44,7 @@ offlineGossipMinTtl = 60 -- minimum TTL for offline gossip events
44maxForwardRetriesPerServer = 100
44maxForwardRetriesPerServer = 100
45maxGossipHops = 5
45maxGossipHops = 5
46gossipOriginHops = 2
46gossipOriginHops = 2
47−pollTimeout : Int<Ms>
47+
48−pollTimeout = 20000 -- 20 seconds
49backgroundPruneInterval : Int<Ms>
48backgroundPruneInterval : Int<Ms>
50backgroundPruneInterval = 30000 -- 30 seconds
49backgroundPruneInterval = 30000 -- 30 seconds
51federationEnabled = federationSecret != ""
50federationEnabled = federationSecret != ""
@@ -824,34 +823,22 @@ pollOnce = \token cursor -> do
824 yield (PollUnauthorized {})
823 yield (PollUnauthorized {})
825 Authorized {pubkey} -> do
824 Authorized {pubkey} -> do
826 seqCounter <- *seqCounter
825 seqCounter <- *seqCounter
827− messages <- *messages
828 let clampedCursor = clamp 0 (max seqCounter t) cursor
826 let clampedCursor = clamp 0 (max seqCounter t) cursor
829 let matchingMsgs = do
827 let matchingMsgs = do
830− m <- messages
828+ m <- *messages
831 where m.toKey == pubkey && m.receivedAt >= t - messageMaxAge && m.receivedAt > clampedCursor
829 where m.toKey == pubkey && m.receivedAt >= t - messageMaxAge && m.receivedAt > clampedCursor
832 yield m
830 yield m
833 let limitedMsgs = boundedSmallest matchingMsgs maxPollEvents (\m -> m.receivedAt)
831 let limitedMsgs = boundedSmallest matchingMsgs maxPollEvents (\m -> m.receivedAt)
832+ if count limitedMsgs == 0 then retry
833+ else do
834− let evts = do
834+ let evts = do
835− m <- limitedMsgs
835+ m <- limitedMsgs
836− yield {id: m.messageId, eventType: "message",
836+ yield {id: m.messageId, eventType: "message",
837− payload: {toKey: m.toKey, fromKey: m.fromKey,
837+ payload: {toKey: m.toKey, fromKey: m.fromKey,
838− encryptedBlob: m.encryptedBlob,
838+ encryptedBlob: m.encryptedBlob,
839− timestamp: m.timestamp}}
839+ timestamp: m.timestamp}}
840− let lastReceivedAt = fold (\acc m -> max acc m.receivedAt) clampedCursor limitedMsgs
840+ let lastReceivedAt = fold (\acc m -> max acc m.receivedAt) clampedCursor limitedMsgs
841− yield (PollOk {events: evts, cursor: lastReceivedAt})
841+ yield (PollOk {events: evts, cursor: lastReceivedAt})
842−
843−pollLoop : Text -> Int -> Int<Ms> -> IO {clock} PollResult
844−pollLoop = \token cursor deadline -> do
845− result <- pollOnce token cursor
846− case result of
847− PollUnauthorized {} -> yield result
848− PollOk {events} -> do
849− t <- now
850− if count events == 0 && t < deadline
851− then do
852− sleep (clamp 1 500 (deadline - t))
853− pollLoop token cursor deadline
854− else yield result
855
842
856-- ============================================
843-- ============================================
857-- Request Handler
844-- Request Handler
@@ -917,11 +904,9 @@ serve = \req -> case req of
917 yield (respond {token: token, expiresAt: expiry})
904 yield (respond {token: token, expiresAt: expiry})
918
905
919 Poll {cursor, authorization, respond} -> do
906 Poll {cursor, authorization, respond} -> do
920− t <- now
921− let deadline = t + pollTimeout
922 result <- case extractToken authorization of
907 result <- case extractToken authorization of
923 Nothing {} -> yield (PollUnauthorized {})
908 Nothing {} -> yield (PollUnauthorized {})
924− Just {value: token} -> pollLoop token cursor deadline
909+ Just {value: token} -> pollOnce token cursor
925 tAfterPoll <- now
910 tAfterPoll <- now
926 maybePruneAndBroadcast tAfterPoll
911 maybePruneAndBroadcast tAfterPoll
927 yield (respond (case result of
912 yield (respond (case result of