Unity 4.6 UI and nested ScrollRect

I wanted to have nested ScrollRects with different scrolling axis using the new Unity UI 4.6. By default this cannot be achieved currently.

My goal was to be able to swipe through 4 pages of the main menu of my game (green axis) and then on one of the pages be able to swipe through a list of items (red axis):

scrolling

So I created a subclass of ScrollRect that routes the drag events to all parent ScrollRects in case a simple rule checked in OnBeginDrag is true. For my simple case I just wanted to route events to the parent in case a horizontal drag was being initiated but the scroll rect is a vertical one, or a vertical drag was being initiated and the scroll rect is a horizontal one. This is my subclass:

Cheers.

IIS vs. Mono/Cherokee .NET web app response times

Some time ago I was testing how a Windows IIS powered backend would perform compared to the current Mono with Cherokee on Linux setup. The graphic shows the measured average response times. The switch to the Windows Backend was done shortly after 10PM. As you can see the average response times were significantly smaller afterwards.
Before there were 2 C3.XLarge Linux instances, afterwards there were 2 C3.Large Windows instances. So the IIS performed better in my setup on a smaller instance.

Switch Linux to Windows

Since this setup is also cheaper than the set of Linux instances, I am thinking about a permanent switch. I am also going to do comparisons using a different Mono setup. I will test Nginx as well as Apache.

Mono Key Duplication Error in High Traffic Environment Workaround

After setting up the Auto Scaling feature in the Amazon cloud to automatically fire up and terminate virtual machines, depending on how much traffic there is, I ran into some issues with Mono.

In my environment I run Mono 3.4 using the Cherokee web server on Ubuntu 14.

Apparently there is a bug (that actually should be already fixed according to http://lists.ximian.com/pipermail/mono-bugs/2013-May/113692.html) which leads to erroneous Mono instances when there is high incoming traffic during startup of that instance. The bad thing is that this instance of Mono will then not recover during subsequent calls, only killing the process helps. I saw two kinds of this error, one with key duplication for key “roleManager” and one for key “sessionState”.

The stack trace of the error looks like this:

In my web.config neither role manager config entries, nor session state entires were present. But each web.config used in your own projects derives from a global web.config (just like on IIS and Windows). The location for that global web.config on Ubuntu for .NET 4 web apps is /usr/etc/mono/4.5/web.config.
Commenting role manager and session manager in the httpModules section will actually fix the problem.

So the pitfall of this workaround is that you can only use it if your web app does not use sessions and is also not using the role manager / membership functionalities.

Thanks for reading, hope this helps somebody to get Mono very stable in a high traffic production environment.

Optimizing Mono FastCGI running on Ubuntu / Cherokee Webserver

For an online game I am running Mono in the Amazon AWS cloud on several c3.large instances. Those instance types have 2 virtual CPUs. Today I experienced a little downtime of the game and found that none of the servers was working at their full capacity. Mostly only 25% CPU was used. The database servers could not be the bottleneck as the following charts showed.

Loadbalancer Request Latency
This diagram shows the average latency our load balancer in AWS encountered while serving the requests. As you can see there is a spike up to 12 seconds at around 13:45.

WebServer CPU usage
This diagram shows the CPU usage on one of our web servers (the others were similar). At around 13:45 a spike of around 40% CPU usage can be found, not nearly at the servers capacity.

Database Server CPU usage
This chart shows the CPU usage of the master database server. Also not nearly at it’s full capacity. Since we already invested quite a lot of time into optimizing the database server, I know that it is possible to max it out and found that this could not be the bottleneck.

In our current configuration the Cherokee WebServer is configured to access one Mono FastCGI source. This apparently rather inefficient. So in order to get better results I added 3 more FastCGI sources basically allowing Cherokee to load balance now between 4 FastCGI sources. I cannot yet confirm that the servers capacity is not used better, but I will update this post as soon as I can.

The cherokee configuration looks now like this:

Each FastCGI process is listening on its own port. Now in order for Cherokee to load balance between the sources, the following is needed in the configuration:

This tells Cherokee that the default handler of my default matching rule of our virtual server should use the information sources 1 to 4 and load balance between them.

Mono Running at 100% CPU Usage After Some Time (Under Load)

I am maintaining the backend of an online soccer app that is hosted on several Ubuntu servers in the Amazon cloud, running on Mono and ASP.NET with FastCGI on the Cherokee web server. I spent quite a while figuring out why after some time, actually only under load, the smaller servers would be stuck at 100% cpu usage consumed by the Mono process. On a medium machine this happened only once yet. In the AWS console it looked like this:

Before altering settings

 

A server is running fine for a while, then suddenly jumping to 100% cpu usage and not recovering from there even though over night the load drastically decreases. It would sit there forever, still serving requests though. After restarting Mono everything would be back to normal for some time until it’s back at 100%. The peak load is around 20 requests per second.

I tried to check a complete stack dump for the Mono process but without any finds.

My latest effort was to play with some environment variable settings for Mono. Setting MONO_THREADS_PER_CPU to a high number of 2000 actually fixed the problem with the cpu going to 100%.

Since I am currently using the Cherokee web server this is an excerpt of my cherokee.conf that configures the FastCGI source:

I actually had to include other environment variables that are normally inherited by mono from the web server as well (PATH, …), otherwise the application would not work.

I will continue to watch if the problem really vanished. Hope this helps somebody since it is hard to find a really good best practice guide or anything to setup and run production servers with Mono on Linux.

 

RESTful Web Services and MVC3 – simplified route mapping

There are many possibilities to create restful web Services using MVC. In one of my latest Projects I used MVC’ IConstraint interface to enable two kinds of calls for a single controller:

1. with an ID, e.g. GET http://project.programmierstube.com/api/users/123/info
2. without an ID, e.g. POST http://project.programmierstube.com/api/users/register

When setting up the route mapping (e.g. in an area registration or global.asax.cs) you can specify a constraint for a placeholder in the routing url:

Notice the assignment of a RestServiceConstraint instance to the id parameter. Now when we have an incoming a request and this routing is selected due to the match of /api/users/ the constraint is being asked if the “id” parameter makes a valid match as well. Also using the constraint we can modify the assigned routing parameters.
When we don’t have an id, we want action to take the value previously assigned to id and if indeed an id is present we don’t want to change the values at all.

This is how the constraint implementation looks like:

On thing to note though is that I do make an assumption as how the id parameter looks like. In this case I assume it is an integer, and I know that I won’t have controller methods named as integers ;-) If for example you have GUIDs or MongoDB Object IDs you could check for the length of the ID parameter since it’s also unlikely to have 40 or 24 character long controller method names.

I found this approach to be simple, easy to set up and perfectly fit the needs for my projects.

WordPress: Separators Between Items When Using wp_nav_menu

Wordpress makes a lot of things relatively easy for developers. One thing I was looking for today is having a simple separator like “|” between menu items when using the wp_nav_menu function. I found that a very nice and easy solution is creating a simple class deriving from wordpress’ Walker_Nav_Menu class.

1. Creating the custom walker class.

Since I was needing this functionality for a custom theme, I put the following code in the theme’s functions.php.

The class is actually really simple. It stores a counter for checking whether the current item is the first item since we only want to have the separator between two items and not at the beginning. The separator is prepended to the output and then, to keep normal wordpress behavior, the parrent::start_el function is called.

2. Showing the menu with a separator

In your theme or wherever you want to display the menu, add the following code:

As you can see an additional custom option is passed to the wp_nav_menu function call: “ex_separator”. The value of this option may contain any html fragment you need for styling the separator.

Note that this code supports only depth=1 menus but could be easily extended to have different kind of separators on different levels (e.g. one for the main menu, one for sub menus).

Please feel free to post suggestions and comments.