Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NotificationSubsystem to push notifications #3786

Merged
merged 43 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3992d9a
libs/wire-subsystems: Introduce
akshaymankar Dec 18, 2023
7240673
WIP
akshaymankar Dec 18, 2023
31dbdb0
[wip] fill some more holes
MangoIV Dec 19, 2023
bb2263f
[feat] first tests for Notification subsystem
MangoIV Dec 19, 2023
52027f2
[fix] fix interpretation into async
MangoIV Dec 19, 2023
f835c1b
[fix] proper property test
MangoIV Dec 19, 2023
5e02be7
[feat] implement pushSlowly
MangoIV Dec 19, 2023
039f53f
[chore] more testing
MangoIV Dec 20, 2023
7d6dc85
[chore] Add more tests for NotificationSubsystem
MangoIV Dec 20, 2023
f68d9a6
[chore] nix package for notificatio-subsystem
MangoIV Dec 20, 2023
a41e73e
wire-subsystem: Move things around and use hspec-discover
akshaymankar Dec 21, 2023
dc6564d
wire-subsystem: Extract the interpreter out of NotificationSubsystem
akshaymankar Dec 21, 2023
bde026b
galley: Delete code ported to wire-subsystems
akshaymankar Dec 21, 2023
aaa2713
galley: Use the new NotificationSubsystem
akshaymankar Dec 21, 2023
78c819b
Regen nix files
akshaymankar Dec 21, 2023
6d8c39a
wire-subsystems/test: Remove unnecessary use of Set
akshaymankar Dec 21, 2023
8b0c44c
Delete commented out code
akshaymankar Dec 21, 2023
b283fcc
wire-subsystems: Make PushSlowly a NotificationSubsystem action
akshaymankar Dec 21, 2023
79b13a1
wire-subsystems: Update nix
akshaymankar Jan 8, 2024
550bba5
Remove unnecessary polymorphism
akshaymankar Jan 8, 2024
f6b3b6e
NotificationSubsystem: Rename things for clarity
akshaymankar Jan 8, 2024
d541e3c
NotificationSubsystem: Allow setting ApsData on pushes
akshaymankar Jan 9, 2024
56dfa4a
Move notification subsystem defaults in the subsystem module
akshaymankar Jan 10, 2024
270c547
galley: Fix wrong merge
akshaymankar Jan 29, 2024
883041f
brig: Use NotificationSubsystem
akshaymankar Jan 9, 2024
80800b6
Remove stale TODO
akshaymankar Jan 10, 2024
25e37ad
brig: Move more calls to gundeck into NotificationSubsystem
akshaymankar Jan 11, 2024
8703a31
brig: Remove unused gundeck stuff
akshaymankar Jan 11, 2024
f7b66d4
brig: Use Wire.Rpc instead of brig's RPC effect
akshaymankar Jan 11, 2024
9ca2265
Changelog
akshaymankar Jan 11, 2024
6f7752f
Comments and better names
akshaymankar Jan 29, 2024
139cbd5
NotificationSubsystem: Remove internal module, export everything
akshaymankar Feb 6, 2024
a93e7f5
Remove unnecessary qualification
akshaymankar Feb 6, 2024
7c73875
NotificationSubsystem.Interpreter: make slowPushDelay a Natural
akshaymankar Feb 6, 2024
e4ea640
Explain threadDelay
akshaymankar Feb 6, 2024
5a14339
NotificationSubsystem: Abstract pushing notifications asynchronously
akshaymankar Feb 6, 2024
3cc3529
NotificationSubsystem: Log error when async push fails
battermann Feb 6, 2024
0518aaf
Add back wrongly deleted comment
akshaymankar Feb 7, 2024
e5776fe
linters
akshaymankar Feb 7, 2024
d34ed88
GundeckAPIAccess: Fix method
akshaymankar Feb 7, 2024
648e3bb
hlint
akshaymankar Feb 8, 2024
4c0cfad
Use DiffTime to denote slowPushDelay
akshaymankar Feb 8, 2024
65b98ed
hlint
akshaymankar Feb 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ packages:
, libs/wire-api/
, libs/wire-api-federation/
, libs/wire-message-proto-lens/
, libs/wire-subsystems/
, libs/zauth/
, services/background-worker/
, services/brig/
Expand Down Expand Up @@ -162,6 +163,8 @@ package wire-api-federation
ghc-options: -Werror
package wire-message-proto-lens
ghc-options: -Werror
package wire-subsystems
ghc-options: -Werror
package zauth
ghc-options: -Werror
package fedcalls
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5-internal/notification-subsystem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Start refactoring code into subsystems, first subsystem being the NotificationSubsystem.
2 changes: 2 additions & 0 deletions libs/extended/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
, servant-server
, temporary
, text
, time
, tinylog
, unliftio
, wai
Expand Down Expand Up @@ -63,6 +64,7 @@ mkDerivation {
servant-openapi3
servant-server
text
time
tinylog
unliftio
wai
Expand Down
2 changes: 2 additions & 0 deletions libs/extended/extended.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build-type: Simple
library
-- cabal-fmt: expand src
exposed-modules:
Data.Time.Clock.DiffTime
Network.AMQP.Extended
Network.RabbitMqAdmin
Options.Applicative.Extended
Expand Down Expand Up @@ -101,6 +102,7 @@ library
, servant-openapi3
, servant-server
, text
, time
, tinylog
, unliftio
, wai
Expand Down
43 changes: 43 additions & 0 deletions libs/extended/src/Data/Time/Clock/DiffTime.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Data.Time.Clock.DiffTime
( DiffTime,
weeksToDiffTime,
daysToDiffTime,
hoursToDiffTime,
minutesToDiffTime,
secondsToDiffTime,
millisecondsToDiffTime,
microsecondsToDiffTime,
nanosecondsToDiffTime,
picosecondsToDiffTime,
diffTimeToFullMicroseconds,
diffTimeToPicoseconds,
)
where

import Data.Time
import Imports

weeksToDiffTime,
daysToDiffTime,
hoursToDiffTime,
minutesToDiffTime,
millisecondsToDiffTime,
microsecondsToDiffTime,
nanosecondsToDiffTime ::
Integer -> DiffTime
weeksToDiffTime = daysToDiffTime . (7 *)
daysToDiffTime = hoursToDiffTime . (24 *)
hoursToDiffTime = minutesToDiffTime . (60 *)
minutesToDiffTime = secondsToDiffTime . (60 *)
millisecondsToDiffTime = picosecondsToDiffTime . (e9 *)
microsecondsToDiffTime = picosecondsToDiffTime . (e6 *)
nanosecondsToDiffTime = picosecondsToDiffTime . (e3 *)

-- | Rounds down. Useful for 'threadDelay', 'timeout', etc.
diffTimeToFullMicroseconds :: DiffTime -> Int
diffTimeToFullMicroseconds = fromInteger . (`div` e6) . diffTimeToPicoseconds

e3, e6, e9 :: Integer
e3 = 1_000
e6 = 1_000_000
e9 = 1_000_000_000
10 changes: 6 additions & 4 deletions libs/gundeck-types/src/Gundeck/Types/Push/V2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ instance ToJSON RecipientClients where
-- ApsData

newtype ApsSound = ApsSound {fromSound :: Text}
deriving (Eq, Show, ToJSON, FromJSON)
deriving (Eq, Show, ToJSON, FromJSON, Arbitrary)

newtype ApsLocKey = ApsLocKey {fromLocKey :: Text}
deriving (Eq, Show, ToJSON, FromJSON)
deriving (Eq, Show, ToJSON, FromJSON, Arbitrary)

data ApsPreference
= ApsStdPreference
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving (Arbitrary) via GenericUniform ApsPreference

instance ToJSON ApsPreference where
toJSON ApsStdPreference = "std"
Expand All @@ -195,7 +196,8 @@ data ApsData = ApsData
_apsPreference :: !(Maybe ApsPreference),
_apsBadge :: !Bool
}
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving (Arbitrary) via GenericUniform ApsData

makeLenses ''ApsData

Expand Down
1 change: 1 addition & 0 deletions libs/polysemy-wire-zoo/polysemy-wire-zoo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ library
Wire.Sem.Concurrency
Wire.Sem.Concurrency.IO
Wire.Sem.Concurrency.Sequential
Wire.Sem.Delay
Wire.Sem.FromUTC
Wire.Sem.Jwk
Wire.Sem.Logger
Expand Down
58 changes: 58 additions & 0 deletions libs/polysemy-wire-zoo/src/Wire/Sem/Concurrency.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ unsafePooledMapConcurrentlyN_ n f as =
(UnsafePooledMapConcurrentlyN_ n f as :: Concurrency 'Unsafe (Sem r) ())
{-# INLINEABLE unsafePooledMapConcurrentlyN_ #-}

unsafePooledForConcurrentlyN ::
forall r t a b.
(Member (Concurrency 'Unsafe) r, Foldable t) =>
-- | Max. number of threads. Should not be less than 1.
Int ->
t a ->
(a -> Sem r b) ->
Sem r [b]
unsafePooledForConcurrentlyN n as f =
send
(UnsafePooledMapConcurrentlyN n f as :: Concurrency 'Unsafe (Sem r) [b])
{-# INLINEABLE unsafePooledForConcurrentlyN #-}

unsafePooledForConcurrentlyN_ ::
forall r t a b.
(Member (Concurrency 'Unsafe) r, Foldable t) =>
-- | Max. number of threads. Should not be less than 1.
Int ->
t a ->
(a -> Sem r b) ->
Sem r ()
unsafePooledForConcurrentlyN_ n as f =
send
(UnsafePooledMapConcurrentlyN_ n f as :: Concurrency 'Unsafe (Sem r) ())
{-# INLINEABLE unsafePooledForConcurrentlyN_ #-}

pooledMapConcurrentlyN ::
forall r' r t a b.
r' ~ '[Final IO] =>
Expand Down Expand Up @@ -111,3 +137,35 @@ pooledMapConcurrentlyN_ n f as =
Concurrency 'Safe (Sem r) ()
)
{-# INLINEABLE pooledMapConcurrentlyN_ #-}

pooledForConcurrentlyN ::
forall r' r t a b.
r' ~ '[Final IO] =>
(Member (Concurrency 'Safe) r, Subsume r' r, Foldable t) =>
-- | Max. number of threads. Should not be less than 1.
Int ->
t a ->
(a -> Sem r' b) ->
Sem r [b]
pooledForConcurrentlyN n as f =
send
( UnsafePooledMapConcurrentlyN n (subsume_ @r' @r . f) as ::
Concurrency 'Safe (Sem r) [b]
)
{-# INLINEABLE pooledForConcurrentlyN #-}

pooledForConcurrentlyN_ ::
forall r' r t a b.
r' ~ '[Final IO] =>
(Member (Concurrency 'Safe) r, Subsume r' r, Foldable t) =>
-- | Max. number of threads. Should not be less than 1.
Int ->
t a ->
(a -> Sem r' b) ->
Sem r ()
pooledForConcurrentlyN_ n as f =
send
( UnsafePooledMapConcurrentlyN_ n (subsume_ @r' @r . f) as ::
Concurrency 'Safe (Sem r) ()
)
{-# INLINEABLE pooledForConcurrentlyN_ #-}
32 changes: 32 additions & 0 deletions libs/polysemy-wire-zoo/src/Wire/Sem/Delay.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# LANGUAGE TemplateHaskell #-}

module Wire.Sem.Delay where

import Imports
import Polysemy

data Delay m a where
Delay :: Int -> Delay m ()

makeSem ''Delay

runDelay :: Member (Embed IO) r => Sem (Delay ': r) a -> Sem r a
runDelay = interpret $ \case
Delay i -> threadDelay i

runControlledDelay :: forall r a. (Member (Embed IO) r) => MVar Int -> Sem (Delay : r) a -> Sem r a
runControlledDelay tickSource = interpret $ \case
Delay n -> waitForTicks n
where
waitForTicks :: Int -> Sem r ()
waitForTicks 0 = pure ()
waitForTicks remaining0 = do
passedTicks <- takeMVar tickSource
let remaining = remaining0 - passedTicks
if remaining <= 0
then pure ()
else waitForTicks remaining

runDelayInstantly :: Sem (Delay : r) a -> Sem r a
runDelayInstantly = interpret $ \case
Delay _ -> pure ()
15 changes: 15 additions & 0 deletions libs/polysemy-wire-zoo/src/Wire/Sem/Logger/TinyLog.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE RecordWildCards #-}

-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
Expand All @@ -21,12 +23,16 @@ module Wire.Sem.Logger.TinyLog
stringLoggerToTinyLog,
discardTinyLogs,
module Wire.Sem.Logger.Level,
LogRecorder (..),
newLogRecorder,
recordLogs,
)
where

import Data.Id
import Imports
import Polysemy
import Polysemy.TinyLog (TinyLog)
import qualified System.Logger as Log
import Wire.Sem.Logger
import Wire.Sem.Logger.Level
Expand Down Expand Up @@ -58,3 +64,12 @@ stringLoggerToTinyLog = mapLogger @String Log.msg

discardTinyLogs :: Sem (Logger (Log.Msg -> Log.Msg) ': r) a -> Sem r a
discardTinyLogs = discardLogs

newtype LogRecorder = LogRecorder {recordedLogs :: IORef [(Level, LByteString)]}

newLogRecorder :: IO LogRecorder
newLogRecorder = LogRecorder <$> newIORef []

recordLogs :: Member (Embed IO) r => LogRecorder -> Sem (TinyLog ': r) a -> Sem r a
recordLogs LogRecorder {..} = interpret $ \(Log lvl msg) ->
modifyIORef' recordedLogs (++ [(lvl, Log.render (Log.renderDefault ", ") msg)])
3 changes: 3 additions & 0 deletions libs/types-common/src/Data/Range.hs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ genRange pack_ gc =
instance (KnownNat n, KnownNat m, n <= m) => Arbitrary (Range n m Integer) where
arbitrary = genIntegral

instance (KnownNat n, KnownNat m, n <= m) => Arbitrary (Range n m Int32) where
arbitrary = genIntegral

instance (KnownNat n, KnownNat m, n <= m) => Arbitrary (Range n m Word) where
arbitrary = genIntegral

Expand Down
Loading
Loading