﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Garren commented on Challenge: What is wrong with this code</title><description>If ProcessMessages() takes longer than five seconds then you have a problem. I encountered something similar to this in a a little (.Net0 service app I was asked to look at a while back. It was supposed to hit the database every once in a while in order to figure out whether or not any new safety incidents had occurred and it would email a report off to a department head if anything qualified. The problem was that the original developer interpreted "every once in a while" to mean five seconds. The combination of a poorly designed database and inefficient processing within the application virtually guaranteed that the five second timer would fire before the previous processing was completed. Sucked.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment27</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment27</guid><pubDate>Thu, 03 Jul 2008 23:33:37 GMT</pubDate></item><item><title>Ajay commented on Challenge: What is wrong with this code</title><description>Infinite loop !!
  
  
even if one message causes an exception (which is ignored) the queue keeps getting bigger and bigger since the bad entry will never get marked as handled.
  
  
Eg. say there are 10 entries and 
  
the data in the 1st one causes an exception (hey there can be bugs - its not a perfect world)
  
Every time we run the select * from .. , this entry is back in queue.
  
causing every single entry after it to get stuck as this one fellow cannot be processed.
  
the queue now starts building and the entry never gets marked as done.
  
This can cause other things like outofmemory..etc if the queue starts building.
  
  
So get up at 2 in the morning 
  
run : update Messages set Handled = &lt;sideline&gt; where entry_id = &lt; id of culprit&gt;
  
the queue is back functioning.
  
  
Next day:
  
Fix : in the catch block: update the entries as sidelined (with retries if needed )
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment26</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment26</guid><pubDate>Thu, 03 Jul 2008 07:54:51 GMT</pubDate></item><item><title>leppie commented on Challenge: What is wrong with this code</title><description>There is never a check for zero messages returned from GetMessages(). There will be no need to send an empty array every 5 seconds...
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment25</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment25</guid><pubDate>Thu, 03 Jul 2008 07:34:46 GMT</pubDate></item><item><title>firefly commented on Challenge: What is wrong with this code</title><description>So does a req.GetResponse() threw a WebException when the message is too long? if it does then it's not so hard to debug but yes the bug is very subtle.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment24</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment24</guid><pubDate>Thu, 03 Jul 2008 07:22:34 GMT</pubDate></item><item><title>Zach commented on Challenge: What is wrong with this code</title><description>Can you do List&lt;Message&gt; ? I think that is the problem with the code.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment23</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment23</guid><pubDate>Thu, 03 Jul 2008 05:52:55 GMT</pubDate></item><item><title>dp commented on Challenge: What is wrong with this code</title><description>The byte[] could grow larger than what the "PUT" request is able to handle. Something akin to Apache's LimitRequestSize restriction.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment22</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment22</guid><pubDate>Thu, 03 Jul 2008 05:32:28 GMT</pubDate></item><item><title>hmemcpy commented on Challenge: What is wrong with this code</title><description>var msgs = GetMessages();
  
  
Ahh, good 'ol ReSharper :)
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment21</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment21</guid><pubDate>Wed, 02 Jul 2008 20:07:31 GMT</pubDate></item><item><title>Anon commented on Challenge: What is wrong with this code</title><description>(Oops, submitted to early)
  
  
Basically our backlog after 34 seconds is 21840 messages when in reality it should be closer to (1200 - 1000) * 34 = 6800. The backlog increases very quickly.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment20</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment20</guid><pubDate>Wed, 02 Jul 2008 18:17:14 GMT</pubDate></item><item><title>Anon commented on Challenge: What is wrong with this code</title><description>@Peter:
  
  
Consider a case where you can send 1000 messages per second.
  
  
Time 0.0s: We call Sleep(5000)
  
  
Time 0.1s-5.0s: We start receiving messages at the rate of 1200 messages per second.
  
  
Time 5.0s: We wake up and pop the 6000 messages and start sending them.
  
  
Time 11.0s: We're done sending them, and will now go to sleep, even though there's already 7200 more messages to send.
  
  
Time 16.0s: We wake back up, and now have to process 13200 messages. This takes 13.2 seconds.
  
  
Time 29.2s: We're done sending. There are now 15840 new messages to process, but we go to sleep again for 5 more seconds.
  
  
Time 34.2s: We wake back up, and now have 21840 messages to process.
  
  
.. etc.
  
  
We call ProcessMessages once
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment19</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment19</guid><pubDate>Wed, 02 Jul 2008 18:13:51 GMT</pubDate></item><item><title>Peter Ritchie commented on Challenge: What is wrong with this code</title><description>Sorry, make that "HTTP put", not "HTTP get".
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment18</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment18</guid><pubDate>Wed, 02 Jul 2008 16:07:30 GMT</pubDate></item><item><title>Peter Ritchie commented on Challenge: What is wrong with this code</title><description>I believe there is a default timeout and a default size limit to HTTP get with .NET.  If a timeout occurs because there is too much data you'll end up simply reprocessing all the messages in vain and be doomed to more timeouts as the size increases from iteration to iteration.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment17</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment17</guid><pubDate>Wed, 02 Jul 2008 16:06:58 GMT</pubDate></item><item><title>Peter Ritchie commented on Challenge: What is wrong with this code</title><description>@Anon why?  ProcessMessages can take as long as it wants.  There's just a 5 second delay between when ProcessMessages finishes and when it is called next.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment16</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment16</guid><pubDate>Wed, 02 Jul 2008 16:04:18 GMT</pubDate></item><item><title>Max Schmeling commented on Challenge: What is wrong with this code</title><description>@Yitzchok - I actually thought the same as Aguiar at first, but you're right: ProcessMessages is synchronous so Thread.Sleep is called after it finishes.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment15</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment15</guid><pubDate>Wed, 02 Jul 2008 15:38:38 GMT</pubDate></item><item><title>Dave THe Ninja commented on Challenge: What is wrong with this code</title><description>While(true) is a bit concerning :-) 
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment14</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment14</guid><pubDate>Wed, 02 Jul 2008 15:02:45 GMT</pubDate></item><item><title>Yitzchok commented on Challenge: What is wrong with this code</title><description>@Aguiar
  
Doesn't it start sleeping only after srv.ProcessMessages(); is done.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment13</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment13</guid><pubDate>Wed, 02 Jul 2008 15:00:58 GMT</pubDate></item><item><title>Markus Zywitza commented on Challenge: What is wrong with this code</title><description>Nice unbounded ResultSet, the story about it in Release It!'s antipattern chapter shows what happens with them...
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment12</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment12</guid><pubDate>Wed, 02 Jul 2008 13:45:41 GMT</pubDate></item><item><title>Andres Aguiar commented on Challenge: What is wrong with this code</title><description>Thread.Sleep(5000)
  
  
If it takes more than 5 seconds to send the message you'll start sending them twice
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment11</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment11</guid><pubDate>Wed, 02 Jul 2008 13:05:18 GMT</pubDate></item><item><title>Charles Wain commented on Challenge: What is wrong with this code</title><description>Large object Heap ...?
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment10</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment10</guid><pubDate>Wed, 02 Jul 2008 12:58:02 GMT</pubDate></item><item><title>Anon commented on Challenge: What is wrong with this code</title><description>You need to ensure your messages buffer is still empty before sleeping:
  
  
while (srv.ProcessMessages()) ;
  
  
If the rate of incoming messages is "too high" (whereby it would take 5 seconds or greater to send a batch them via HTTP PUT) then the buffer gets "overfilled" during the sleep and you wind up waiting for no reason. Then, by the time you've woken up, you now have 10 "seconds of data" in the buffer, and you wind up getting backlogged very quickly.
  
  
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment9</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment9</guid><pubDate>Wed, 02 Jul 2008 12:16:28 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: What is wrong with this code</title><description>Yes, you deal with them in a piece by piece.
  
And you want to do this as early as possible, so TOP / LIMIT is the way to go
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment8</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment8</guid><pubDate>Wed, 02 Jul 2008 09:57:46 GMT</pubDate></item><item><title>Tobin Harris commented on Challenge: What is wrong with this code</title><description>So does that mean you need to use "SELECT TOP 100 *" to limit the number of messages sent in any one pass?
  
  
Or would you just split the payload up (the array) and send it using multiple requests?
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment7</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment7</guid><pubDate>Wed, 02 Jul 2008 09:50:14 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: What is wrong with this code</title><description>1/ is unlikely to happen. And anyway, 4GB + of msgs isn't quite the thing to have.
  
2/ And that is the main issue, yep. What is worth, once you got to that point, you are doomed, because the amount of work will just get bigger and bigger, and you''ll always fail
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment6</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment6</guid><pubDate>Wed, 02 Jul 2008 05:29:21 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: What is wrong with this code</title><description>Lamar,
  
Continue this to the obvious conclusion, you are almost there, although you went _way_ ahead.
  
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment5</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment5</guid><pubDate>Wed, 02 Jul 2008 05:27:57 GMT</pubDate></item><item><title>Yitzchok commented on Challenge: What is wrong with this code</title><description>1. byte[] data = Serialize(msgs) - is greater then Array can handle (Or maybe use data.LongLength and write a few times to the stream)
  
2. Message too long so you get a server Time Out (and this might grow the next time you try)....
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment4</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment4</guid><pubDate>Wed, 02 Jul 2008 05:17:42 GMT</pubDate></item><item><title>Lamar commented on Challenge: What is wrong with this code</title><description>If there are too many messages in the system, the request stream will exceed the max bytes allowed by the http server you are posting to, and the post request will fail.  
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment3</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment3</guid><pubDate>Wed, 02 Jul 2008 05:07:55 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: What is wrong with this code</title><description>1/ Assume that MarkMessagesAsHandled can't throw, if the other server sent an OK reply, we will get the data and mark them. If it didn't, this is failure case and we should retry.
  
2/ doesn't really matter. In general, you are correct, but if an exception is thrown from GetResponse() the response object will be disposed by the system.
  
3/ no messages means that the other side gets an empty message, and just ignore it. No issue. Assume that GetMessages can't throw.
  
4/ you are close
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment2</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment2</guid><pubDate>Wed, 02 Jul 2008 04:38:10 GMT</pubDate></item><item><title>Yitzchok commented on Challenge: What is wrong with this code</title><description>Here are some guesses.
  
  
1. If any exception is thrown after you send the data to the server but before you call MarkMessagesAsHandled(..) then the same Messages will be be sent again.
  
  
2. You don't close "resp" in a finally block (if an exception is thrown - using up system resources).
  
  
(3. If no messages are returned from GetMessages() or it throws an exception.)
  
  
4. If there is to much messages to handle.
</description><link>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment1</link><guid>http://ayende.com/3396/challenge-what-is-wrong-with-this-code#comment1</guid><pubDate>Wed, 02 Jul 2008 04:25:52 GMT</pubDate></item></channel></rss>