Feature Requests for C# 7.0

C# is an amazing language which get improved with every version and now .Net become portable which is even more amazing. However there are still some features I'm missing or problems I want to see solved.

Scope and Resource

IDisposable interface and using statement are problematic, I really think it is the second biggest design flaw in C# (after events, the cause of all memory leaks).

Some of the issues:

  • You can forget to dispose an object and cause memory leak or exhaust operating system resources.
  • With using keyword you unnecessarily indenting the code and make it harder to read, especially when nesting multiple using.
  • You don't get any errors with you using object that implements IDisposable without using using, this is just calling for bugs.
  • If you are writing a public library and didn't use IDisposable at the beginning and add it later you actually cause same problem as forgetting to dispose an object as in the first bullet.

So the first addition is the resource keyword which you can use at class declaration. When using the resource keyword it is like implementing IDisposable except you get a compile error if you didn't scope the instance. So what exactly is scope? first let see the how to use the resource keyword:

resource class Socket
{
  void Dispose()
  {
    // Do something, or not...
  }
}

Actually the Dispose method is optional, because usually with resource class all resources will be released automatically because of the scope keyword.

So what is the scope keyword, when create a resource class you must scope (in golang it called defer), when you scope a resource class it will get disposed when you exit the scope. Scope can be at class level or scope level.

Let see some examples:

class resource PersistentList
{
  // class level scope variable
  private scope AutoResetEvent loadedEvent = new AutoResetEvent();

  public void Load()
  {
     scope FileStream stream = new FileStream(FileName);

     loadedEvent.Set();
  }

  public void Save()
  {
    // If we really want we can still use Using
    using (FileStream stream = new FileStream(FileName))
    {
    }
  }  
}

So as you see we don't even need to implement the Dispose method, because all resources will be disposed automatically when the scope will exit. The important role of the resource keyword is to make sure a compilation error will happen if resource class is used without scope or using. Bottom line this saves bugs, memory leaks, writing dispose method and code is cleaner (no unnecessary indent).

Messaging

It is hard to write high performance or concurrent systems with C# and .Net, I know this from first experience, even async and await doesn't really solve the problem. The real problem is that C# and .Net doesn't encourage the right pattern for high performance systems.

Let me explain, if you read the ZeroMQ guide or familiar with NetMQ and/or ZeroMQ you know the philosophy is to never share data between threads, instead we exchange messages between threads. Sharing data between thread is slow and very complicated, usually an art only few can master.

The problem with C# is that sharing data is in the syntax of the language with lock and volatile keywords. Also the library is full with share data classes like ConcurrentBag, ConcurrentDictionary, ReaderWriterLock, ManualResetEvent and more.

Language that want to support high performance should prefer messaging over locks, golang has channels (which are like queues) and goroutines (which are cheap threads), erlang has actors, and C#? C# has BlockingCollection, only from .net 4.0, which is not enough.

First we need a way to create threads that are not really OS threads, we can do this with the various single thread TaskScheduler implementations (as NetMQ Poller is doing) but remember we want the support in the language.
The other parts is that we need a way to wait on a queue (without really blocking a thread) and on multiple queues.

Following is a suggestion:

void Main()
{
  Channel<int> channel1 = new Channel<int>();
  Channel<int> channel2 = new Channel<int>();  
  Channel<int> channel3 = new Channel<int>();  

  // Calling a concurrent routine
  SingelQueue(channel1);
  MultipleChannels(channel2, channel3);

  // Sending to a channel
  5 => channel1;
}

concurrent void SingleQueue(Channel<int> channel)
{
  while (channel.Open)
  {
    // Receiving from channel, this is actually not blocking the
    // thread but release it back to the thread pool, like await.
    var item <= channel; 
  }
}

concurrent void MultipleChannels(
  Channel<int> channel1, Channel<int> channel2)
{
  while (channel.Open)
  {
    select
    {
      case var m <= channel1:
        // Do some processing
        break;
      case var m <= channel2:
        // Do some processing
        break;
    }
  }
}

Another important point is to make this extendable (which golang didn't do), so if I want to develop my custom channel (NetMQChannel for example) I can, following is a suggestion for receiving interface:

delegate void Callback(IReceiveChannel<T> channel);

interface IReceiveChannel<T>
{
  void RegisterForCallback(Callback callback);  
  bool TryReceive(out T value);
}

So the custom channel will need to implement RegisterForCallback which will be called when message is ready to be fetched.
TryReceive, which will be called within the callback, will actually retrieve the message if it still available.

For more information take a look at libmill for C or golang channels.

Parameter-less Lambda

Today when you want to create parameter-less lambda you have to use parenthesis, which is just boilerplate and verbose, so instead of this:

() => Console.WriteLine("Hello");

I want to do this:

=> Console.WriteLine("Hello);

Syntax is cleaner and less code to write.

Multiple return value and pre-statement

In my opinion the exception usages in .net library is mostly wrong, if we actually want to write safe code almost every line of code need to be wrapped with try-catch. I much prefer the Try pattern, like with TryGetValue of dictionary.

However using the Try pattern is a little annoying, you need to remember to create a variable before calling the Try method, which is not how my brain works, when I get to the Try method I usually go up one line and declare the out variable. Also the variable is defined in an outer scope while you usually use it inside the if statement. My suggestion is to return multiple values, so instead of this:

string text;
if (dictionary.TryGetValue("Greeting", out text))
  Console.WriteLine(text);

We will write this:

if (var text, found = dictionary.TryGetValue("Greeting"); found)
  Console.WriteLine(text);

The text variable is defined in the correct scope, we don't have to use the out keyword and wrote less code. I like this syntax better, although the pre-statement can look better, I'm leaving this to Microsoft.

Instant enums

Continuing the previous example, it would be much nicer if instead of checking the found boolean value we will do it like this:

if (var text, result = dictionary.TryGetValue("Greeting"); 
    result == Found)
  Console.WriteLine(text);

The problem with writing a lot of enums, it is too verbose, you get a class with a lot of internal public enums, you don't know which method use which enum and you also have to think of the names for all those enums. So I would like to do it in method declaration:

// Property
public {NotStarted, Running, Disposed} Status {get; private set;}

// Method
public {Ok, QueueIsFull} Enqueue(string value);

// Method with two return values
public string, {Found, Missing} TryGetValue(string key);

Summary

Hopefully you get the point, I didn't make it bulletproof features yet, just suggestions and way of thinking. The bottom line is clearer code, less code, more safe, less bugs and built for performance.

Reliable PubSub

In the last post I told you about two new features I recently pushed to both ZeroMQ and NetMQ. Last post was about the manual subscription feature. In this post I will create a reliable pubsub using the new Welcome Message feature.

I will use NetMQ through out the post but everything apply to ZeroMQ as well.

ZeroMQ guide has a great chapter on pubsub including reliable pubsub, however I will tackle a different problem, how to make sure the subscriber is always connected to a publisher (and also to the closest one).

Welcome Message

Welcome message is pretty simple, when welcome message is set on a publisher (must be of type XPub) the publisher will send a welcome message to each new subscriber. So the first message subscriber receive will be the welcome message. Subscriber must subscribe to the welcome message before trying to connect.

To set the welcome message with ZeroMQ you need to call zmq_setsockopt with ZMQ_XPUB_WELCOME_MSG and the welcome message.

Following is small example of setting welcome message with NetMQ:

using (var context = NetMQContext.Create()) 
{ 
  using (var publisher = context.CreateXPublisherSocket()) 
  { 
    // Set the welcome message, this will be sent to any new subscriber
    publisher.SetWelcomeMessage("WM"); 
    publisher.Bind("tcp://*:5555"); 

    while (true) 
    { 
      // we have to handle all subscription 
      // requests in order for welcome message to work, 
      // we can just drop them afterwards
      publisher.Receive(); 
    } 
  } 
}

Client example:

using (var context = NetMQContext.Create()) 
{ 
  using (var subscriber = context.CreateSubscriberSocket()) 
  { 
    subscriber.Subscribe("WM"); 
    subscriber.Connect("tcp://localhost:5555"); 
    var welcomeMessage = subscriber.ReceiveString(); 
    Console.WriteLine(welcomeMessage); 
  } 
}

Connect attempt

So you probably been asking yourself why welcome message is useful? the main problem welcome message is solving is weather a connect attempt was successful, before this feature we have to use a heartbeat message sent from the server periodically and on the client wait for the heartbeat message, which can take up to the time of the keep alive interval.

Our reliable pubsub will have a list of available publishers and will try to connect to each until one of them return welcome message in acceptable time.

Connect to closest publisher

As I mentioned we would have a list of publishers, but what if instead of trying to connect one after the other until successful connection I will connect to all of them, take the first one to reply and close the rest? In the LAN I will probably will connect to the less busy publisher or randomly and in the WAN I will probably connect to closest publisher. We achieved geo-load-balancer without expensive hardware or DNS service.

Recognize connection drop

Welcome message has another useful behavior, ZeroMQ automatically try to reconnect a dropped connection. When welcome message is set every time a reconnect happen the publisher will send a welcome message. We can use that to check if we missed messages. With the clone pattern we can use the welcome message to request full snapshot again from the server.

We would use that behavior to create our reliable pub sub.

Heartbeat

Welcome message solves the connect attempt problem, but we still need to recognize if publisher is down after the connection has been made. So if for 5 seconds no message has been received we would try to reconnect. But what if there was no message sent for 5 seconds? To solve this problem we would send a heartbeat message from the server every 2 seconds, and now if for 5 seconds we didn’t receive any message we can know for sure the publisher is down and we should reconnect.

Code

The following code is simplified for the example in the post. You can view full source code at https://github.com/somdoron/ReliablePubSub.

Server

using (NetMQContext context = NetMQContext.Create())
{
  using (var publisherSocket = context.CreateXPublisherSocket())
  {
    publisherSocket.SetWelcomeMessage("WM");
    publisherSocket.Bind("tcp://*:6669");

    // we just drop subscriptions                     
    publisherSocket.ReceiveReady += (sender, eventArgs) => 
      publisherSocket.SkipMultipartMessage();

    Poller poller = new Poller(publisherSocket);

    // send a message every second
    NetMQTimer sendMessageTimer = new NetMQTimer(1000);
    poller.AddTimer(sendMessageTimer);
    sendMessageTimer.Elapsed += (sender, eventArgs) => 
      publisherSocket.
        SendMoreFrame("A").
        SendFrame(new Random().Next().ToString());

    // send heartbeat every two seconds
    NetMQTimer heartbeatTimer = new NetMQTimer(2000);
    poller.AddTimer(heartbeatTimer);
    heartbeatTimer.Elapsed += 
      (sender, eventArgs) => publisherSocket.SendFrame("HB");

    poller.PollTillCancelled();
  }
}

Client

Client code is a little more complicated and is split into multiple methods. Let's start with the Connect method which connect to the first socket that reply with Welcome Message.

Connect

private SubscriberSocket Connect(string[] addresses)
{
  List<SubscriberSocket> sockets = new List<SubscriberSocket>();
  Poller poller = new Poller();

  SubscriberSocket connectedSocket = null;

  // event handler to handle message from socket
  EventHandler<NetMQSocketEventArgs> handleMessage = (sender, args) =>
  {
    if (connectedSocket == null)
    {
      connectedSocket = (SubscriberSocket)args.Socket;
      poller.Cancel();
    }
  };

  // If timeout elapsed just cancel the
  // poller without setting the connected socket
  NetMQTimer timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));
  timeoutTimer.Elapsed += (sender, args) => poller.Cancel();
  poller.AddTimer(timeoutTimer);

  foreach (var address in addresses)
  {
    var socket = m_context.CreateSubscriberSocket();
    sockets.Add(socket);

    socket.ReceiveReady += handleMessage;
    poller.AddSocket(socket);

    // Subscribe to welcome message
    socket.Subscribe("WM");
    socket.Connect(address);
  }

  poller.PollTillCancelled();

  // if we have a connected socket the connection attempt succeed
  if (connectedSocket != null)
  {
    // remove the connected socket from the list
    sockets.Remove(connectedSocket);

    // close all existing sockets
    CloseSockets(sockets);

    // drop the welcome message
    connectedSocket.SkipMultipartMessage();

    // subscribe to heartbeat
    connectedSocket.Subscribe("HB");

    // subscribe to our only topic
    connectedSocket.Subscribe("A");

    connectedSocket.ReceiveReady -= handleMessage;
    connectedSocket.ReceiveReady += OnSubscriberMessage;

    return connectedSocket;
  }
  else
  {
    // close all existing sockets
    CloseSockets(sockets);

    return null;
  }
}

What we are doing?

  1. Create a timeout which will cancel the poller when timeout is elapsed.
  2. Create a handler to handle the welcome messages, first time handler will be called it will cancel the poller.
  3. Create all sockets and connect.
  4. Poll until cancelled, which will either be timeout or welcome message arrived.
  5. Close all existing connections except the one connected, subscribe to heartbeat and topics and register the handler to handle messages from now on.

Run

The run method orchestrate the entire process.

public void Run(params string[] addresses)
{
  using (m_context = NetMQContext.Create())
  {
    var subscriber = Connect(addresses);

    if (subscriber == null)
      throw new Exception("cannot connect to eny of the endpoints");

    // timeout timer, when heartbeat was not arrived for 5 seconds
    m_timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));
    m_timeoutTimer.Elapsed += (sender, args) =>
    {
      // timeout happend, first dispose existing subscriber
      subscriber.Dispose();
      m_poller.RemoveSocket(subscriber);

      // connect again
      subscriber = Connect(addresses);

      if (subscriber == null)
        throw new Exception("cannot connect to any of the endpoints");

      m_poller.AddSocket(subscriber);
    };

    m_poller = new Poller(subscriber);
    m_poller.AddTimer(m_timeoutTimer);

    m_poller.PollTillCancelled();
  }
}

What we are doing:

  1. Trying to connect to a server
  2. Create a timeout timer in order to recognize connection drops and try to reconnect
  3. Creating the poller and polling

OnSubscriberMessage

The only thing missing in the puzzle is the handle of subscriber messages, which we registered in the Connect method.

private void OnSubscriberMessage(object sender, NetMQSocketEventArgs e)
{
  var topic = e.Socket.ReceiveFrameString();

  switch (topic)
  {
    case "WM":
      // welcome message, print and reset timeout timer
      Console.WriteLine("Connection drop and reconnect monitoed");
      m_timeoutTimer.Enable = false;
      m_timeoutTimer.Enable = true;
      break;
    case "HB":
      // heartbeat, we reset timeout timer
      m_timeoutTimer.Enable = false;
      m_timeoutTimer.Enable = true;
      break;
    default:
      // its a message, reset timeout timer, notify the client, 
      // for the example we just print it
      m_timeoutTimer.Enable = false;
      m_timeoutTimer.Enable = true;
      string message = e.Socket.ReceiveFrameString();
      Console.WriteLine("Message received. Topic: {0}, Message: {1}", 
        topic, message);
      break;
  }
}

What we are doing:

  1. Get the topic of the message
  2. Check which topic it is
  • When it is welcome message we reset the timeout timer and notify the user on the connection drop
  • When it is an heart beat we only reset the timer
  • When it is a message we reset the timeout timer and notify the user

Summary

So what Reliable PubSub really does:

  • Trying to connect to multiple servers and pick the first one that reply with Welcome Message. Can be used as geo load balancer or to connect to less busy server.
  • Recognize temporary connection drop (when Welcome Message arrived while the connection is up), the user can use that to request full snapshot, take a look at the Clone pattern from zeromq guide.
  • Reconnect when the server is dead, will probably connect to the next closest server.

The Reliable PubSub is a good fit for:

  • Financial Market Data (Forex/Stock Quotes)
  • Social Stream
  • Publishing changes to a client

If you implement the pattern in another language please send it to me and I will add a link to the post.

You can find the complete example and another more complete implementation at:
https://github.com/somdoron/ReliablePubSub.

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
        if (subscriptionBytes[0] == 1 || subscriptionBytes[0] == 0)
        {
          // 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 (subscriptionBytes[0] == 1)
              {
                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();
      
      // converting to string, not most efficient but will do for our example
      message.Append(a.ToString()); 
      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()); 
    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…