Mangos is a pure Go implementation of Scalable Protocolos, there is a good introduction about it, in this blog post.
Also, that blog gave me the idea to implement a simple way to do service discovery. The common solution for service discovery is using some kind of distribuited system to store the changes, the usual solutions for this problems are Zookeeper, etcd, consul, eureka.
That’s usually pretty good, but sometimes you don’t need that kind of solution to solve a simple problem. You may not have many servers and using a library could be enough to solve that problem.
So I released a small library to do service discovery based in Mangos, I called:
It uses one socket for emiting a survey heartbeat.
With the heartbeat from the server you can know the clients that are online, and whenever a client goes down, you’ll know it in the next heartbeat slot.
But you need to notify in some way to the interested parties that new clients are connected or disconnected. And for that I use mangos pub/sub socket to publish the changes.
You can have clients that are aware of the changes in other peers, or you can have a pure subscriber that only receives the changes.
A good use case is for example, to update the peers in groupcache
We can have a very light server for service discovery:
urlServer := "tcp://10.0.0.100:40007"
urlPubSub := "tcp://10.0.0.100:50007"
// on the server
server, err := gopherdiscovery.Server(urlServer, urlPubSub, opts)
server.Wait()
And for every single client, use groupcache in combination with gopherdiscovery to update the peers that are doing the cache.
var peers chan []string
urlServer := "tcp://10.0.0.100:40007"
urlPubSub := "tcp://10.0.0.100:50007"
me := "http://10.0.0.1:8080"
// any of the peers
poolGroupCache := groupcache.NewHTTPPool(me)
client, err := gopherdiscovery.ClientWithSub(urlServer, urlPubSub, me)
peers, err = client.Peers()
for nodes := ranges peers {
// every time there is a change in the peers -> Set in groupcache
poolGroupCache.Set(nodes...)
}
Another example could be, adding and removing proxies from a loadbalancer.
If you like it, check the source and the docs, and any feedback is welcome!