Learning Go: expvar in RPC server

March 26, 2012

It is great to build RPC upon HTTP, so we can use Web browsers to test/debug a RPC server.  If an RPC server is built using Go’s RPC package and is over HTTP, we can check its states (how many times every method has been invoked) by following the URL: http://<ip&gt;:<port>/debug/rpc.

This is good, but not enough in many cases.  We might want to add more counters than those for method invocations.  We might also want browse-able variables other than counters of integer type, including float, string and map.  In Go, these can be handled by the expvar package, which export variables values through HTTP.  If an RPC server (is also an HTTP server)  have the expvar package linked (imported), we would be able to following http://<ip&gt;:<port>/debug/vars to check exported variables’ values.

An example RPC server follows:

package main

import (
 "fmt"
 "net/rpc"
 "net/http"
 "expvar"
)

var CountPageFeatureRequest expvar.Int

type ExtractPageFeaturesRequest struct {
 PageURL string
 Title string
 Body string
}

type ExtractPageFeaturesResponse struct {
 TitleKeywords []string
 BodyKeywords []string
}

type PageFeatureService int

func (pageServer *PageFeatureService) ExtractPageFeatures(
 args *ExtractPageFeaturesRequest,
 reply *ExtractPageFeaturesResponse) error {
 reply.TitleKeywords = []string{"apple", "orange"}
 reply.BodyKeywords = nil
 CountPageFeatureRequest.Add(1)
 return nil
}

func main() {
 CountPageFeatureRequest := new(expvar.Int)
 expvar.Publish("CountPageFeatureRequest", CountPageFeatureRequest)

pageServer := new(PageFeatureService)
 rpc.Register(pageServer)
 rpc.HandleHTTP()

if err := http.ListenAndServe(":1234", nil); err != nil {
 fmt.Println(err.Error())
 }
}

Xcode 4.3 Command Line Tools

March 26, 2012

After installing new Xcode 4.3, I found that common tools including gcc and g++ are lost.  That is because Xcode does not install these tools into standard locations (/usr/bin nor /usr/local/bin).  But the fix is simple:

  1. Install “Command Line Tools” in Xcode : Preferences > Downloads > Command Line Tools > Install
  2. Download and install command line tools from https://developer.apple.com/. You need to login but it is free.  And you do not need to install Xcode.

Another looks-good solution is

export PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH

But it does not work completely. For example, you might suffer from the difficulties including cannot find system header files.


Balanced Binary Search Tree in Go

March 11, 2012

Treap is the balanced binary search tree published by StatHat: http://www.stathat.com/src/treap.


Follow

Get every new post delivered to your Inbox.

Join 29 other followers