Closed svenkeidel closed 9 years ago
Hi @svenkeidel ,
thank you very much for your helpful advices! I will try to put all my state in one MVar. I'm aware of the of Maybe and the redundant code things. Code cleanup is on my list and will be done soon.
Best regards, Wulf
@wpmp, you asked how to dispatch the different messages in the code below. An easy solution would be to just reserve different ports for the different message types. What do you think?
let maybeVersionMsg = (A.decodeStrict rawMsg) :: Maybe VersionMessage
let maybeDiscoverMsg = (A.decodeStrict rawMsg) :: Maybe DiscoverRequest
let maybeConfigMsg = (A.decodeStrict rawMsg) :: Maybe ConfigurationMessage
case maybeVersionMsg of
Just msg -> ...
Nothing -> ...
case maybeDiscoverMsg of
Just msg -> ...
Nothing -> ...
case maybeConfigMsg of
Just msg -> ...
Nothing -> ...
@svenkeidel, this would be a possibility, however I think that would make implementation of sources in clients more complex. I would probably leave it like this for now. Whats your opinion?
Can you give an example of what would be more difficult this way?
Nevermind, i mixed up some things. I thought about it again and I think this might also be helpful for performance as configuration and discover messages can't block version and product messages, right?
yes
Hi @wpmp,
I reviewed the code base and have the following recommendations.
Use of MVar's: Put all your state into one
MVar
instead of multiples, else you run into deadlocks. Also you should avoid usingreadMVar
andputMVar
because they always have to appear in pairs. Instead try to usemodifyMVar
:Use of Maybe:
fromJust
is a partial function and should be avoided. If you can provide a meaning default value when aMaybe
turns out to beNothing
usefromMaybe meaningfullDefault maybeValue
:Another alternative is using the
Monad
instance ofMaybe
. It allows you to short-circuit a whole computation if one maybe values is nothing:If you have several
Maybe
values of the same type and you just want to get one of the values that isJust
, you can use theMonoid
instance ofMaybe
:Eliminate redundant code:
broker/Main.hs
contains 6 times the following code:You should introduce a new function for this piece of code.
If you have further questions contact me and we can discuss the issues in person.
Best, Sven