Reworked netpool/tcp

Some time ago I wrote blog post about tcp socket acceptor pool which i'm developing for XMPP server and today I fully reworked it. Main data structures like a Listener, Connection still as in previous time excluding really little changes in Listener data structure. I added some API for acceptor pool, fixed some little bugs and fully changed internal structure. I think it post will be useful for newbie Golang developers who wants to play with Golang, TCP and some concurrency. So if you're interesting in it follow this post.


My thoughts about why Haskell is not popular

Despite Haskell programming language is not young language, it has a really great and helpful community, it has big amount of libraries, unfortunately Haskell is not popular programming language. I'm not against this programming language, opposite, haskell's unpopularity makes me sad. I will not write about haskell popularity in industry, I'll try to explain my thoughts about "Why Haskell is not popular" not as Haskell expert, not as professional Haskell developer (i don't get payment for Haskell programming), but from position of usual developer who started to learn/experiment with Haskell some months ago and already don't afraid monads :). I can't So I will try to explain my opinion about Why Haskell is not popular in this post.

Reader monad understanding

Some time ago I started to learn Haskell. It's not the first time (and event not second) that I started to learn this language. Every time I met different problems with understanding different abstractions like a Monads, Arrows, Monad Transformers and etc... I think that I'm not alone with this :) But, in this time, I decided not to throw up training until I can not say: yes I can write in Haskell without pain. I started (as every time i did it) with the most popular book (for newbies) about Haskell - Learn You a Haskell and stopped at Reader monad. Spending some time I understand it (at least I think that I understand :)) and decided to share my understanding of this monad for the same newbie haskellers as me.



Simple bash script for setting up Golang environment

Some time ago, when i just started to use golang, golang project's structure organizing, this magic $GOPATH and other things was strange for me. No, it wasn't bad, just nothing like i have used before. It's simple to understand with erlang, at least for the start, you must just install erlang, to create project, to add rebar with deps and that's all. Something like this with nodejs project bootstraping. There are clear and intuitive package managers which downloads project's dependencies and puts it to the project dir.

Another case with Golang && go get. They are very cool, but not for the first look. You read it's documentation, execute all instructions and than get something like go can't find `projectname` in $GOPATH and $GOROOT. So i created simple bash script which will help you to set up golang environment from the scratch. It makes 3 simple things:
  • Creates directories for Golang compiler and your future projects
  • Downloads golang compiler and builds it
  • Puts golang/bin/ and directory with your projects to $GOPATH
That's all. Very simple. Of course this script is not for a professional golang developers. It's primarily for two points:
  • For newbie gophers
  • For fast setting up golang environment
Here is the script:


Just run this script and pass directory path where will be golang and projects, and your github username. It will create directories for golang and you project. After it you can start to coding.

That's all. If you will have any questions/suggestions/problems with this script write me a comment.

Happy coding :)

Implementing tcp socket acceptor pool with golang

So, as maybe some of you knows that i have started to use Golang programming language for my pet projects and since to solve "Project Euler" tasks is very useful deal, but very boring in the same time, i decided to create something more closer to real life. I decided to write XMPP server in golang and in this post i will tell about one part of this server - netpool/tcp library. netpool/tcp - is a tcp based socket acceptor pool. The idea is very simple, you will launch TCP listener which will launch many acceptors besides one, in this way you no need to spend time for creating new connection acceptor.

First of all let's create new tcp package and import all libraries which will we need in this project:

Now let's define types which we will need in our acceptor pool:

Let's go by order. First enumeration defines strategies when connection listener handles more connections than we define.

  • RefuseConnection - listener will refuse connections after connections amount overflow;
  • IncreaseConnection - listener will start yet another 50 acceptors.
Next is simpe constant value which defines how many acceptors need to start after connections amount overflow.

Next is ListenerStruct structure. It is a main structure which we will pass to StartListener function for launching new listener. It has following fields:

  • Accnb - number of acceptors;
  • Port - connection port;
  • Handler - function with 2 parameters: input string and conn tcp.Connection. It will calls when client send something to server;
  • Lc - listener channel, it needs for communicating with listener;
  • OverFlowStrategy - It can be 0 || 1, see note about overflow strategies;
  • Ssl - it is a map, we need to use it if we want to use ssl. Just create map with 2 string keys: pam and key. Next if will show how to use it.
Also it has API which consists now only from one function - StopListener without any arguments, you can stop current connection with it.

Next is a Connection structure. We have it every time as second parameter in connection handler function. It has simple API for manipulating with connection:

  • Connection.Close - close current connection;
  • Connection.Send  - send response.
So, now web have all types which we will use in this library. Now proceed to implementation. All API of netpool/tcp consists only from one function:

It's a main function of this library and it launches new tcp listener and some acceptors. Let's see how it implemented.

First of all it defines some variables like: acceptorCounter for count acceptors, checks is it connection over SSL or not and launches listener which depends on this:

Next it creates 2 channels:

  • connectionCounter - channel for communicating between acceptor goroutines and listener. If connection was closed it sends message to listener and listener decrease connection numbers, if new connection was accepted it increase connection number;
  • closeConnection - channel for commuicating between listener and acceptor for closing current connection.
and starts acceptors:

After this listener waits for messages from another goroutines for closing current listener, closing connection, connection removed/created and etc...:

So what about acceptors... It's pretty simple as a listener implementation, it starts from accepting new connection and waits for incoming messages from connected client. If there are any incoming data from client it call handler function and pass input data and  Connection structure to it:

After this it as a listener checks incoming message from other goroutines, to stop connection and etc...

So it's end. As we can saw concurrency is a pretty simple with Golang.

As i said in previous post i'm not a golang super-hacker, if you will have any recommendations for my golang code or you want to take part in XMPP server developing with golang i will be very pleased to hear it, write me in comment or ping me in twitter.. Also i made this library primarily for future extd XMPP server but i tried to make it generic so i think it can be useful not only for me. Links: