1
\$\begingroup\$

Is there a way to make a threading/async post with a web reference not slow down the whole website just because the web reference takes a long time to return a response?

I have a send email function that gets posted async with a threading system that basically looks like this. This whole .cs file is called Mail.cs (that's where the Mail. name comes from).

public static bool SendMail(var alotOfVariablesHere)
{
    MailParameters param = new MailParameters(alotOfVariablesHere);
    Thread thread = new Thread(Mail.QueueSendMailAsyc);
    thread.Start(param);
    return true;

}

static Object locker = new object();
public static void QueueSendMailAsyc(object Objparam)
{
    lock (locker)
    {
        SendMailAsyc(Objparam);
    }
}

Inside SendMailAsyc() we run our web reference:

WebReference.Service1 Converter = new WebReference.Service1();
Converter.Timeout = 3 * 60 * 1000;

byte[] file = Converter.ConvertFunction(File.ReadAllBytes(attachfilepath), fileName);

The only thing the WebReference does is receiving a byte array and a file name and then returns a new byte array.

Pretty simple and it all works fine.

But recently the server were the web reference is sitting on (a totally different server from the webpage) has started getting really slow. This means that the web reference takes a long time to respond (it does respond and it does return everything like the way we want it. just that it's now a lot slower)

And this has started causing a lot of issues, like: The webpage also get really slowed down, it takes ages to navigate through the pages and so on.

Plus the fact that this one single web reference threading/async post takes so long time means that there is plenty of time to put up even more threading/async posts meaning it'll put an even bigger pressure on the server, causing it to slow down a lot.

This issue is primary that the web reference has gotten really slow lately, and that our webpage server cannot handle the pressure of too many threads/async posts.

I can't do much about the server the web reference is sitting on has started getting really slow recently.

But is there anything I can do code-wise to make my system handle this really slow web reference better?

\$\endgroup\$
  • \$\begingroup\$ "After this we run SendMailAsyc function. This function itself is kinda too big to be posted here." Red flag for me. This screams "my function is very big and ugly and I know it must be split up but I can't let you because it's not relevant". What about still posting that bit of code? \$\endgroup\$ – Pimgd Jul 24 '14 at 9:17
  • \$\begingroup\$ @Pimgd It's just a lot of strings here and there plus variable checks. And it all gets completed within 0.1 seconds. The issue here is the web reference and how to deal with it \$\endgroup\$ – HenrikP Jul 24 '14 at 9:24
  • \$\begingroup\$ 0.1 seconds is a very, very long time for a computer. \$\endgroup\$ – rolfl Jul 24 '14 at 11:56
  • \$\begingroup\$ @rolfl notice the "within" part. At worst it might go up to 0.1 seconds to run. The web reference however can take up to half a minute just to respond. \$\endgroup\$ – HenrikP Jul 24 '14 at 12:01
  • \$\begingroup\$ @HenrikP honestly I didn't read much of the question. Is the code running on the server or the client? Please explain why you made this choice. \$\endgroup\$ – user12202 Jul 24 '14 at 13:08
1
\$\begingroup\$

There's not enough information here to tell you what's wrong, but I'll tell you what I would do differently regardless.

Manually spawning threads is something you should avoid doing, especially if there's a chance that thread is just going to wait on a lock. As the remote service gets slower and your app gets busier, you'll have more and more idle threads taking up resources. You can make a single mail thread that has new params passed to it (in a thread-safe way).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.