Token-Based PubSub

I recently pushed two new features to both ZeroMQ and NetMQ: manual subscriptions and welcome message, both for the XPub socket. In this post, I will explore what we can do with the manual subscriptions feature. The next post will cover the welcome message.

Neither feature is part of any release of ZeroMQ or NetMQ yet, so you will have to compile from the source code to use the new features.

I will use NetMQ throughout the post, but you can implement the examples using any ZeroMQ binding.

Manual Subscriptions

When the Manual Subscriptions feature is enabled, subscription (or unsubscribe) requests are not added to (or removed from) the internal XPub subscriptions store. They will, however, be available to read as messages. So, after reading a subscription request from XPub socket, we can decide how to handle the socket. If we do nothing (and drop the subscription), the subscriber will not receive anything, including messages that match the subscription. If we want to confirm the subscription as is, we need to call the subscribe method on the XPub socket with the subscription (with ZeroMQ, we need to call the setsockopt with ZMQ_SUBSCRIBE) the same as we subscribe on the Sub socket. Following is an example that confirms each subscription:

using (var context = NetMQContext.Create())
{
  string[] topics = new string[] { "A", "B", "C" };
  Random random = new Random();

  using (var publisher = context.CreateXPublisherSocket())
  {
    // Set publisher to manual subscriptions mode
    publisher.Options.ManualPublisher = true;
    publisher.Bind("tcp://*:5556");
    publisher.ReceiveReady += (sender, eventArgs) =>
    {
      var message = publisher.ReceiveString();

      // we only handle subscription requests, unsubscription and any
      // other type of messages will be dropped
      if (message[0] == (char) 1)
      {
        // calling Subscribe to add subscription to the last subscriber
        publisher.Subscribe(message.Substring(1));
      }
    };

    NetMQTimer sendTimer = new NetMQTimer(1000);
    sendTimer.Elapsed += (sender, eventArgs) =>
    {
      // sends a message every second with random topic and current time
      publisher.
        SendMore(topics[random.Next(3)]).
        Send(DateTime.Now.ToString());
    };

    Poller poller = new Poller();
    poller.AddSocket(publisher);
    poller.AddTimer(sendTimer);
    poller.PollTillCancelled();
  }
}

As noted in the example, we confirm all subscription requests, but we drop any other type of request. The subscriber will not be able to unsubscribe.

Permission-Based PubSub

With permission-based PubSub, we first check that the subscriber has permission to subscribe to the topic and only then call the subscribe method.

Permission-based subscriptions have one main issue–because we can only use the Subscribe method on the message we are currently handling, the permission check has to be synchronous. If all permissions are stored in memory, that is not a problem. However, usually we need to check with the database or another service and blocking the publisher thread is not an option. The next pattern solves this issue.

Permission-based subscriptions are only relevant to ZeroMQ and not NetMQ, because NetMQ does not currently support ZMTPv3 and ZAP (authentication).

Token-Based PubSub

With token-based subscriptions, the subscriber uses tokens instead of subscriptions. The server then decodes those tokens and decides to which subscriptions to subscribe the subscriber. To acquire the tokens, the subscriber needs to make a request to an authorization service that generates them. Following is the typical workflow:

  1. The client sends a request with username and password to the authorization service.
  2. The authorization service processes the request and replies to the client with a token.
  3. The client connects to the publisher and subscribes with the token.
  4. The publisher decodes the token, confirming its validity, and adds the relevant subscriptions.

XSub Instead of Sub

For a token-based subscription, we cannot use a Sub socket and must use an XSub socket. Sub sockets filter messages according to the subscriptions. Because we use tokens instead of subscriptions, the subscriber will filter all messages. An XSub socket does not filter messages, so we will use that one.

Generating Tokens

Generating tokens is beyond the scope of this post. I’m certain the web is full of examples. For our demo, I will use HMACSHA1, where the publisher and authorization have to share the same key.

Full Source Code

You can find the full source code of the example at: https://github.com/somdoron/TokenPubSub.

Authorization Service

static void Main(string[] args)
{
	// this key should be shared between authorization server and publisher
	const string Key = "SecretKey";

	using (var context = NetMQContext.Create())
	{
		using (var response = context.CreateResponseSocket())
		{
			response.Bind("tcp://*:5557");

			while (true)
			{
				var requestMessage = response.ReceiveMessage();

				string command = requestMessage.Pop().ConvertToString();

				if (command == AuthorizationProtocol.GetTokenCommand &&
						requestMessage.FrameCount == 3)
				{
					string username = requestMessage.Pop().ConvertToString();
					string password = requestMessage.Pop().ConvertToString();
					string subscription = requestMessage.Pop().ConvertToString();

					// TODO: validating username and password is not part
					// of the example
					// TODO: validate that the user has permission to
					// the subscription is not part of the example

					Console.WriteLine("Received GetTokenCommand {0} {1} {2}",
							username, password, subscription);

					// Create a token
					Token token = new Token(subscription, Key);

					// send token to the client
					response.
							SendMore(AuthorizationProtocol.SuccessReply).
							Send(token.Serialize());
				}
				else
				{
					// unsupported command
					response.Send(AuthorizationProtocol.ErrorReply);
				}
			}
		}
	}
}

Publisher

static void Main(string[] args)
{
	// this key should be shared between authorization server and publisher
	const string Key = "SecretKey";

	string[] symbols = new[] {"EURUSD", "GBPUSD", "EURJPY",
		"USDJPY", "EURGBP", "GBPJPY"};

	Random random = new Random();

	using (var context = NetMQContext.Create())
	{
		using (var publisher = context.CreateXPublisherSocket())
		{
			publisher.Options.ManualPublisher = true;
			publisher.Bind("tcp://*:5558");
			publisher.ReceiveReady += (sender, eventArgs) =>
			{
				byte[] subscriptionBytes = publisher.Receive();

				// first byte indicate if it a subscription or unsubscription
				bool subscription = subscriptionBytes[0] == 1;

				if (subscription)
				{
					// the rest of the bytes is the token, convert them to string
					string serializedToken = Encoding.ASCII.GetString(
							subscriptionBytes, 1, subscriptionBytes.Length - 1);

					// deserialize the token
					Token token;

					if (Token.TryDeserialize(serializedToken, out token))
					{
						// Check if the token is valid
						if (token.Validate(Key))
						{
							if (subscription)
							{
								Console.WriteLine("Subscription request {0}",
										token.Subscription);
								publisher.Subscribe(token.Subscription);
							}
							else
							{
								publisher.Unsubscribe(token.Subscription);
							}
						}
						else
						{
							Console.WriteLine("Invalid token {0}",
									serializedToken);
						}
					}
				}
			};

			// Some fake publishing
			NetMQTimer publishTimer = new NetMQTimer(100);
			publishTimer.Elapsed += (sender, eventArgs) =>
			{
				publisher.
						SendMore(symbols[random.Next(symbols.Length)]).
						Send(random.Next().ToString());
			};

			Poller poller = new Poller();
			poller.AddSocket(publisher);
			poller.AddTimer(publishTimer);
			poller.PollTillCancelled();
		}
	}
}

Client

static void Main(string[] args)
{
	string username = args[0];
	string password = args[1];
	string subscription = args[2].ToUpper();

	using (var context = NetMQContext.Create())
	{
		string token;

		// first we try to get a token
		using (var request = context.CreateRequestSocket())
		{
			request.Connect("tcp://localhost:" + AuthorizationProtocol.Port);

			// send token request
			request.
					SendMore(AuthorizationProtocol.GetTokenCommand).
					SendMore(username).
					SendMore(password).
					Send(subscription);

			string result = request.ReceiveString();

			if (result == AuthorizationProtocol.SuccessReply)
			{
				token = request.ReceiveString();
			}
			else
			{
				throw new Exception("Invalid username or password");
			}
		}

		// we must use XSUB because
		using (var subscriber = context.CreateXSubscriberSocket())
		{
			subscriber.Connect("tcp://localhost:" + StreamingProtocol.Port);

			// create the subscription message
			byte[] subscriptionMessage = new byte[token.Length + 1];
			subscriptionMessage[0] = 1;
			Encoding.ASCII.GetBytes(token, 0, token.Length, subscriptionMessage, 1);
			subscriber.Send(subscriptionMessage);

			while (true)
			{
				string symbol = subscriber.ReceiveString();
				string price = subscriber.ReceiveString();

				Console.WriteLine("{0} {1}", symbol, price);
			}
		}
	}
}

Token

public class Token
{
	public Token(string subscription, string key)
	{
		Subscription = subscription;
		MAC = GenerateMAC(subscription, key);
	}

	public Token()
	{

	}

	public string Subscription { get; set; }
	public string MAC { get; set; }

	private static string GenerateMAC(string subscription, string key)
	{
		HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes(key));
		byte[] hmacBytes = hmac.ComputeHash(Encoding.ASCII.GetBytes(subscription));
		return Convert.ToBase64String(hmacBytes);
	}

	public string Serialize()
	{
		return JsonConvert.SerializeObject(this);
	}

	public bool Validate(string key)
	{
		return MAC.Equals(GenerateMAC(Subscription, key));
	}

	public static bool TryDeserialize(string json, out Token token)
	{
		try
		{
			token = JsonConvert.DeserializeObject<Token>(json);
			return true;
		}
		catch (Exception)
		{
			token = null;
			return false;
		}
	}
}

Token Never Expires

The token in the example never expires. In reality, this is a big concern. Tokens should always have an expiration date. If we use an expiration date on a token, the client should continuously request a new token when the old token expires and subscribe with the replacement token. If the client does not request a new token, they will stop receiving any messages on the next connection drop (ZeroMQ automatically reconnects and sends subscriptions on reconnect. As a result, the publisher would reject the expired token and the client won’t receive any messages).

Summary

Before the advent of the manual subscription, it was impossible to create a secure pub-sub using the Pub and Sub sockets. You could create it using a dealer-router, but you had to manage the subscription store yourself. Manual subscription features make this possible.

NetMQ and IO Completion Ports

One of the original goals of NetMQ was to use IO Completion ports (a.k.a IOCP) on Windows.
I’m happy to let you know that after two years and multiple attempts NetMQ is now using IOCP.

IO Completion Ports

IO Completion ports is windows answer to C10k (http://www.kegel.com/c10k.html,
Wikipedia) problem, C10K problem is the problem of optimizing sockets to handle large number (10K) of clients at the same time. Linux has epoll, FreeBSD has kqueue and Windows has IO Completion ports.

ZeroMQ

ZeroMQ doesn’t scale well on Windows, on Linux ZeroMQ is using epoll which can scale to thousands of sockets. On windows ZeroMQ is using select which is slow and doesn’t scale well. NetMQ was ported from ZeroMQ and up until now it was using select as well.

In the past multiple attempts were made to integrate IOCP to ZeroMQ but none of them succeeded.

Reactor vs Proactor

The main problem of porting network project from Linux (or any other operating system) to Windows is the different asynchronous network model, Linux is using a pattern called reactor and windows is using proactor.

Both reactor and procator patterns enable multiple asynchronous receive and send operations without blocking the thread.
Both are using an event loop, the different is with the meaning of the event.

With reactor pattern you get an event when the socket is ready for an operation. For example you can register a socket for receive readiness and get an event when the data is available for receiving data from the socket.

Linux has a native support for the reactor pattern with epoll (which ZeroMQ is using) that can scale to thousands of clients. Windows also has support for the reactor pattern with select, but as I mentioned select is slow and doesn’t scale well.

With the proactor pattern you first call the method and get an event when the operation is completed. .Net is using the proactor pattern heavily with Begin/End pattern, tasks and Async pattern (from .net4.5) and that is no surprise because Windows has a native support for the proactor pattern with IO Completion ports.

As you can understand it hard to make same code-base support for both reactor and proactor patterns. This the main reason all the attempts to use IO Completion ports in ZeroMQ failed.
ZeroMQ supports multiple implantation of reactor pattern including epoll on linux, kqueue on FreeBSD and of course select on Windows.

On his new project nanomsg, Martin Sustrik, original developer of ZeroMQ, succeeded in using IO Completion ports and epoll linux on the same code-base. Martin’s approach was to make the epoll behave like proactor. In a nutshell, the send/receive is called in a non-blocking way, if the call failed because the socket was not ready the method will be called again once the ready event is sent and only then the procator completion event is raised.

Mono framework is using same approach as nanomsg when running on Linux.

AsyncIO Library

So as I mentioned earlier, in the past I attempted to make NetMQ use IO Completion ports and failed, the main reason is that .Net support for IOCP is a bit annoying because you don’t have a control over which thread the completion event will be handled on.

Eventually I decided to develop my own library for IO Completion ports with control over the thread and using events instead of callbacks. On windows native IO Completion ports API are used (with pinvoke). When running on other platforms (or when forced) the project is using native .Net Async API (which on Linux with Mono using epoll).

You can find the project on Github and Nuget.

Summary

So to summarize, NetMQ master repository is now using IO Completion ports, which means you can use it with thousands of clients (I don’t have the numbers yet).

So if you only used NetMQ to communicate between your servers you can now use it for client-server communication with multiple clients.

Nuget current version of NetMQ (3.3.0.11) is not using IOCP, to get NetMQ with IOCP you need to compile it from the source code.

Using NetMQ and ASP.NET

From time to time a question regarding how to use NetMQ in ASP.NET application is popping up in the NetMQ mailing list so I have decided to write a post on the subject.

WebAPI

For the examples in the post I will use WebAPI 2.0, but it should work for other asp.net application types. Also I’m changing the way WebAPI is configured, here is WebApiConfig class:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
 
        // Web API routes
        config.MapHttpAttributeRoutes();
 
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

The only difference is using /{action} instead of /{id} and this is because I’m not writing a REST service.

I’m using Autofac as dependency injection for the examples in the post.

Calculator

Through out the post I will use a simple calculator example, following is the code of the calculator server application:

class Program
{
    static void Main(string[] args)
    {
        using (NetMQContext context = NetMQContext.Create())
        {
            using (var responseSocket = context.CreateResponseSocket())
            {
                responseSocket.Bind("tcp://*:10001");
 
                while (true)
                {
                    var requestMessage = responseSocket.ReceiveMessage();
                    string a = requestMessage.Pop().ConvertToString();
                    string b = requestMessage.Pop().ConvertToString();
 
                    int aNumber = Convert.ToInt32(a);
                    int bNumber = Convert.ToInt32(b);
 
                    string result = (aNumber + bNumber).ToString();
 
                    NetMQMessage responseMessage = new NetMQMessage();
                    responseMessage.Append(result);
 
                    responseSocket.SendMessage(responseMessage);
                }
            }
        }
    }
}

Simple Pattern

In the simple pattern each controller will create and connect it’s own socket. The NetMQ context will be created once and will be injected into the controllers.

SimpleController.cs

public class SimpleController : ApiController
{
    private readonly NetMQContext m_context;
    private string m_serviceAddress;
 
    public SimpleController(NetMQContext context, string serviceAddress)
    {
        m_context = context;
        m_serviceAddress = serviceAddress;
    }
 
    [HttpGet]
    public int Calc(int a, int b)
    {
        using (var requestSocket = m_context.CreateRequestSocket())
        {
            requestSocket.Connect(m_serviceAddress);            
 
            NetMQMessage message = new NetMQMessage();
            message.Append(a.ToString()); // converting to string, not most efficient but will do for our example
            message.Append(b.ToString());
 
            requestSocket.SendMessage(message);
 
            var replyMessage = requestSocket.ReceiveMessage();
            string result = replyMessage.Pop().ConvertToString();
 
            return Convert.ToInt32(result);
        }
    }
}

Global.asax.cs:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        string address = "tcp://127.0.0.1:10001";
 
        var builder = new ContainerBuilder();
 
        // Register the NetMQ context
        builder.RegisterInstance(NetMQContext.Create()).SingleInstance();
        builder.RegisterType().WithParameter("serviceAddress", address);
 
        // Build the container.
        var container = builder.Build();
 
        // Create the dependency resolver.
        var resolver = new AutofacWebApiDependencyResolver(container);
 
        // Configure Web API with the dependency resolver.
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
 
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

The advantage of the simple pattern is that it’s very simple.

However we are creating and connecting a TCP socket on each request, this is not efficient and can take time (Because of TCP and ZMTP handshake process).

We can fix this easily with a device in the middle, and this take us to the next solution.

Simple Device Pattern

Device in ZeroMQ/NetMQ is a component that sits in the middle of zeromq applications and forward messages between them, you can learn more about devices at the zeromq guide.

The simple device bind on a inproc address and connect to the calculator service.

Any request coming from the inproc is forward to the service and responses are routing back to the inproc socket.

We don’t change anything in the SimpleController except injecting the inproc address instead of the service address.

Let’s take a look at the Device class:

public class Device : IDisposable, IStartable
{
    private readonly NetMQContext m_context;
    private readonly string m_backEndAddress;
    private readonly string m_frontEndAddress;
    private Poller m_poller;
    private RouterSocket m_frontendSocket;
    private DealerSocket m_backendSocket;
 
    public Device(NetMQContext context, string backEndAddress, string frontEndAddress)
    {
        m_context = context;
        m_backEndAddress = backEndAddress;
        m_frontEndAddress = frontEndAddress;
    }
 
    public void Start()
    {
        Task.Factory.StartNew(() =>
        {
            m_poller = new Poller();
 
            using (m_frontendSocket = m_context.CreateRouterSocket())
            {
                using (m_backendSocket = m_context.CreateDealerSocket())
                {
                    m_backendSocket.Connect(m_backEndAddress);
                    m_frontendSocket.Bind(m_frontEndAddress);
 
                    m_backendSocket.ReceiveReady += OnBackEndReady;
                    m_frontendSocket.ReceiveReady += OnFrontEndReady;
 
                    m_poller.AddSocket(m_backendSocket);
                    m_poller.AddSocket(m_frontendSocket);
 
                    m_poller.Start();
                }
            }
 
 
        }, TaskCreationOptions.LongRunning);
    }
 
    private void OnFrontEndReady(object sender, NetMQSocketEventArgs e)
    {
        NetMQMessage message = m_frontendSocket.ReceiveMessage();
        m_backendSocket.SendMessage(message);
    }
 
    private void OnBackEndReady(object sender, NetMQSocketEventArgs e)
    {
        NetMQMessage message = m_backendSocket.ReceiveMessage();
        m_frontendSocket.SendMessage(message);
    }
 
    public void Dispose()
    {
        m_poller.Stop(true);
    }
}

The device class will start automatically by the Autofac because it is inherited from IStartable.

Let’s take a look at the global.asax.cs file:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        const string serviceAddress = "tcp://127.0.0.1:10001";
        const string inprocAddress = "inproc://broker";        
 
        var builder = new ContainerBuilder();
 
        // Register the NetMQ context
        builder.RegisterInstance(NetMQContext.Create()).SingleInstance();
        builder.RegisterType().
            WithParameter("serviceAddress", inprocAddress).InstancePerRequest();
        builder.RegisterType().SingleInstance().As().
            WithParameter("backEndAddress", serviceAddress).
            WithParameter("frontEndAddress", inprocAddress);
 
        // Build the container.
        var container = builder.Build();
 
        // Create the dependency resolver.
        var resolver = new AutofacWebApiDependencyResolver(container);
 
        // Configure Web API with the dependency resolver.
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
 
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

With the simple device only one place connects to the calculator service and in rest of the web application we send the request to the device using inproc transport, also as you see we didn’t have to change anything in the controller code.

The simple device pattern will usually be enough for most asp.net web application, however we still have some problems that the simple device pattern doesn’t solve:

    1. We are still creating and disposing a lot of NetMQ sockets and sockets are an expensive resource.
    2. The controllers code is blocking, we occupy a thread until the response is arrived
    3. Timeout is not handled, what if the response is gone? or the calculator service is down?

For the third problem I suggest reading the Reliable Request-Reply chapter at the zeromq guide.

For a quick fix we can set the ReceiveTimeout of the socket and catch the AgainException, like this:

[HttpGet]
public IHttpActionResult Calc(int a, int b)
{
    using (var requestSocket = m_context.CreateRequestSocket())
    {
        requestSocket.Options.ReceiveTimeout = TimeSpan.FromSeconds(10);
        requestSocket.Connect(m_serviceAddress);
 
        NetMQMessage message = new NetMQMessage();
        message.Append(a.ToString()); 
        message.Append(b.ToString());
 
        requestSocket.SendMessage(message);
 
        try
        {
            var replyMessage = requestSocket.ReceiveMessage();
            string result = replyMessage.Pop().ConvertToString();
 
            return Ok(Convert.ToInt32(result));
        }
        catch (AgainException ex)
        {
            return BadRequest();
        }
 
    }
}

For the blocking problem we need convert our controllers to be asynchronous, imagine we can wrote this:

[HttpGet]
public async Task Calc(int a, int b)
{
    using (var requestSocket = m_context.CreateRequestSocket())
    {
        requestSocket.Options.ReceiveTimeout = TimeSpan.FromSeconds(10);
        requestSocket.Connect(m_serviceAddress);
 
        NetMQMessage message = new NetMQMessage();
        message.Append(a.ToString()); 
        message.Append(b.ToString());
 
        requestSocket.SendMessage(message);
 
        try
        {
            var replyMessage = await requestSocket.ReceiveMessageAsync();
            string result = replyMessage.Pop().ConvertToString();
 
            return Ok(Convert.ToInt32(result));
        }
        catch (AgainException ex)
        {
            return BadRequest();
        }
 
    }
}

But sadly we cannot write this code, yet.

AsyncSocket

In the last pattern in the post I will write an asynchronous wrapper for NetMQ socket in order to be able to use the async/await keywords of .Net 4.5.

So the code here is getting a little complicated, we will have to use TaskCompletionSource and NetMQScheduler, take a look:

public class AsyncSocket : IDisposable
{
    private readonly NetMQContext m_context;
    private readonly string m_serviceAddress;
    private NetMQScheduler m_scheduler;
    private Poller m_poller;
    private NetMQSocket m_requestSocket;
    private TaskCompletionSource<NetMQMessage> m_taskCompletionSource; 
 
    public AsyncSocket(NetMQContext context, string address)
    {
        m_context = context;
        m_serviceAddress = address;
 
        m_requestSocket = context.CreateRequestSocket();
        m_requestSocket.ReceiveReady += OnReceiveReady;
        m_requestSocket.Connect(address);
 
        m_poller = new Poller(m_requestSocket);
        m_scheduler = new NetMQScheduler(m_context, m_poller);
 
        Task.Factory.StartNew(() => m_poller.Start(), TaskCreationOptions.LongRunning);
    }
 
    public Task<NetMQMessage> SendAndReceiveAsync(NetMQMessage message)
    {
        var task = new Task<Task<NetMQMessage>>(() =>
        {
            m_taskCompletionSource = new TaskCompletionSource<NetMQMessage>();
 
            m_requestSocket.SendMessage(message);
 
            return m_taskCompletionSource.Task;
        });
 
        // will start the task on the scheduler which the same thread as the Poller thread
        task.Start(m_scheduler);
        return task.Result;
    }
 
    private void OnReceiveReady(object sender, NetMQSocketEventArgs e)
    {
        NetMQMessage message = m_requestSocket.ReceiveMessage();
        m_taskCompletionSource.SetResult(message);
        m_taskCompletionSource = null;
    }
 
    public void Dispose()
    {
        m_scheduler.Dispose();
        m_poller.Stop(true);
        m_requestSocket.Dispose();            
    }
}

And the Controller code:

[HttpGet]
public async Task Calc(int a, int b)
{
    using (var asyncSocket = new AsyncSocket(m_context, m_serviceAddress))
    {
        NetMQMessage message = new NetMQMessage();
        message.Append(a.ToString());
        message.Append(b.ToString());
 
        var replyMessage = await asyncSocket.SendAndReceiveAsync(message);
 
        string result = replyMessage.Pop().ConvertToString();
 
        return Ok(Convert.ToInt32(result));
    }
}

So now we have an async socket with an async controller, but we created a new problem, we now create a background thread for every request coming, how can we fix that?

We can change the AsyncSocket to handle multiple events, the pure zeromq way to write this is complicated and actually not very neat (NetMQ cannot pass objects, we have to pass the TaskCompletionSource between sockets, the only way to that is using there address, which is not very neat).

The way I’m going to implement it will force us to share AsyncSocket between threads, however it will be completely safe and lock-free with the magic of NetMQScheduler.

public class AsyncSocket : IStartable, IDisposable
{
    private readonly NetMQContext m_context;
    private readonly string m_serviceAddress;
    private NetMQScheduler m_scheduler;
    private Poller m_poller;
    private NetMQSocket m_dealerSocket;
    private Dictionary<int, TaskCompletionSource<NetMQMessage>> m_requests;
    private int m_requestId;
 
    public AsyncSocket(NetMQContext context, string address)
    {
        m_context = context;
        m_serviceAddress = address;
        m_requests = new Dictionary<int, TaskCompletionSource<NetMQMessage>>();
        m_requestId = 0;       
    }
 
    public void Start()
    {
        m_dealerSocket = m_context.CreateDealerSocket();
        m_dealerSocket.ReceiveReady += OnReceiveReady;
        m_dealerSocket.Connect(m_serviceAddress);
 
        m_poller = new Poller(m_dealerSocket);
        m_scheduler = new NetMQScheduler(m_context, m_poller);
 
        Task.Factory.StartNew(() => m_poller.Start(), TaskCreationOptions.LongRunning);
    }
 
    public Task<NetMQMessage> SendAndReceiveAsync(NetMQMessage message)
    {
        // duplicate the message because we are not the owner of the message
        NetMQMessage duplicteMessage = new NetMQMessage(message);
 
        var task = new Task<Task<NetMQMessage>>(() =>
        {
            var taskCompletionSource = new TaskCompletionSource<NetMQMessage>();
 
            // because we are using a dealer we have to push the delimiter
            duplicteMessage.PushEmptyFrame();
 
            // sending the request id the request identifier
            duplicteMessage.Push(m_requestId.ToString());
 
            // add the request to the pending request dictionary
            m_requests.Add(m_requestId, taskCompletionSource);
 
            // increase the request id for the next request
            m_requestId++;
 
            m_dealerSocket.SendMessage(duplicteMessage);
 
            return taskCompletionSource.Task;
        });
 
        // will start the task on the scheduler which is the same thread as the Poller thread
        task.Start(m_scheduler);
        return task.Result;
    }
 
    private void OnReceiveReady(object sender, NetMQSocketEventArgs e)
    {
        NetMQMessage message = m_dealerSocket.ReceiveMessage();
 
        // pop the request id
        string identity = message.Pop().ConvertToString();
 
        // pop the delimiter
        message.Pop();
 
 
        int requestId = Convert.ToInt32(identity);
 
        TaskCompletionSource<NetMQMessage> taskCompletionSource;
 
        // getting the task completion source, if we were also try to handle timeout the request will be gone and the response will be dropped
        if (m_requests.TryGetValue(requestId, out taskCompletionSource))
        {
            taskCompletionSource.SetResult(message);
            m_requests.Remove(requestId);
        }                
    }
 
    public void Dispose()
    {
        m_scheduler.Dispose();
        m_poller.Stop(true);
        m_dealerSocket.Dispose();            
    }  
}

Please note that with new implementation of AsyncSocket we don’t need the SimpleDevice anymore and the AsyncSocket can connect directly to the service, the reason is that we know only have one socket connecting to the service.

This how our new controller looks:

public class AsyncController : ApiController
{
    private AsyncSocket m_asyncSocket;
 
    public AsyncController(AsyncSocket asyncSocket)
    {
        m_asyncSocket = asyncSocket;
    }
 
    [HttpGet]
    public async Task Calc(int a, int b)
    {
        NetMQMessage message = new NetMQMessage();
        message.Append(a.ToString()); // converting to string, not most effient but will do for our example
        message.Append(b.ToString());
 
        var replyMessage = await m_asyncSocket.SendAndReceiveAsync(message);
 
        string result = replyMessage.Pop().ConvertToString();
 
        return Ok(Convert.ToInt32(result));
    }
}

And the autofac magic:

protected void Application_Start()
{
    const string serviceAddress = "tcp://127.0.0.1:10001";
    const string inprocAddress = "inproc://broker";        
 
    var builder = new ContainerBuilder();
 
    // Register the NetMQ context
    builder.RegisterInstance(NetMQContext.Create()).
        SingleInstance();
    builder.RegisterType().
        WithParameter("serviceAddress", inprocAddress).
        InstancePerRequest();        
    builder.RegisterType().SingleInstance().
        As().
        AsSelf().
        WithParameter("address", serviceAddress);
 
    // Build the container.
    var container = builder.Build();
 
    // Create the dependency resolver.
    var resolver = new AutofacWebApiDependencyResolver(container);
 
    // Configure Web API with the dependency resolver.
    GlobalConfiguration.Configuration.DependencyResolver = resolver;
 
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

As you can see I’m handling timeouts in the code, good handling of timeouts and reliability is out of the scope for the this post, however if you already read the zeromq guide you probably know how to handle reliability. An easy fix here would be to also record the request time of each request and use NetMQTimer to remove timed-out request (we can call SetException on the TaskCompletionSource).

Summary

In the post we explore 3 patterns to use NetMQ inside ASP.NET application, the Simple Pattern,
the Simple Device Pattern and the Async Socket Pattern.

Although the AsyncSocket is not pure NetMQ solution and we have to share the object between threads it is my favorite, using the AsyncSocket we can write very fast lock-free ASP.NET controllers without blocking an ASP.NET thread.

What would be a nicer solution? what about writing the entire web server using NetMQ? without ASP.NET at all? would it be nice to write the following code?

using (NetMQContext context = NetMQContext.Create())
{
    using (var responseSocket = context.CreateResponseSocket())
    {
        responseSocket.Bind("http://localhost:80/api/Calculator/Calc");                
 
        while (true)
        {
            var requestMessage = responseSocket.ReceiveMessage();
 
            string a = requestMessage.Pop().ConvertToString();
            string b = requestMessage.Pop().ConvertToString();
 
            int aNumber = Convert.ToInt32(a);
            int bNumber = Convert.ToInt32(b);
 
            string result = (aNumber + bNumber).ToString();
 
            NetMQMessage responseMessage = new NetMQMessage();
            responseMessage.Append(result);
 
            responseSocket.SendMessage(responseMessage);
        }
    }
}

Maybe one day…

Introducing NetMQ.WebSockets and JSMQ

NetMQ version 3.3.10.0 introduced Stream socket type, which added the ability to read raw data from a TCP socket.
Today I want to introduce what you can do with Stream socket type and what I think could be a great development for NetMQ.

Let’s start with NetMQ.WebSockets, NetMQ.WebSockets is an extension to NetMQ which adds WebSocket transport as an extension.

Because NetMQ doesn’t have a pluggable transport feature, NetMQ.WebSockets actually wraps NetMQ and provides a new socket object which has a very similar interface as the NetMQ socket.
NetMQ.WebSockets currently implements only Router and Publisher patterns.

So who can communicate with NetMQ.WebSockets? Time to introduce JSMQ.

JSMQ is a NetMQ/ZeroMQ client in javascript whose API is very similar to other zeromq bindings and can communicate with NetMQ.WebSockets.

Now some of the C/C++ or even Java gurus need to throw down the gauntlet and implement WebSocket extension for zeromq/JeroMQ and then we will have a javascript library that can talk to all zeromq implementations.

You can find the projects on github:
https://github.com/somdoron/NetMQ.WebSockets
https://github.com/somdoron/JSMQ

You can download both JSMQ and NetMQ.WebSockets from nuget (make sure to choose prerelease) or visit there pages:
https://www.nuget.org/packages/NetMQ.WebSockets
https://www.nuget.org/packages/JSMQ

And now let’s see some examples:

NetMQ.WebSockets example

static void Main(string[] args)
{
  using (NetMQContext context = NetMQContext.Create())
  {
    using (WSRouter router = context.CreateWSRouter())
    using (WSPublisher publisher = context.CreateWSPublisher())
    {
      router.Bind("ws://localhost:80");
      publisher.Bind("ws://localhost:81");
 
      router.ReceiveReady += (sender, eventArgs) =&gt;
      {
        string identity = eventArgs.WSSocket.Receive();
        string message = eventArgs.WSSocket.Receive();
 
        eventArgs.WSSocket.SendMore(identity).Send("OK");
 
        eventArgs.WSSocket.SendMore("chat").Send(message);
      };
 
      Poller poller = new Poller();
      poller.AddWSSocket(router);
 
      // we must add the publisher to the poller although we are not registering to any event.
      // the protocol processing is happening in the user thread, without adding the publisher to the poller
      // the next time the publisher will accept a socket or receive a subscription is only when send is called.
      // when socket is added to the poller the processing is happen everytime data is ready to be processed
      poller.AddWSSocket(publisher);
      poller.Start();
 
    }
  }
}

JSMQ example

Javascript File

var dealer = new Dealer();
dealer.connect("ws://localhost");
 
// we must wait for the dealer to be connected before we can send messages, 
// any messages we are trying to send while the dealer is not connected will be dropped
dealer.sendReady = function() {
    document.getElementById("sendButton").disabled = "";
};
 
var subscriber = new Subscriber();
subscriber.connect("ws://localhost:81");
subscriber.subscribe("chat");
 
subscriber.onMessage = function (message) {
    // message is an array of all the message parts
    // we ignore the first frame because it's the topic
 
    document.getElementById("chatTextArea").value =
        document.getElementById("chatTextArea").value +
        message[1]  + "\n";
};
 
dealer.onMessage = function (message) {
    // the response from the server
    alert(message[0]);
};
 
function send() {
    dealer.send(document.getElementById("messageTextBox").value);
}

HTML File

<textarea id="chatTextArea" readonly="readonly"></textarea> <label>Message:</label><input id="messageTextBox" type="text" value="" /> <button id="sendButton" onclick="javascript:send();" disabled="disabled"> Send </button>

NetMQ 3.3.0.10 Released

A new NetMQ version was released today to nuget, you can find it at https://www.nuget.org/packages/NetMQ/, or just search NetMQ on nuget.

Features:

  • Stream socket, coming from ZeroMQ, you can use NetMQ to talk to any protocol.
  • ReceiveString with a timeout parameter.
  • NetMQMessage.ToString is now returning a better result.
  • BindRandomPort – which bind the socket to a random port.
  • The assembly is now signed

Bug Fixes:

  • NetMQMonitor – Null reference exception was thrown when used ConnectDelayed.
  • NetMQScheduler – socket was created for each thread that used the scheduler.
  • Bind and Connecting on local was a big mess, now it’s working as should.
  • IPv6 – tested, also when now using IPv6 the socket will also accept IPv4 sockets if the operation system support it (> Windows Vista).

If you wonder what you can do with Stream, what about talking in WebSocket to web browser? Stay tuned for the next post…

Enjoy.

.Net Async Programming – Part 2

In the first article in the series I covered the programming with a single thread, in this article I will cover how to call blocking method in our one thread without blocking our single thread.

In order to write completely async code with a single thread we need to make sure that the one thread we have will always do work and not block on IO operation (disk, network, database, etc…).
Some IO classes on .net have async pattern implementation, in the next article we will see how to use them as well.

Let’s that when a deposit is made we want to save it to DB, so we have a repository with the insert method and when the Deposit method is being called we will insert it to the DB:

public Task Deposit(double amount)
{
  Task task = new Task(() =>
    {
      m_balance += amount;
 
      m_depositRepository.Insert(AccountId, amount);
    });
  task.Start(m_taskScheduler);
 
  return task;
}

We took the example from the last post and just added the insert call, the problem with this code is that our single thread will block on the Insert call until the insert is complete, deny other tasks from being processed. To solve this we are going to schedule the Insert call on a thread pool thread:

public Task Deposit(double amount)
{
  Task task = new Task(() =>
    {
      m_balance += amount;
 
      Task.Factory.StartNew(() => m_depositRepository.Insert(AccountId, amount));
    });
  task.Start(m_taskScheduler);
 
  return task;
}

Now the single thread won’t be blocked, but there is a still a problem with this code because the Deposit method might return before the insert is actually completed, compromise the integrity of our code and software, let’s try to solve this using .Net 4.0:

public Task Deposit(double amount)
{
  TaskCompletionSource<int> taskCompletionSource = new TaskCompletionSource<int>();
 
  Task task = new Task(() =>
    {
      m_balance += amount;
 
      Task.Factory.StartNew(() => m_depositRepository.Insert(AccountId, amount)).ContinueWith(() => taskCompletionSource.SetResult(1));
    });
  task.Start(m_taskScheduler);
 
  return taskCompletionSource.Task;
}

We are using a task completion source (we are using int because there is no void task completion source, yes it is ugly) to signal when the insert is actually completed.
The problem with this code is that it’s not so elegant and contain too much boilerplate code, with .Net 4.5 this become much easier:

public async Task Deposit(double amount)
{
  Task task = new Task(() =>
  {
    m_balance += amount;
 
    await Task.Factory.StartNew(() => m_depositRepository.Insert(AccountId, amount)).ConfigureAwait(true);
  });
  task.Start();
 
  await task;
}

I’m not going to get into the details of async and await, you can read more on that on msdn, but few important things need mentioning, we are using ConfigureAwait to make sure the continuation will be on our single thread and because we cannot return the task as we did in the previous example (because the async keyword) but we still want to the Deposit method to mark as completed only when the inner task complete we await for the inner task.

Summary

Using the thread pool is for the example but if our system is handling a lot of requests we can exhaust the thread pool with the blocking operations, the better approach will be to use the Async Pattern of .Net, we will see an example in our next article in the series.

Another thing worth mentioning, when writing a single thread code the gate (the place we transition into our thread) will not actually be in our business objects as in our example, I’m only doing it there for sake of the example, it will probably be in higher layers (application layer, infrastructure of the facade, depend on your system architecture), I will discuss this in future article in the series.

.Net Async Programming – Part 1

Introduction

When working on high performance systems (thousands of operation per second) very fast you understand that you cannot use locks (to synchronize access to state) in your code and also cannot do any blocking operation (like writing to a file, socket or a database).

The remain question is, How can you write you code without a single lock and without any blocking operation?
In this part I will cover how to write code with zero locks (Blocking operation will be covered in next articles).

Single Thread Programming

I want to take you years back when we only had one processor and this processor had one core and even before threads and multi-threading exist.
If I told that all of your code is running on a single thread? If everything is running on a single thread you don’t need locks, and if everything is running on one thread you actually not using multithreading.

You are probably thinking but one thread won’t be fast enough, well you are wrong (in the next article of the series we are going to cover multi-threads as well).
When you really need more than one thread (I really doubt it) the magic trick is to not have shared state between the threads, each state belong to one thread and only one thread can read and write from it, but more about this in the next articles.

If you are building a system that doesn’t need to support more then 1000 (and actually even much higher numbers) operation per second (I’m assumening that the operation is short and without blocking IO) you should use one thread, of course there are more variables but this is a rule of thumb.

SingleThreadTaskScheduler

So after the long introduction, how do we use only one thread in C# ? Actually everything already exist in C#, we just taking 3 different component and combine them together: BlockingCollection, TaskSchdeuker and Thread, following is a simple implementation:

  public class SingleThreadTaskScheduler : TaskScheduler, IDisposable, IOrderedScheduler
  {
    private BlockingCollection<Task> m_queue = new BlockingCollection<Task>();
    private ThreadLocal<bool> m_isSchedulerThread = new ThreadLocal<bool>(() => false);
    private Task m_workerTask;
    private bool m_disposed = false;
 
    public SingleThreadTaskScheduler()
    {
      m_workerTask = Task.Factory.StartNew(() => Worker(), TaskCreationOptions.LongRunning);
    }
 
    ~SingleThreadTaskScheduler()
    {
      Dispose();
    }
 
    private void Worker()
    {
      m_isSchedulerThread.Value = true;
      try
      {
        foreach (Task task in m_queue.GetConsumingEnumerable())
        {
          base.TryExecuteTask(task);
        }
      }
      finally
      {
        m_isSchedulerThread.Value = false;
      }
    }
 
    protected override IEnumerable<Task> GetScheduledTasks()
    {
      return m_queue.ToArray();
    }
 
    protected override void QueueTask(Task task)
    {
      m_queue.Add(task);
    }
 
    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
    {
      return m_isSchedulerThread.Value && TryExecuteTask(task);
    }
 
    public void Dispose()
    {
      if (!m_disposed)
      {
        m_queue.CompleteAdding();
        m_workerTask.Wait();
        m_disposed = true;
 
        GC.SuppressFinalize(this);
      }
    }
  }

Async Programming

Now our application receiving input from somewhere (for the example let’s assume WCF service) our example will be a Brokerage service which provide the client with the current balance, before using the Task Scheduler the code will probably look something like this:

public double GetBalance(int accountId)
{
  Account account = m_accountRepository.GetAccountById(accountId);
 
  return account.GetBalance();
}

Now the simplest thing to do is to wrap this code with Task and invoke on our new task scheduler:

public double GetBalance(int accountId)
{
  Task<double> task =
    new Task<double>(() =>
      {
        Account account = m_accountRepository.GetAccountById(accountId);
        return account.GetBalance();
      });
 
  task.Start(m_singleThreadTaskScheduler);
  task.Wait();
 
  return task.Result;
}

But this code is not encapsulated, somebody can call the method without wrapping with task and run it on wrong task scheduler, we can insert the wrapping into the Account class:

class Account
{
  private readonly SingleThreadTaskScheduler m_taskScheduler;
  private double m_balance = 0;
 
  public Account(SingleThreadTaskScheduler taskScheduler)
  {
    m_taskScheduler = taskScheduler;
  }
 
  public double GetBalance()
  {
    Task<double> task = new Task<double>(() => m_balance);
    task.Start(m_taskScheduler);
    task.Wait();
 
    return task.Result;
  }
}

Now the account class is encapsulated and making sure nobody can access the class state from another thread, the SingleThreadTaskSchduler will be injected to all of our classes or it can also be static (we already said we will have only one thread).
We can improve our Account class a little more and return a task instead of the double, this will come very handy in .Net 4.5 with the await keyword, but more about this in the next articles in the series, for the example we also adding another method to account to show both methods write and read the state.

class Account
{
  private readonly SingleThreadTaskScheduler m_taskScheduler;
  private double m_balance = 0;
 
  public Account(SingleThreadTaskScheduler taskScheduler)
  {
    m_taskScheduler = taskScheduler;
  }
 
  public Task<double> GetBalance()
  {
    Task<double> task = new Task<double>(() => m_balance);
    task.Start(m_taskScheduler);
    task.Wait();
 
    return task;
  }
 
  public Task Deposit(double amount)
  {
    Task task = new Task(() =>
      {
        m_balance += amount;
      });
 
    return task;
  }
}

And for last we can now implement the WCF service with the async pattern model using the task from the Account.GetBalance and our system is completely async:

public IAsyncResult BeginGetBalance(int accountId, AsyncCallback callback, object state)
{
  Account account = m_accountRepository.GetAcountById(accountId);
 
  Task<double> task = account.GetBalance();
 
  // using Task extension method from http://blogs.msdn.com/b/pfxteam/archive/2011/06/27/10179452.aspx
  return task.ToAPM(callback, state);
}
 
public double EndGetBalance(IAsyncResult asyncResult)
{
  return ((Task<double>) asyncResult).Result;
}

In .Net 4.5 WCF contracts can have methods that return tasks so the GetBalance method can return the Account.GetBalance directly, like so:

public Task<double> GetBalanceAsync(int accountId)
{
  Account account = m_accountRepository.GetAccountById(accountId);
 
  return account.GetBalance();
}

In the next article I will cover calling IO blocking methods without blocking our one and only thread.

Distributed .Net with NetMQ