Monday, February 16, 2009

Notes on YetAnotherForum.net speed

I've been using YetAnotherForum on runSaturday and have found it very good.

However... I have also had some problems - which I think I've started to solve...

Here are my notes - as submitted to http://forum.yetanotherforum.net/yaf_postsm31803_Problems-with-slow-forum-not-on-godaddy--and-my-solution.aspx#post31803

I've been using this on my DotNetNuke site - http://www.runsaturday.com - and I've had some problems with slowness - similar I think to some of the GoDaddy problems much discussed here.

My speed problems (I think) were:
 - that my mail server is occasionally slow to react - so the sending of emails is often slow... and this seems to be done on the forum main thread.
 - that the user images returned from the forum are not sent with caching information - so they are requested far too often by clients.

To work around these problems:

1. I've added this code to the mail sender in forumpage.cs - I've not fully tested it works yet - but the speed improvement seems OK:

        const double NumSecondsBetweenMailSendAttempts = 600;
        static DateTime MailLastSent = DateTime.Now;
        class MailSendLock
        {
            private int i = 0;
        }
        static MailSendLock MailSendLockObject = new MailSendLock();

        private void TriggerMailIfNecessary()
        {
            if ((DateTime.Now - MailLastSent).TotalSeconds > NumSecondsBetweenMailSendAttempts)
            {
                lock (MailSendLockObject)
                {
                    if ((DateTime.Now - MailLastSent).TotalSeconds > NumSecondsBetweenMailSendAttempts)
                    {
                        MailLastSent = DateTime.Now;
                        System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(SendMailThread));
                    }
                }
            }
        }

2. I've added this code to the resource.ashx.cs file:

        private static void AddCaching(HttpContext context)
        {
            context.Response.Cache.SetExpires(DateTime.Now.AddDays(7.0));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetValidUntilExpires(false);
        }

        public void ProcessRequest( HttpContext context )
        {
            if ( context.Request.QueryString ["r"] != null )
            {
                // resource request
                GetResource( context );

            }
            else if ( context.Session ["lastvisit"] != null )
            {
                if ( context.Request.QueryString ["u"] != null )
                {
                    GetResponseLocalAvatar( context );
                    AddCaching(context);
                }
                else if ( context.Request.QueryString ["url"] != null && context.Request.QueryString ["width"] != null && context.Request.QueryString ["height"] != null )
                {
                    GetResponseRemoteAvatar( context );
                    AddCaching(context);
                }
                else if ( context.Request.QueryString ["a"] != null )
                {
                    GetResponseAttachment( context );
                    AddCaching(context);
                }
            }
            else
            {
                // they don't have a session...
                context.Response.Write( "Please do not link directly to this resource. You must have a session in the forum." );
            }
        }

No comments:

Post a Comment