Note: This is an experimental site, if you have a feed you would like to add or you would like your feed to be removed please contact me.

Planet 5

January 23, 2012

Miek Gieben

Super-short guide to getting q

Get the latest version (called weekly) of Go:

  1. Get Go: hg clone -u release https://go.googlecode.com/hg/ go Note the directory you have downloaded it to and set $GOROOT to it: export GOROOT=$PWD/go. Add the GOROOT bin directory to your path: PATH=$PATH:$GOROOT/bin

  2. Update Go to the latest weekly: cd $GOROOT; hg pull; hg update weekly

  3. Compile Go: cd $GOROOT/src ; ./all.bash

    Install missing commands (gcc, sed, bison, etc.) if needed.

The latest Go is now installed.

Install GoDNS

  1. Get GoDNS: cd ~; git clone git://github.com/miekg/godns.git
  2. Compile it: cd godns; make ; make install
  3. Compile the examples; cd examples; make ; make install
  4. Query with q: q mx miek.nl
  5. Report bugs

by Miek Gieben at January 23, 2012 05:07 PM

January 19, 2012

research!rsc

Regular Expression Article #4

In January 2007 I posted an article on my web site titled “Regular Expression Matching Can Be Simple And Fast.” I intended this to be the first of three; the second would explain how to do submatching using automata, and the third would explain how to make a really fast DFA. These were inspired by my work on Google Code Search.

Today, the fourth article in my three-part series is available, accompanied by source code (as usual). This one describes how Code Search worked.

by rsc (noreply@blogger.com) at January 19, 2012 05:24 PM

RSC

_rsc: Miss Code Search? https://t.co/5LLDCqVv

_rsc: Miss Code Search? https://t.co/5LLDCqVv

January 19, 2012 04:04 PM

January 15, 2012

Adam Langley

BEAST followup

(See the original post for background.)

Everyone seems to have settled on 1/n-1 record splitting as a workaround for the BEAST attack in TLS 1.0 and SSLv3. Briefly: 1/n-1 record splitting breaks CBC encrypted records in two: the first with only a single byte of application data and the second with the rest. This effectively randomises the IV and stops the attack.

The workaround which OpenSSL tried many years ago, and which hit significant issues, was 0/n record splitting. It's the same thing, but with the first record being empty. The problem with it was that many stacks processed the empty record and returned a 0-byte read, which higher levels took to mean EOF.

1/n-1 record splitting doesn't hit that problem, but it turns out that there's a fair amount of code out there that assumes that the entire HTTP request comes in a single read. The single byte record breaks that.

We first implemented 1/n-1 record splitting in Chrome 15 but backed off after only a couple of days because logging into several large sites broke. But that did motivate the sites to fix things so that we could switch it on in Chrome 16 and it stuck that time.

Opera also implemented it around this time, but I think Chrome took the brunt of the bug reports and it's time consuming dealing with them. Myself and a colleague have been emailing and phoning a lot of sites and vendors while dealing with upset users and site admins. Chrome certainly paid a price for moving before Firefox and IE but then we're nice like that.

Thankfully, this week, Microsoft released a security update which implements 1/n-1 record splitting in SChannel and switches it on in IE. (Although it defaults to off for other users of SChannel, unlike NSS.) Now the sites which broke with Chrome 16 are also broken in a patched IE and that takes some pressure off us. In a few weeks, Firefox 10 should be released and then we'll be about as good as we're going to get.

After taking the brunt with Chrome 16, there is one case that I'm not going to fight: Plesk can't handle POST payloads that don't come in a single read. Chrome (currently) sends POSTs as two writes: one for the HTTP headers and a second for the POST body. That means that each write is split into two records and Plesk breaks because of the second split. IE and Firefox send the headers and body in a single write, so there's only a single split in the HTTP headers, which Plesk handles.

Chrome will start merging small POST bodies into the headers with Chrome 17 (hopefully) and this will fix Plesk. Also, merging as Firefox and IE do saves an extra packet so it's worthwhile on its own. Once again, anything that's mostly true soon becomes an unwritten rule on the Internet.

It's worth contrasting the BEAST response to the renegotiation attack. The BEAST workaround caused a number of problems, but it worked fine for the vast majority of sites. The renegotiation fix requires that very nearly every HTTPS site on the Internet be updated and then that browsers refuse to talk to unpatched servers.

I'd bet that we'll not manage to get enough patched servers for any browser to require it this side of 2020. Unpatched servers can still disable renegotiation to protect themselves, but it's still not hard to find major sites that allow insecure renegotiation (www.chase.com was literally the second site that I tried).

January 15, 2012 08:00 AM

January 14, 2012

Adam Langley

OTR in Go

“Off the record” is, unfortunately, an overloaded term. To many it's feature in gTalk and AOL IM which indicates that the conversation isn't logged. However, to crypto folks it's a protocol for secure chat.

(In fact, resoloving the ambiguity is on the EFF's wish list.)

Pidgin has been my chat client of choice for some time because it's pretty fully featured and supports OTR via a plugin. However, I just don't trust it from a security point of view. The latest incident was only a couple of weeks ago: CVE-2011-3919.

So, I implemented otr in Go, as well as an XMPP library and client. It's an absolutely minimal client (except for OTR support) and implements only what I absolutely need in a client.

But it does mean that the whole stack, including the TLS library, is implemented in a memory safe language. (On the other hand, pretty much everything in that stack, from the modexp function to the terminal handling code was written by me and has never really been audited. I'm a decent programmer but I'm sure there are some howlers of security issues in there somewhere.)

January 14, 2012 08:00 AM

January 11, 2012

Kyle Lemons

Quote: Gentleness and Strength

“Nothing is so strong as gentleness, nothing so gentle as real strength”
- St. Francis de Sales

by Kyle Lemons at January 11, 2012 10:16 PM

January 06, 2012

Kyle Lemons

Go: A New Language for a New Year

Go Gopher Logo

Go Gopher

<style>p{text-indent:1em}</style>

As 2011 makes its way out and 2012 takes its place, it’s time for a bit of reflection and a bit of looking forward. I haven’t been writing software professionally for particularly long, but I have written software in a number of languages (everything from Pascal to Python), and I think we can all agree that none of the usual suspects are particularly ideal. If you’re like me, you hunker down with your favorite set of language features and write your code in as much isolation as possible, so that you can work around or ignore whatever problems your language and/or environment cause you. You probably have some tools lying around to help you do various things for which the language or your editor/environment aren’t well-equipped. You probably don’t even realize all of the things that bother you about the language after awhile, because it’s the norm: every programmer has to deal with them, it just comes with the territory. Please note: I will be comparing Go to other languages extensively in this blog post; do not take it to be an indictment of them or even, really, as reasons to not use them. I’m simply giving my opinion on their differences and why I personally find that Go is a more suitable language for my development. I have used all of the languages that I discuss and will continue to use them when their particular strengths are required.

Let’s start with Python. I loved my time writing Python, because I felt like I could be really productive. I created a library that my team and I could use to replace huge pieces of boilerplate with a few lines of very terse but legible code. I used just about every new, shiny feature of Python on which I could get my grubby little hands: I used context objects, generators, fancy inheritance and especially I abused the data model in ways that now make me cringe. My code was a pleasure to read and even more of a pleasure to write. It got the job done with a really small amount of code, and my team and managers loved me for how productive I was and how productive I was making my team. What I knew somewhere in the back of my mind but didn’t tell anyone was how horribly painful it was to debug the code written this way. My code reviewers were able to read the code and admit that it looked right, see that the test cases were comprehensive enough and verify that I was covering all of the requirements, but I doubt that they had any idea what was really going on behind-the-scenes or how brittle some of the libraries could be. Since I had written one of the libraries myself and knew most of the nuances of the Python features employed, I was able to debug problems with the library (or my use of it) with relative ease. Unfortunately, it often meant tracing through many layers and scrutinizing pieces of code with a fine-toothed comb to find all of the places where Python was secretly calling (or not calling) out to other pieces of code. Often, it was simply a matter of hiding yet another __special__ function to an object to handle a new use case (for those of you familiar with C++ and the Rule of 3, there needs to be a Python Rule of 17 or something…) that wasn’t already covered. The rest of the team pretty much just used my example code as a template and never ran into these issues, but if they’re still using this particular library I really hope that it’s not causing them more problems than it solved. I’m not even going to get into the various issues that flimsy duck-typing and non-static typing can cause, often a long time after the offending code is written… What remains clear to me is that Python (and langauges like it) are not suitable for large-scale development without very strict adherence to a style guide which precludes almost all of what I described above, and to me that would just take all of the fun out of it.

That, however, is Python, and is the price you expect to pay for such a dynamic language. Even lower-level languages like C and Pascal have their issues. Don’t get me wrong, I absolutely love C and feel like I am a deus intra machinam whenever I write it; I still think it’s the first programming language that a student should learn, and that they shouldn’t be able to move to a higher- or lower-level language until they’ve mastered it. The problem with these languages, especially C, is that you have to take such care when writing and reviewing any significant amount of code that verifying that the code is correct, robust, and has no memory leaks is the next closest thing to impossible. The boilerplate and the bookkeeping that have to be done before you even get to the meat of your application or algorithm are tedious and error-prone in the worst way. There are certainly situations in which such lower-level languages are the only language that can solve your problem, whether it be for performance, real-time/deterministic execution or bare metal proximity reasons, but they just aren’t the most suitable languages for large-scale development.

I’ve also done a bit of graphical (read: drag-and-drop, flow chart) programming, which is interesting to say the least. I won’t go into much detail here, except to say that from my limited exposure, it seems best suited as a special-purpose tool for small- to medium-scale logic at best, and should probably not be used when there are more than a few developers on a particular piece of “code.” It’s fun at first, but the limitations show up fast, and after awhile it seems more tedious, repetitive, and limiting than anything else.

Go Gopher Understudy from Squishable

Go even has an adorable mascot

Enter Go. It is a new programming language by pretty much any definition, despite having been in development at Google since 2007 and as an open source project since 2009. It’s a compiled, statically typed, garbage collected language with some cool primitives for concurrency. Despite being compiled and statically typed, it has a very dynamic feel. The syntax has some concise shortcuts to minimize repetition and variable typing. The presence of closures, a garbage collector, and a fairly expansive standard library also contribute to its dynamic feel. These things, coupled with the clarity and explicit nature of Go source code, all help make Go one of the best general purpose languages that I have ever used. I won’t be providing code directly (this blog post is long enough already), but many of the links I provide will be to the documentation or to the relevant portions of the Go Tour, where you can play with the features yourself.

We look forward to the first long-term stable release of the Go standard library and compilers sometime early this year: Go version 1. I’ve been convinced for awhile now that Go is a production-ready language, but in my mind this piece is really the last thing left barring wider adoption. There are some exciting language changes leading up to this Go 1 release, and I am really excited to see what gets done after it is finished. To date, the fast pace at which Go has evolved (both the language and the standard libraries) has posed a challenge for third-party library maintainers, who often have to maintain both a “release” branch of their code and a “weekly” branch due to the rate at which the weekly branch progresses and the relatively high percentage of Go users who are doing their development at (or beyond) the “weekly” branch to take advantage of the new, shiny features or to get the latest bug fixes. With Go 1 this should change, and the number of libraries available to use with a single command (more on that later) will be able to grow even faster.

Why Go?

Compared to most languages in use today, Go was designed with many programmers, concurrent systems, and large-scale problems in mind. Some languages, like C, were simply not designed at a time where multicore and multiprocessor systems were in broad deployment. Some languages, like Python, weren’t originally intended to scale well onto multiple cores and have been slow to adopt strategies for utilizing such resources. Many languages to this day don’t have networking libraries which work predictably and have a clean, understandable API. Programs written in many languages, especially those with operator overloading, require in-depth knowledge of both the architecture of the program itself and the libraries in use in order to be able to understand the code well. Go attempts to address all of these problems and more; in my opinion, it does so brilliantly.

Two of the first things you will hear most people talk about when you hear about Go are channels and goroutines. They’re certainly two of the “shiniest” features of the language, and are the reasons why I tried out Go back in late 2009. They’re far from the biggest reasons why I think you should try Go, but they’re as good a place as any to start.

Channels and Goroutines

These two features are inspired by Hoare’s Communicating Sequential Processes (CSP). A goroutine (similar to a coroutine) is a function that runs independently of the calling function. They’re similar to threads in that a scheduler manages which ones are running at any given time, but they are much lighter-weight than their operating system counterparts. In fact, a Go application with many thousands of goroutines can run comfortably on a single operating system thread.

Channels are a data type in Go which allows for simple, clear communication between goroutines. They are first-class values that have a data type associated with them. Any value of the associated type can be sent over the channel and then received from the channel, usually by another goroutine. They can be passed into functions and goroutines, stored in objects, even sent through other channels.

In most programming languages, when you have multiple threads that need to share data or communicate, you arrange to do so via the use of mutual exclusion locks (mutexes). At first glance, it seems simple: before you access the data you lock it, and when you’re done you unlock it. It gets more complicated very quickly, of course. In Go, a mutex (while available) is not the preferred solution to most problems. As stated in Effective Go, the slogan has become:
Do not communicate by sharing memory; instead, share memory by communicating.Thus instead of using a mutex to protect a shared, global object, you either pass the object around (changing ownership as you do) or you maintain a single owner and all other goroutines access the object by sending requests to that goroutine.

Using these two tools, it turns out to be quite easy to implement solutions to a problem that make logical sense and that very closely reflect the way we might solve the problem in our head. Small, focused pieces of code are concerned with specific aspects of the solution, and are either given their input via the usual way (function parameters) or continually from channels (they also have the same alternatives available for their output). Ownership of data is clear, communication is explicit, the code is highly legible. Such designs are also often easy to unit test; simulating the input and verifying the output via channels is straightforward and very common.

Closures and Deferred Functions

One of the many features of Go that makes it have a very dynamic feel are closures. Like lambdas in python or blocks in ruby, a function can be declared as a local variable or even passed as a literal to a function. All local variables in scope at the closure declaration are also in scope within the closure. They’re powerful tools for functional-style APIs and can be a really fun alternative to (or improvement upon) callbacks. They are also commonly executed directly as a goroutine.

In addition to the “go” directive that runs a function in its own goroutine, Go also has a “defer” directive. When a function (or closure) call is preceded by the “defer,”, the call is evaluated but not executed until just before the function returns. Multiple deferred calls will be executed in reverse order. This turns out to be a very powerful tool for using any object or API which requires cleanup: as soon as you create it or lock it, you can defer the cleanup or unlock. This makes it very easy to spot when you forgot to close a file descriptor or (should you find yourself using one) unlocking a mutex.

These two features provide the ability to confine relevant code all in the same place, which in turn makes reading, debugging, and maintaining the code easier. This is a common theme, and I think was at or near the top of the Go designers’ priority list when designing the language. The concepts are simple, orthogonal, and flexible without sacrificing readability, debuggability, or maintainability.

Interfaces

Interfaces in Go are a welcome twist on a familiar concept. Typically, in a language like Java, interfaces must be explicitly declared by any implementing class. This, of course, means that adding a new interface requires modifying (and having access to modify) all classes that you plan to use in values of that interface type. The paradigm in Go is reversed: instead of defining an interface and declaring all of the objects that satisfy it, the interface is defined and any object which could satisfy the interface can implicitly be used as a value of the interface. This also provides a useful tool, the empty interface, which acts as a container for any possible value (similar to Java’s Object, from which all classes are derived) because all values satisfy it.

In contrast to interfaces in other languages (for instance, in Java), Go interfaces are often very small, and contain one or just a few methods. Because of the presence of such small interfaces in the standard library, they have naturally given rise to some common, idiomatic function names and signatures. Possibly the best examples of these from the standard library are the io.Reader and io.Writer interfaces. Because of the prevalence of objects which implement these two interfaces, stringing together producers and consumers of a data stream is often reminiscent of a unix pipeline. It is simple, for instance, to open a file, unzip it, decrypt its contents, and stream it directly to an HTTP client because all of these interfaces satisfy or consume the above interfaces. Even Go’s version of the old favorite printf can write to any object which satisfies the io.Writer interface, which gives rise to the very concise web-based version of “Hello, World!” in Go.

Simplicity and Explicitness

Some of the best features of Go are, unlike those listed above, not something you’ll find listed in the specification. They probably stem from the guiding philosophy behind its design more than anything else. I think that the simplicity, orthogonality, and explicitness of the features of the Go language make it a prime candidate for a teaching language, especially at the middle- or high-school level.

First, the features of Go are simple and minimal. The entire language specification fits in 45 printed pages. By contrast, the C++ specification clocks in at 750 pages and Javascript has nearly 250. It is short enough that an average programmer can actually read it, understand it, and internalize it in its entirety. This turns out to be quite an asset in a large project setting with multiple developers, each of whom will have a slightly different coding style and may utilize a different different set of features. Being able to understand all of the language features in use, their nuances, and their side effects turns out to be easy in Go when it could be a nearly impossible task in many others.

Second, the features of Go are designed to be orthogonal to one another. When you understand two concepts independently, you understand them together. I have already given one example of this: deferred function calls. If you understand defer (e.g. that it doesn’t make the call immediately, that it evaluates arguments at the defer site, and that it is executed before any subsequent defer calls as the function returns) and you understand closures (that any local variables are in scope, etc), you already understand what happens if you defer a call to a function literal. Compare this to my Python anecdotes earlier, and you’ll find that this is not as ubiquitous a quality as you might expect. There are also a fair number of C++ and Java features that interact non-orthogonally and require some more in-depth understanding (hence the 750-page C++ specification).

Ease of Development

This one is more difficult to quantify. I have found, qualitatively, that writing Go is easier and more fun than pretty much any other language I have used to date. Dynamic languages like Python and Ruby are fun to learn and are fun languages with which to experiment, but I have found that the amount of time spent debugging them (especially when it’s somebody else’s code) takes almost all of the fun out of large project and multi-developer situations. By contrast, languages like C and C++ (and to a somewhat lesser extent, Java) are more painful to write up front but have well-established tools that make analyzing and debugging them a much more pleasant experience. Until I tried Go, I accepted this as the nature of the beast. You just had to to choose. With Go, however, I think the designers managed to strike a balance.

On the development side, I have found that it is easy to translate designs into code. As I mentioned previously, channels and goroutines make it easy to implement your design in pieces that all connect together and communicate. The lack of verbose syntaxes for declarations and the implicit nature of interfaces reduce the amount of arguably unnecessary up-front work. The simplicity of the type system lets you (or forces you to) focus on the implementation instead of fixating on design patterns or type hierarchies. The lack of certain features in the language like operator overloading, destructors, exceptions, and the like all force code to be explicit. An oft-quoted mailing list post by Andrew Gerrand states that “[in] Go, the code does exactly what it says on the page.” Error handling, function calls, math, map/slice access and inter-goroutine communication all look exactly like what they are. When examining a function in isolation, it is a relatively small feat to understand the side-effects of a given statement, which makes the debugging process much more pleasant than in some other languages in which the simplest statement like “a+b” could potentially have unlimited side effects.

Outside of the code itself, there are many benefits to the Go programming language. The standard library is very complete, cohesive, and easy to understand. The third party package ecosystem is nurtured by the Go team, and the standard distribution includes an application called “goinstall” (soon to be bundled into a “go” meta-command) by means of which a package hosted in any web-accessible Git, Mercurial, or Subversion repository can be installed (and any such dependencies, ad infinitum) with a single command. By default, such installations are anonymized and aggregated on a package dashboard, which shows the most- and most-recently installed packages as well as their build status as of the latest release. The standard distribution also includes a formatting tool called “gofmt” which knows how to format a piece of source code according to “Go style,” which reduces or eliminates the need for unproductive debating within a project about things like brace placement or whitespace around operators. There is also a tool called “gofix” which contains modules to automatically fix up any mechanical changes that have been made to the standard library or language between releases (for instance, changing function names, package imports or method signatures) where possible. The last of the tools that I will mention is “godoc,” which is similar to pydoc or javadoc. It uses the comments in the source to provide easily accessible documentation both on the command-line and from a web browser. All of these tools add up to make even the experience of development that doesn’t involve code production as easy as possible.

Summary

Go is an industrial-strength programming language for solving industrial-sized problems. There are many features that I haven’t even touched upon, like the fact that Go is now the third runtime on Google App Engine, which could probably fill another equally long blog post. If you read this far, I hope you at least take the time to take the Go tour or peruse the language specification. Keep an eye out for future projects or tools where you might be able to try out a new language. You may not like it, it may not be accepted in your place of work, or it may simply not be adequate for your needs, but I think that it has the potential to make significant waves in the software industry and I encourage everyone (even if you’ve never programmed before in your life) to give it a shot.

by Kyle Lemons at January 06, 2012 07:30 PM

January 01, 2012

Command Center

Esmerelda's Imagination

An actress acquaintance of mine—let's call her Esmerelda—once said, "I can't imagine being anything except an actress." To which the retort was given, "You can't be much of an actress then, can you?"

I was reminded of this exchange when someone said to me about Go, "I can't imagine programming in a language that doesn't have generics." My retort, unspoken this time, was, "You can't be much of a programmer, then, can you?"

This is not an essay about generics (which are a fine thing and may arrive in Go one day, or may not) but about imagination, or at least what passes for imagination among computer programmers: complaint. A friend observed that the definitive modern pastime is to complain on line. For the complainers, it's fun, for the recipients of the complaint it can be dispiriting. As a recipient, I am pushing back—by complaining, of course.

Not so long ago, a programmer was someone who programs, but that seems to be the last thing programmers do nowadays. Today, the definition of a programmer is someone who complains unless the problem being solved has already been solved and whose solution can be expressed in a single line of code. (From the point of view of a language designer, this reduces to a corollary of language success: every program must be reducible to single line of code or your language sucks. The lessons of APL have been lost.)

A different, more liberal definition might be that a programmer is someone who approaches every problem exactly the same way and complains about the tools if the approach is unsuccessful.

For the programmer population, the modern pastime demands that if one is required to program, or at least to think while programming, one blogs/tweets/rants instead. I have seen people write thousands of words of on-line vituperation that problem X requires a few extra keystrokes than it might otherwise, missing the irony that had they spent those words on programming, they could have solved the problem many times over with the saved keystrokes. But, of course, that would be programming.

Two years ago Go went public. This year, Dart was announced. Both came from Google but from different teams with different goals; they have little in common. Yet I was struck by a property of the criticisms of Dart in the first few days: by doing a global substitution of "Go" for "Dart", many of the early complaints about Go would have fit right into the stream of Dart invective. It was unnecessary to try Go or Dart before commenting publicly on them; in fact, it was important not to (for one thing, trying them would require programming). The criticisms were loud and vociferous but irrelevant because they weren't about the languages at all. They were just a standard reaction to something new, empty of meaning, the result of a modern programmer's need to complain about everything different. Complaints are infinitely recyclable. ("I can't imagine programming in a language without XXX.") After all, they have a low quality standard: they need not be checked by a compiler.

A while after Go launched, the criticisms changed tenor somewhat. Some people had actually tried it, but there were still many complainers, including the one quoted above. The problem now was that imagination had failed: Go is a language for writing Go programs, not Java programs or Haskell programs or any other language's programs. You need to think a different way to write good Go programs. But that takes time and effort, more than most will invest. So the usual story is to translate one program from another language into Go and see how it turns out. But translation misses idiom. A first attempt to write, for example, some Java construct in Go will likely fail, while a different Go-specific approach might succeed and illuminate. After 10 years of Java programming and 10 minutes of Go programming, any comparison of the language's capabilities is unlikely to generate insight, yet here come the results, because that's a modern programmer's job.

It's not all bad, of course. Two years on, Go has lots of people who've spent the time to learn how it's meant to be used, and for many willing to invest such time the results have been worthwhile. It takes time and imagination and programming to learn how to use any language well, but it can be time well spent. The growing Go community has generated lots of great software and has given me hope, hope that there may still be actual programmers out there.

However, I still see far too much ill-informed commentary about Go on the web, so for my own protection I will start 2012 with a resolution:

I resolve to recognize that a complaint reveals more about the complainer than the complained-about. Authority is won not by rants but by experience and insight, which require practice and imagination. And maybe some programming.

by rob (noreply@blogger.com) at January 01, 2012 07:17 PM

December 31, 2011

codegrunt.co.uk

New Year

It’s been a long time since I’ve updated this blog, and a completely crazy past few months, so I hope to rectify the former by using the latter as the basis for this post :)

I turned 30 this September, and the force of switching from the deferring-friendly 20’s (‘I can achieve these things, don’t worry, I’m still young’) to the ‘oh shit I’m an adult now’ 30’s has hit harder than I expected (in fact, to be honest, I didn’t expect it at all).

So what’s happened? Well in my personal life, I finally did something about my weight issue and lost ~3.5 stone (~50lb, ~23kg).

In my intellectual/geek life I committed to the open Stanford AI course and completed it with a 98.7% average*, and wrote notes for the purposes of actual applying the course to real things (the notes are not yet fully complete, however I do plan to finish that later), which has really helped with my recently very low intellectual confidence.

Two fundamental things have changed - firstly I’ve got organised about things (for weight loss - food diary, for study - committed, realistic work schedule), secondly and far more importantly, I’ve stopped beating myself up quite as badly about everything. Beating yourself up like that makes you incapable of doing anything since you feel constantly bad about whatever it is you’re doing, and thus reticent to do it, which means you don’t improve/change anything, which means you beat yourself up more, etc. - a vicious, vicious cycle.

Another thing to note here is the amazing usefulness of timeboxing. The past week I have got more hacking done in a week than I have for hundreds of weeks prior.

So what about resolutions? Over the coming year I plan to contribute considerably more to go, and build weak into a fully-fledged chess engine (though I can make no guarantees to its eventual strength) and I intend to work on improving my algorithmic and computer science fundamentals knowledge. On the final bit - I plan to enter the google code jam and blog about the experience, though I don’t expect or predict any sort of performance, it’s more of a motivation and point of focus.

Happy New Year!

* I’m not boasting here. I fell considerably below the performance of many other participants and wish I’d been more careful with many of the answers. Also I realised how rusty I am at this stuff. Also I couldn’t stand to carry on attending the meetup I was attending given how erudite, intelligent and informed the other people were compared to me.

December 31, 2011 12:00 AM

December 27, 2011

Airs – Ian Lance Taylor

Non-free Services

As both of my faithful readers can see, my blog postings have dropped significantly. I’ve been posting my random little comments on Google+ instead.

Which leads me to the following. There is a hard-core group of people who only use free software. I’m not quite that hard-core, but in practice I do use only free software, except perhaps for some binary drivers in the kernel (I don’t actually know whether the systems I’m running use binary drivers or not, and I’m not hard-core enough to find out).

I’ve seen some people argue that if you are serious about using free software, you should also only use Internet services which are themselves free software. For example, you should not use Facebook or Google+, because the software used to run those services is not free.

I don’t agree with that argument. The key goal of free software is that I always have the right to change the software that I am running. When I use an Internet service like Google+, I am not running the software. Even if I had a copy of the software, I would not be able to run it, because I don’t have enough servers. And even if I had enough servers, it would be useless for me to run the software, because I don’t have the data. And there is no way to grant me access to the data, because that would violate the reasonable privacy choices of everybody else using the service.

When it comes to a service like Google+, whether the software is free is not important. Releasing the software would not give me any more freedom than I already have. Google+ is only interesting when many people are operating out of a single shared data base, and that data base must have privacy safeguards to ensure that it is not copied.

What matters with Google+ is not the software, but the data. It is important that I be able to retrieve all my data associated with Google+, and that I be able to retrieve it in a way that makes it possible to use with other software. That is, I should be able to retrieve my posts, my comments on other people’s posts, my list of followers, my photos, etc. And I should be able to plug them into some other software service if I so choose.

In fact Google+ does have a set of APIs which permit me to retrieve my data. I haven’t verified that all Google+ data is available via the APIs, but all the obvious stuff seems to be available. Given those APIs, it should be possible for me to move all my data to some other service which provides te required APIs itself.

So I personally don’t see any reason why even a hard-core free software supporter should avoid using a service like Google+. This isn’t to say that it wouldn’t be nice if Google freed up the software and accepted patches from outside users. It’s just that that is not a critical part of freedom to use software.


by Ian Lance Taylor at December 27, 2011 05:54 PM

December 21, 2011

Go's official blog

Getting to know the Go community

Over the past couple of years Go has attracted a lot of users and contributors, and I've had a great time meeting and talking with many of you. However, for every Gopher I know there are dozens I know nothing about. In order to address this imbalance I've prepared a survey for Go users everywhere.

The survey is short. It asks about you, your involvement with Go, and and your interest in Go-related events. Among other things, this data will help myself and the rest of the Go team plan future Go events and schedule conference appearances.

Please take a minute to complete the survey now.

Thanks!
Andrew

by Andrew Gerrand (noreply@blogger.com) at December 21, 2011 11:55 PM

December 19, 2011

Go's official blog

Building StatHat with Go

My name is Patrick Crosby and I'm the founder of a company called Numerotron.We recently released StatHat. This postis about why we chose to develop StatHat in Go,including details about how we are using Go.

StatHat is a tool to track statistics andevents in your code. Everyone from HTML designers to backend engineers can useStatHat easily, as it supports sending stats from HTML, JavaScript, Go, andtwelve other languages.

You send your numbers to StatHat; it generates beautiful, fully-embeddablegraphs of your data. StatHat will alert you when specified triggers occur,send you daily email reports, and much more. So instead of spending timewriting tracking or reporting tools for your application, you canconcentrate on the code. While you do the real work, StatHat remainsintensely vigilant, like an eagle in its mountaintop nest, or a babysitteron meth.

Here's an example of a StatHat graph of the temperature in NYC, Chicago,and San Francisco:


(click to enlarge)

Architecture Overview

StatHat consists of two main services: incoming statistic/event API callsand the web application for viewing and analyzing stats. We wanted to keepthese as separate as possible to isolate the data collection from the datainteraction. We did this for many reasons, but one major reason is thatwe anticipate handling a ton of automated incoming API HTTP requests andwould thus have different optimization strategies for the API service thana web application interacting with humans.

The web application service is multi-tiered. The web server processes allrequests and sends them to an interactor layer. For simple tasks, theinteractor will handle generating any necessary data. For complex tasks,the interactor relies on multiple application servers to handle tasks likegenerating graphs or analyzing data sets. After the interactor isfinished, the web server sends the result to a presenter. The presenterresponds to the HTTP request with either HTML or JSON. We can horizontallyscale the web, API, application servers, and databases as the demand forservices grows and changes over time. There is no single point of failureas each application server has multiple copies running. The interactorlayer allows us to have different interfaces to the system: http, commandline, automated tests, mobile API. StatHat uses MySQL for data storage.

Choosing Go

When we designed StatHat, we had the following check list for ourdevelopment tools:

  • same programming language for backend and frontend systems
  • good, fast HTML templating system
  • fast start-up, recompilation, testing for lots of tinkering
  • lots of connections on one machine
  • language tools for handling application-level concurrency
  • good performance
  • robust RPC layer to talk between tiers
  • lots of libraries
  • open source

We evaluated many popular and not-so-popular web technologies and ended upchoosing to develop it in Go.

When Go was released in November 2009, I immediately installed it and lovedthe fast compilation times, goroutines, channels, garbage collection, andall the packages that were available. I was especially pleased with howfew lines of code my applications were using. I soon experimented withmaking a web app called Langalot that concurrently searched through fiveforeign language dictionaries as you typed in a query. It was blazinglyfast. I put it online and it's been running since February, 2010.

The following sections detail how Go meets StatHat's requirements and ourexperience using Go to solve our problems.

Runtime

We use the standard Go http package for our API and web appservers. All requests first go through Nginx and any non-file requests areproxied to the Go-powered http servers. The backend servers are allwritten in Go and use the rpc package to communicate with the frontend.

Templating

We built a template system using the standard template package. Our systemadds layouts, some common formatting functions, and the ability torecompile templates on-the-fly during development. We are very pleasedwith the performance and functionality of the Go templates.

Tinkering

In a previous job, I worked on a video game called Throne of Darkness thatwas written in C++. We had a few header files that, when modified,required a full rebuild of the entire system, 20-30 minutes long. Ifanyone ever changed `Character.h`, he would be subject to the wrath ofevery other programmer. Besides this suffering, it also slowed downdevelopment time significantly.

Since then, I've always tried to choose technologies that allowed fast,frequent tinkering. With Go, compilation time is a non-issue. We canrecompile the entire system in seconds, not minutes. The development webserver starts instantly, tests complete in a few seconds. As mentionedpreviously, templates are recompiled as they change. The result is thatthe StatHat system is very easy to work with, and the compiler is not abottleneck.

RPC

Since StatHat is a multi-tiered system, we wanted an RPC layer so that allcommunication was standard. With Go, we are using the rpc package and thegob package for encoding Go objects. In Go, the RPC server just takes anyGo object and registers its exported methods. There is no need for anintermediary interface description language. We've found it very easy touse and many of our core application servers are under 300 lines of code.

Libraries

We don't want to spend time rewriting libraries for things like SSL,database drivers, JSON/XML parsers. Although Go is a young language, ithas a lot of system packages and a growing number of user-contributedpackages. With only a few exceptions, we have found Go packages foreverything we have needed.

Open source

In our experience, it has been invaluable to work with open source tools.If something is going awry, it is immensely helpful to be able to examinethe source through every layer and not have any black boxes. Having thecode for the language, web server, packages, and tools allows us tounderstand how every piece of the system works. Everything in Go isopen source. In the Go codebase, we frequently read the tests as theyoften give great examples of how to use packages and language features.

Performance

People rely on StatHat for up to the minute analysis of their data and weneed the system to be as responsive as possible. In our tests, Go'sperformance blew away most of the competition. We tested it against Rails,Sinatra, OpenResty, and Node. StatHat has always monitored itself bytracking all kinds of performance metrics about requests, the duration ofcertain tasks, the amount of memory in use. Because of this, we were ableto easily evaluate different technologies. We've also taken advantage ofthe benchmark performance testing features of the Go testing package.

Application-Level Concurrency

In a former life, I was the CTO at OkCupid. My experience there using OKWStaught me the importance of async programming, especially when it comes todynamic web applications. There is no reason you should ever do somethinglike this synchronously: load a user from the database, then find theirstats, then find their alerts. These should all be done concurrently, yetsurprisingly, many popular frameworks have no async support. Go supportsthis at the language level without any callback spaghetti. StatHat usesgoroutines extensively to run multiple functions concurrently and channelsfor sharing data between goroutines.

Hosting and Deployment

StatHat runs on Amazon's EC2 servers. Our servers are divided into severaltypes:

  • API
  • Web
  • Application servers
  • Database

There are at least two of each type of server, and they are in differentzones for high availability. Adding a new server to the mix takes just acouple of minutes.

To deploy, we first build the entire system into a time-stamped directory.Our packaging script builds the Go applications, compresses the CSS and JSfiles, and copies all the scripts and configuration files. This directoryis then distributed to all the servers, so they all have an identicaldistribution. A script on each server queries its EC2 tags and determineswhat it is responsible for running and starts/stops/restarts any services.We frequently only deploy to a subset of the servers.

More

For more information on StatHat, please visitstathat.com. We are releasing some of the Go code we've written.Go to www.stathat.com/srcfor all of the open source StatHat projects.

To learn more about Go, visit golang.org.

by Andrew Gerrand (noreply@blogger.com) at December 19, 2011 05:00 PM

December 17, 2011

Go on web

Go lang on OS X (lion)

Recently I’ve moved from linux to OS X, yep I’m using a so-called “hackintosh”.   Having spent the past week learning how to live in the environment, which for the record I really live, it’s time to hack some Go! … Continue reading


by hokapoka at December 17, 2011 11:43 AM

December 14, 2011

Go's official blog

From zero to Go: launching on the Google homepage in 24 hours

This article was written by Reinaldo Aguiar, a software engineer from the Search team at Google. He shares his experience developing his first Go program and launching it to an audience of millions - all in one day!

I was recently given the opportunity to collaborate on a small but highly visible "20% project": the Thanksgiving 2011 Google Doodle. The doodle features a turkey produced by randomly combining different styles of head, wings, feathers and legs. The user can customize it by clicking on the different parts of the turkey. This interactivity is implemented in the browser by a combination of JavaScript, CSS and of course HTML, creating turkeys on the fly.

Once the user has created a personalized turkey it can be shared with friends and family by posting to Google+. Clicking a "Share" button (not pictured here) creates in the user's Google+ stream a post containing a snapshot of the turkey. The snapshot is a single image that matches the turkey the user created.

With 13 alternatives for each of 8 parts of the turkey (heads, pairs of legs, distinct feathers, etc.) there are more than than 800 million possible snapshot images that could be generated. To pre-compute them all is clearly infeasible. Instead, we must generate the snapshots on the fly. Combining that problem with a need for immediate scalability and high availability, the choice of platform is obvious: Google App Engine!

The next thing we needed to decide was which App Engine runtime to use. Image manipulation tasks are CPU-bound, so performance is the deciding factor in this case.

To make an informed decision we ran a test. We quickly prepared a couple of equivalent demo apps for the new Python 2.7 runtime (which provides PIL, a C-based imaging library) and the Go runtime. Each app generates an image composed of several small images, encodes the image as a JPEG, and sends the JPEG data as the HTTP response. The Python 2.7 app served requests with a median latency of 65 milliseconds, while the Go app ran with a median latency of just 32 milliseconds.

This problem therefore seemed the perfect opportunity to try the experimental Go runtime.

I had no previous experience with Go and the timeline was tight: two days to be production ready. This was intimidating, but I saw it as an opportunity to test Go from a different, often overlooked angle: development velocity. How fast can a person with no Go experience pick it up and build something that performs and scales?

Design

The approach was to encode the state of the turkey in the URL, drawing and encoding the snapshot on the fly.

The base for every doodle is the background:

A valid request URL might look like this: http://google-turkey.appspot.com/thumb/20332620

The alphanumeric string that follows "/thumb/" indicates (in hexadecimal) which choice to draw for each layout element, as illustrated by this image:

The program's request handler parses the URL to determine which element is selected for each component, draws the appropriate images on top of the background image, and serves the result as a JPEG.

If an error occurs, a default image is served. There's no point serving an error page because the user will never see it - the browser is almost certainly loading this URL into an image tag.

Implementation

In the package scope we declare some data structures to describe the elements of the turkey, the location of the corresponding images, and where they should be drawn on the background image.

var (
// dirs maps each layout element to its location on disk.
dirs = map[string]string{
"h": "img/heads",
"b": "img/eyes_beak",
"i": "img/index_feathers",
"m": "img/middle_feathers",
"r": "img/ring_feathers",
"p": "img/pinky_feathers",
"f": "img/feet",
"w": "img/wing",
}

// urlMap maps each URL character position to
// its corresponding layout element.
urlMap = [...]string{"b", "h", "i", "m", "r", "p", "f", "w"}

// layoutMap maps each layout element to its position
// on the background image.
layoutMap = map[string]image.Rectangle{
"h": {image.Pt(109, 50), image.Pt(166, 152)},
"i": {image.Pt(136, 21), image.Pt(180, 131)},
"m": {image.Pt(159, 7), image.Pt(201, 126)},
"r": {image.Pt(188, 20), image.Pt(230, 125)},
"p": {image.Pt(216, 48), image.Pt(258, 134)},
"f": {image.Pt(155, 176), image.Pt(243, 213)},
"w": {image.Pt(169, 118), image.Pt(250, 197)},
"b": {image.Pt(105, 104), image.Pt(145, 148)},
}
)

The geometry of the points above was calculated by measuring the actual location and size of each layout element within the image.

Loading the images from disk on each request would be wasteful repetition, so we load all 106 images (13 * 8 elements + 1 background + 1 default) into global variables upon receipt of the first request.

var (
// elements maps each layout element to its images.
elements = make(map[string][]*image.RGBA)

// backgroundImage contains the background image data.
backgroundImage *image.RGBA

// defaultImage is the image that is served if an error occurs.
defaultImage *image.RGBA

// loadOnce is used to call the load function only on the first request.
loadOnce sync.Once
)

// load reads the various PNG images from disk and stores them in their
// corresponding global variables.
func load() {
defaultImage = loadPNG(defaultImageFile)
backgroundImage = loadPNG(backgroundImageFile)
for dirKey, dir := range dirs {
paths, err := filepath.Glob(dir + "/*.png")
if err != nil {
panic(err)
}
for _, p := range paths {
elements[dirKey] = append(elements[dirKey], loadPNG(p))
}
}
}

Requests are handled in a straightforward sequence:

  1. Parse the request URL, decoding the decimal value of each character in the path.
  2. Make a copy of the background image as the base for the final image.
  3. Draw each image element onto the background image using the layoutMap to determine where they should be drawn.
  4. Encode the image as a JPEG
  5. Return the image to user by writing the JPEG directly to the HTTP response writer.

Should any error occur, we serve the defaultImage to the user and log the error to the App Engine dashboard for later analysis.

Here's the code for the request handler with explanatory comments:

func handler(w http.ResponseWriter, r *http.Request) {
// Defer a function to recover from any panics.
// When recovering from a panic, log the error condition to
// the App Engine dashboard and send the default image to the user.
defer func() {
if err := recover(); err != nil {
c := appengine.NewContext(r)
c.Errorf("%s", err)
c.Errorf("%s", "Traceback: %s", r.RawURL)
if defaultImage != nil {
w.Header().Set("Content-type", "image/jpeg")
jpeg.Encode(w, defaultImage, &imageQuality)
}
}
}()

// Load images from disk on the first request.
loadOnce.Do(load)

// Make a copy of the background to draw into.
bgRect := backgroundImage.Bounds()
m := image.NewRGBA(bgRect.Dx(), bgRect.Dy())
draw.Draw(m, m.Bounds(), backgroundImage, image.ZP, draw.Over)

// Process each character of the request string.
code := strings.ToLower(r.URL.Path[len(prefix):])
for i, p := range code {
// Decode hex character p in place.
if p < 'a' {
// it's a digit
p = p - '0'
} else {
// it's a letter
p = p - 'a' + 10
}

t := urlMap[i] // element type by index
em := elements[t] // element images by type
if p >= len(em) {
panic(fmt.Sprintf("element index out of range %s: "+
"%d >= %d", t, p, len(em)))
}

// Draw the element to m,
// using the layoutMap to specify its position.
draw.Draw(m, layoutMap[t], em[p], image.ZP, draw.Over)
}

// Encode JPEG image and write it as the response.
w.Header().Set("Content-type", "image/jpeg")
w.Header().Set("Cache-control", "public, max-age=259200")
jpeg.Encode(w, m, &imageQuality)
}

For brevity, I've omitted several helper functions from these code listings. See the source code for the full scoop.

Performance

This chart - taken directly from the App Engine dashboard - shows average request latency during launch. As you can see, even under load it never exceeds 60 ms, with a median latency of 32 milliseconds. This is wicked fast, considering that our request handler is doing image manipulation and encoding on the fly.

Conclusions

I found Go's syntax to be intuitive, simple and clean. I have worked a lot with interpreted languages in the past, and although Go is instead a statically typed and compiled language, writing this app felt more like working with a dynamic, interpreted language.

The development server provided with the SDK quickly recompiles the program after any change, so I could iterate as fast as I would with an interpreted language. It's dead simple, too - it took less than a minute to set up my development environment.

Go's great documentation also helped me put this together fast. The docs are generated from the source code, so each function's documentation links directly to the associated source code. This not only allows the developer to understand very quickly what a particular function does but also encourages the developer to dig into the package implementation, making it easier to learn good style and conventions.

In writing this application I used just three resources: App Engine's Hello World Go example, the Go packages documentation, and a blog post showcasing the Draw package. Thanks to the rapid iteration made possible by the development server and the language itself, I was able to pick up the language and build a super fast, production ready, doodle generator in less than 24 hours.

Download the full app source code (including images) at the Google Code project.

Special thanks go to Guillermo Real and Ryan Germick who designed the doodle.

by Andrew Gerrand (noreply@blogger.com) at December 14, 2011 11:23 PM

December 09, 2011

RSC

_rsc: RT @littlecalculist: "The syntax is weird." — Everyone, of every new language, ever

_rsc: RT @littlecalculist: "The syntax is weird." — Everyone, of every new language, ever

December 09, 2011 11:10 PM

December 05, 2011

RSC

_rsc: RT @cahooon: watching the #golang html parser's progress (http://t.co/UuYbADmd) is impressive, and makes me excited to never write an ht ...

_rsc: RT @cahooon: watching the #golang html parser's progress (http://t.co/UuYbADmd) is impressive, and makes me excited to never write an ht ...

December 05, 2011 02:07 PM

_rsc: RT @ehedgehog: These three considerations ... bring us to the year -292277022399. #golang #epoch #newtimeAPI

_rsc: RT @ehedgehog: These three considerations ... bring us to the year -292277022399. #golang #epoch #newtimeAPI

December 05, 2011 02:04 PM

November 29, 2011

Adam Langley

Certificate Transparency

(I don't have comments on this blog, but you can comment on my Google+ post.)

Ben Laurie and I have been working on a longer term plan for improving the foundations of the certificate infrastructure on which most Internet transport security is based on these days. Although Chrome has public key pinning for some domains, which limits the set of permitted certificates, we don't see public key pinning as a long term solution (and nor was it ever designed to be).

For the 10 second summary of the plan, I'll quote Ben: “certificates are registered in a public audit log. Servers present proofs that their certificate is registered, along with the certificate itself. Clients check these proofs and domain owners monitor the logs.”. I would add only that anyone can check the logs: the certificate infrastructure would be fully transparent.

We now have an outline of the basic idea and will be continuing to flesh it out in the coming months, hopefully in conjunction with other browser vendors.

But I thought that, at the outset, it would be helpful to describe some of the limitations to the design space, as I see them:

No side-channels

As I've previously described, side-channels occur when a browser needs to contact a server other than the immediate destination in order to verify a certificate. Revocation checking with OCSP is an example of a side-channel used today.

But in order to be effective, side-channels invariably need to block the certificate verification until they complete, and that's a big problem. The Internet isn't fully connected. Captive portals, proxies and firewalls all mean that the only thing you can really depend on talking to is the immediate destination server. Because of this, browsers have never been able to make OCSP lookups blocking, and therefore OCSP is basically useless for security.

And that's not to mention the privacy, performance and functionality issues that arise from needing side-channels. (What happens when the side-channel server goes down?)

So our design requires that the servers send us the information that we require. We can use side-channels to check up on the logs, but it's an asynchronous lookup.

It's not opt-in, it's all-in

SSL Stripping is a very easy and very effective attack. HSTS prevents it and is as easy to deploy as anything can be for HTTPS sites. But, despite all this, and despite a significant amount of evangelism, take up has been very limited, even by sites which are HTTPS only and the subject of attacks.

While HSTS really has to be opt-in, a solution to the certificate problem doesn't. Although our scheme is incrementally deployable, the eventual aim is that it's required for everybody. Thankfully, since certificates have to be renewed there's an obvious means to incrementally deploy it: require it for certificates issued after a certain date. Although an eventual hard requirement is still needed, it's a lot less of a problem.

It's easy on the server operator

Since the aim is to make it a requirement for all servers, we've sacrificed a lot in order to make it very easy on the server operator. For most server operators, their CA will probably take care of fetching the audit proofs, meaning there's no additional work at all.

Some initial designs included short-lived log signatures, which would have solved the revocation problem. (Revocation would simply be a matter of instructing the logs to stop signing a given certificate.) However, this would have required server operators to update their audit proofs on a near-daily basis. After discussions it became clear that such a requirement wouldn't be tenable for many and so we reluctantly dropped it.

We are also sacrificing decentralisation to make things easy on the server. As I've previously argued, decentralisation isn't all it's cracked up to be in most cases because 99.99% of people will never change any default settings, so we haven't given up much. Our design does imply a central set of trusted logs which is universally agreed. This saves the server from possibly having to fetch additional audit proofs at runtime, something which requires server code changes and possible network changes.

There are more valid certificates than the one that's currently serving

Cheap virtual hosting and EC2 have made multi-homed services common. Even small to medium scale sites have multiple servers these days. So any scheme that asserts that the currently serving certificate is the only valid certificate will run into problems when certificates change. Unless all the servers are updated at exactly the same time, then users will see errors during the switch. These schemes also make testing a certificate with a small number of users impossible.

In the end, the only real authority on whether a certificate is valid is the site itself. So we don't rely on external observations to decide on whether a certificate is valid, instead to seek to make the set of valid certificates for a site public knowledge (which it currently isn't), so that the site can determine whether it's correct.

It's not easy to do

We believe that this design will have a significant, positive impact on an important part of Internet security and that it's deployable. We also believe that any design that shares those two properties ends up looking a lot like it. (It's no coincidence that we share several ideas with the EFF's Sovereign Keys.)

None the less, deployment won't be easy and, hopefully, we won't be pushing it alone.

November 29, 2011 08:00 AM

November 23, 2011

RSC

_rsc: Road maps are more fun if you pretend they're Feynman diagrams.

_rsc: Road maps are more fun if you pretend they're Feynman diagrams.

November 23, 2011 06:37 PM

November 22, 2011

RSC

_rsc: @sdegutis If I remember correctly, lack of FFI is exactly why John McCarthy didn't use #golang to implement the first Lisp.

_rsc: @sdegutis If I remember correctly, lack of FFI is exactly why John McCarthy didn't use #golang to implement the first Lisp.

November 22, 2011 09:36 PM

Adam Langley

Forward secrecy for Google HTTPS

As announced on the Google Security Blog, Google HTTPS sites now support forward secrecy. What this means in practice is two things:

Firstly, the preferred cipher suite for most Google HTTPS servers is ECDHE-RSA-RC4-SHA. If you have a client that supports it, you'll be using that ciphersuite. Chrome and Firefox, at least, support it.

Previously we were using RSA-RC4-SHA, which means that the client (i.e. browser) picks a random key for the session, encrypts it with the server's public key and sends it to the server. Since only the holder of the private key can decrypt the session key, the connection is secure.

However, if an attacker obtains the private key in the future then they can decrypt recorded traffic. The encrypted session key can be decrypted just as well in ten years time as it can be decrypted today and, in ten years time, the attacker has much more computing power to break the server's public key. If an attacker obtains the private key, they can decrypt everything encrypted to it, which could be many months of traffic.

ECDHE-RSA-RC4-SHA means elliptic curve, ephemeral Diffie-Hellman, signed by an RSA key. You can see a previous post about elliptic curves for an introduction, but the use of elliptic curves is an implementation detail.

Ephemeral Diffie-Hellman means that the server generates a new Diffie-Hellman public key for each session and signs the public key. The client also generates a public key and, thanks to the magic of Diffie-Hellman they both generate a mutual key that no eavesdropper can know.

The important part here is that there's a different public key for each session. If the attacker breaks a single public key then they can decrypt only a single session. Also, the elliptic curve that we're using (P-256) is estimated to be as strong as a 3248-bit RSA key (by ECRYPT II), so it's unlikely that the attacker will ever be able to break a single instance of it without a large, quantum computer.

While working on this, Bodo Möller, Emilia Kasper and I wrote fast, constant-time implementations of P-224, P-256 and P-521 for OpenSSL. This work has been open-sourced and submitted upstream to OpenSSL. We also fixed several bugs in OpenSSL's ECDHE handling during deployment and those bug fixes are in OpenSSL 1.0.0e.

Session Tickets

The second part of forward secrecy is dealing with TLS session tickets.

Session tickets allow a client to resume a previous session without requiring that the server maintain any state. When a new session is established the server encrypts the state of the session and sends it back to the client, in a session ticket. Later, the client can echo that encrypted session ticket back to the server to resume the session.

Since the session ticket contains the state of the session, and thus keys that can decrypt the session, it too must be protected by ephemeral keys. But, in order for session resumption to be effective, the keys protecting the session ticket have to be kept around for a certain amount of time: the idea of session resumption is that you can resume the session in the future, and you can't do that if the server can't decrypt the ticket!

So the ephemeral, session ticket keys have to be distributed to all the frontend machines, without being written to any kind of persistent storage, and frequently rotated.

Result

We believe that forward secrecy provides a fairly significant benefit to our users and we've contributed our work back to OpenSSL in the hope that others will make use of it.

November 22, 2011 08:00 AM

November 12, 2011

Kyle Lemons

Throwing My Hat into the Ring

Generics in Go - A proposal for Generic Types in the Go language

I won’t bore you with the details here; read the PDF for those. Here are some examples to whet your appetite.

A somewhat comprehensive example:

// Package gen provides some example generic functionality.
package gen

// Generic types for this package
type Value generic
type Result generic
type ReduceFunc func(Result, Value) Result

// Tern is the ternary operator: Tern(cond,t,f) == (cond?t:f)
func Tern(cond bool, t, f Value) Value {
  if cond {
    return t
  }
  return f
}

// Reduce returns the result of running the ReduceFunc on each successive value
// in list along with the last result.
func (red ReduceFunc) Reduce(r0 Result, list ...Value) Result {
  for _, v := range list {
    r0 = red(r0, v)
  }
  return r0
}

// Check strips out the os.Error return from a function call for use inline.
func Check(val Value, err os.Error) Value {
  if err != nil {
    panic(err)
  }
  return val
}

Using that package:

package main

import (
  "fmt"
  "gen"
  "strconv"
)

func main() {
  add := gen.ReduceFunc(func(x0 int, x1 string) int {
    return x0 + gen.Check(strconv.Atoi(x1))
  })

	nums := os.Args[1:]
  fmt.Printf("Sum of your %s: %v\n",
    "number" + gen.Tern(len(nums)==1, "", "s"),
    add.Reduce(0, os.Args))
}

Everybody’s favorite generic interface:

type Value generic
type Equaler interface {
  Equals(other Value) bool
}

// Integer implements Equaler.
type Integer int
func (i Integer) Equals(j Integer) bool {
	return i == j
}

// Compile-time check.
var _ Equaler = Integer(0)

// Search returns the index of the first element for which list[idx].Equal(val)
// returns true.
func Search(list []Equaler, val Value) int {
  for i, v := range list {
    if v.Equal(val) {
      return i
    }
  }
  return -1
}

The proposal’s source can be found on github.

by Kyle Lemons at November 12, 2011 08:08 AM

November 11, 2011

RSC

_rsc: RT @sesamestreet: Today is definitely brought to you by the number 11.

_rsc: RT @sesamestreet: Today is definitely brought to you by the number 11.

November 11, 2011 05:06 PM

Go's official blog

The Go Programming Language turns two

Two years ago a small team at Google went public with their fledgling project - the Go Programming Language. They presented a language spec, two compilers, a modest standard library, some novel tools, and plenty of accurate (albeit succinct) documentation. They watched with excitement as programmers around the world began to play with Go. The team continued to iterate and improve on what they had built, and were gradually joined by dozens - and then hundreds - of programmers from the open source community.

The Go Authors went on to produce lots of libraries, new tools, and reams of documentation. They celebrated a successful year in the public eye with a blog post last November that concluded "Go is certainly ready for production use, but there is still room for improvement. Our focus for the immediate future is making Go programs faster and more efficient in the context of high performance systems."

Today is the second anniversary of Go's release, and Go is faster and more stable than ever. Careful tuning of Go's code generators, concurrency primitives, garbage collector, and core libraries have increased the performance of Go programs, and native support for profiling and debugging makes it easier to detect and remove performance issues in user code. Go is also now easier to learn with A Tour of Go, an interactive tutorial you can take from the comfort of your web browser.

This year we introduced the experimental Go runtime for Google's App Engine platform, and we have been steadily increasing the Go runtime's support for App Engine's APIs. Just this week we released version 1.6.0 of the Go App Engine SDK, which includes support for backends (long-running processes), finer control over datastore indexes, and various other improvements. Today, the Go runtime is near feature parity with - and is a viable alternative to - the Python and Java runtimes. In fact, we now serve golang.org by running a version of godoc on the App Engine service.

While 2010 was a year of discovery and experimentation, 2011 was a year of fine tuning and planning for the future. This year we issued several "release" versions of Go that were more reliable and better supported than weekly snapshots. We also introduced gofix to take the pain out of migrating to newer releases. Furthermore, last month we announced a plan for Go version 1 - a release that will be supported for years to come. Work toward Go 1 is already underway and you can observe our progress by the latest weekly snapshot at weekly.golang.org.

The plan is to launch Go 1 in early 2012. We hope to bring the Go App Engine runtime out of "experimental" status at the same time.

But that's not all. 2011 was an exciting year for the gopher, too. He has manifested himself as a plush toy (a highly prized gift at Google I/O and other Go talks) and in vinyl form (received by every attendee at OSCON and now available at the Google Store).

And, most surprisingly, at Halloween he made an appearance with his gopher girlfriend!


Photograph by Chris Nokleberg.

by Andrew Gerrand (noreply@blogger.com) at November 11, 2011 12:28 AM

November 10, 2011

Sonia Codes

Horse racing and ant colonies

This post might be about a worker synchronization idiom—whatever an idiom is.  I don’t know.  Is it a way to solve a common problem in a particular language?  I don’t even know if idioms are good or bad.

Anyway, suppose you have a data structure and you want to let multiple workers concurrently read from it and, each doing things slightly differently, compute independent results.  The workers all need to see the entire data structure, but that’s okay because the data isn’t changing during this read and compute phase.  The results need to be combined and reduced.  When all the results are in and reduced, the result is used to update the shared data structure.  Then it all needs to be repeated for some number of iterations.  Is this common?  Are there physical simulations or stochastic algorithms that are like this?  I would think so.

I know, it’s not sounding like idiomatic Go.  It’s communicating by sharing memory.  But still, if it’s a common thing and there’s a way to do it in the language, isn’t that an idiom?  (Maybe it’s a bad idiom.  Then, I’ve heard people say all idioms are bad.  Maybe it’s a good idiom for doing a bad thing…)  Sometimes though, you have an existing algorithm and you want to follow it, even if it does communicate by sharing memory.

The important feature of the algorithm seemed to be the checkpoints (or barriers or whatever) that separate the phases.

Initialization | processing | update | processing | update | …

  • Initialization is a single thread that sets up the shared data.
  • Processing involves multiple concurrent workers, and can only start after initialization is complete.
  • Update is a single thread again and can only start after all workers rendezvous.
  • The next iteration of processing can only start after the update is complete.

It’s not too hard, and surely, like exercise #69 (it seems to be #70 now) there must be lots of ways to do it.  Before showing my complete program though, I tried reducing it to a minimal program that would show the just synchronization without the clutter of everything else.  It was at that point that the problem struck me as analogous to a horse race.

Initialization is like getting the track groomed and everything all set up.  It has to happen without the horses running around.  There is just one track.  It is shared.  To keep the horses from starting, there is a gate that holds them.  They all wait at the gate until the gate opens and lets them run.  Then they all begin to run concurrently.  They finish at different times and some data is recorded.  At some point, the gates must close in preparation for the next race.

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

var horses = []string{"pie", "biscuit", "lap", "derpy"}

const nRaces =  4

var startSignal, gates sync.WaitGroup
var finish = make(chan string, len(horses))

func main() {
    startSignal.Add(1)
    for _, name := range horses {
        go horse(name)
    }

    places := make([]string, len(horses))

    for r := 1; r <= nRaces; r++ {
        gates.Add(len(horses))
        startSignal.Done()
        gates.Wait()
        startSignal.Add(1)

        for i := range horses {
            places[i] = <-finish
        }
        fmt.Println("race", r, places)
    }
}

func horse(horseName string) {
    rnd := rand.New(rand.NewSource(time.Nanoseconds()))
    for {
        // wait for start signal
        startSignal.Wait()
        gates.Done()

        // do something
        time.Sleep(9e7+rnd.Int63n(2e7))

        // return result
        finish <- horseName
    }
}

Example output:

race 1 [lap derpy biscuit pie]
race 2 [derpy pie biscuit lap]
race 3 [biscuit derpy lap pie]
race 4 [biscuit derpy pie lap]

It seems a little clunky and mousetrap-like, but I think that might just be the nature of communicating by sharing memory. Results are gathered with a channel in the usual way, and then I use two WaitGroups to implement the latch mechanism of the starting gate. startSignal is binary and is a 1 to N broadcast. All of the horses wait on it to fall to zero, implementing the starting gates opening. Then there is the problem of setting it back to 1 for the start of the next race. It needs to be done before the first horse finishes, or else the horse will find the gate open and begin to run again. It cannot be done immediately on the next line of code however, because that leads to the gate snapping shut before some of the horses have had a chance to leave. The solution was a a separate WaitGroup, this one used in the more common N to 1 mode indicating when each goroutine has completed the step of the horse leaving the gate.

Finally now, the fun program. It’s still just a toy problem, but I coded up an ant colony solution” of the knight’s tour problem as described by Philip Hingston and Graham Kendall. It was fun to reproduce their results and get the same number for cumulative production rate. (Although I couldn’t figure out what they were doing to get the .09 number for mean production rate.

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

const boardSize = 8
const nSquares = boardSize * boardSize
const completeTour = nSquares - 1
const cycles = 27000

// pheromone representation read by ants
var tNet = make([]float64, nSquares*8)

// row, col deltas of legal moves
var drc = [][]int{{1, 2}, {2, 1}, {2, -1}, {1, -2},
    {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}

// get square reached by following edge k from square (r, c)
func dest(r, c, k int) (int, int, bool) {
    r += drc[k][0]
    c += drc[k][1]
    return r, c, r >= 0 && r < boardSize && c >= 0 && c < boardSize
}

// struct represents a pheromone amount associated with a move
type rckt struct {
    r, c, k int
    t       float64
}

func main() {
    // waitGroups for ant release clockwork
    var start, reset sync.WaitGroup
    start.Add(1)
    // channel for ants to return tours with pheromone updates
    tch := make(chan []rckt)

    // create an ant for each square
    for r := 0; r < boardSize; r++ {
        for c := 0; c < boardSize; c++ {
            go ant(r, c, &start, &reset, tch)
        }
    }

    // accumulator for new pheromone amounts
    tNew := make([]float64, nSquares*8)

    // map for collecting set of complete tours
    allUnique := make(map[string]int)
    tbuf := make([]byte, 2+completeTour) // for building map key

    // heading
    fmt.Println("Board size:", boardSize)
    fmt.Println("Cycles per repeat:", cycles)
    fmt.Println("          Unique                        Production   Cumm.")
    fmt.Println("        complete     Cumm.      Total   rate         prod.")
    fmt.Println("Repeat     tours    unique   attempts   this repeat  rate")

    // each iteration is a "repeat" as described in the paper
    for repeat := 1; ; repeat++ {
        unique := make(map[string]int) // complete tours this repeat

        // initialize board
        for r := 0; r < boardSize; r++ {
            for c := 0; c < boardSize; c++ {
                for k := 0; k < 8; k++ {
                    if _, _, ok := dest(r, c, k); ok {
                        tNet[(r*boardSize+c)*8+k] = 1e-6
                    }
                }
            }
        }

        // each iteration is a "cycle" as described in the paper
        for i := 0; i < cycles; i++ {
            // evaporate pheromones
            for i := range tNet {
                tNet[i] *= .75
            }

            reset.Add(nSquares) // number of ants to release
            start.Done()        // release them
            reset.Wait()        // wait for them to begin searching
            start.Add(1)        // reset start signal for next cycle

            // gather tours from ants
            for i := 0; i < nSquares; i++ {
                tour := <-tch
                // accumulate complete tours
                if len(tour) == completeTour {
                    tbuf[0] = byte(tour[0].r)
                    tbuf[1] = byte(tour[0].c)
                    for i, m := range tour {
                        tbuf[i+2] = byte(m.k)
                    }
                    key := string(tbuf)
                    unique[key] = 1
                    allUnique[key] = 1
                }
                // accumulate pheromone amounts from all ants
                for _, move := range tour {
                    tNew[(move.r*boardSize+move.c)*8+move.k] += move.t
                }
            }

            // update pheromone amounts on network, reset accumulator
            for i, tn := range tNew {
                tNet[i] += tn
                tNew[i] = 0
            }
        }

        // print statistics
        //  fmt.Println("          Unique                        Production   Cumm.")
        //  fmt.Println("        complete     Cumm.       Total   rate         prod.")
        //  fmt.Println("Repeat     tours    unique    attempts   this repeat  rate")
        fmt.Printf("%6d %9d %9d %10d   %6.4f       %6.4f\n",
            repeat, len(unique), len(allUnique), repeat*cycles*nSquares,
            float64(len(unique))/float64(cycles*nSquares),
            float64(len(allUnique))/float64(repeat*cycles*nSquares))
    }
}

func printTour(tour []rckt) {
    seq := make([]int, nSquares)
    for i, sq := range tour {
        seq[sq.r*boardSize+sq.c] = i + 1
    }
    last := tour[len(tour)-1]
    r, c, _ := dest(last.r, last.c, last.k)
    seq[r*boardSize+c] = nSquares
    fmt.Println("Move sequence:")
    for r := 0; r < boardSize; r++ {
        for c := 0; c < boardSize; c++ {
            m := seq[r*boardSize+c]
            if m > 0 {
                fmt.Printf(" %3d", seq[r*boardSize+c])
            } else {
                fmt.Print("    ")
            }
        }
        fmt.Println()
    }
}

type square struct {
    r, c int
}

func ant(r, c int, start, reset *sync.WaitGroup, tourCh chan []rckt) {
    rnd := rand.New(rand.NewSource(time.Nanoseconds()))
    tabu := make([]square, nSquares)
    moves := make([]rckt, nSquares)
    unexp := make([]rckt, 8)
    tabu[0].r = r
    tabu[0].c = c

    for {
        // cycle initialization
        moves = moves[:0]
        tabu = tabu[:1]
        r := tabu[0].r
        c := tabu[0].c

        // wait for start signal
        start.Wait()
        reset.Done()

        for {
            // choose next move
            unexp = unexp[:0]
            var tSum float64
        findU:
            for k := 0; k < 8; k++ {
                dr, dc, ok := dest(r, c, k)
                if !ok {
                    continue
                }
                for _, t := range tabu {
                    if t.r == dr && t.c == dc {
                        continue findU
                    }
                }
                tk := tNet[(r*boardSize+c)*8+k]
                tSum += tk
                // note:  dest r, c stored here
                unexp = append(unexp, rckt{dr, dc, k, tk})
            }
            if len(unexp) == 0 {
                break // no moves
            }
            rn := rnd.Float64() * tSum
            var move rckt
            for _, move = range unexp {
                if rn <= move.t {
                    break
                }
                rn -= move.t
            }

            // move to new square
            move.r, r = r, move.r
            move.c, c = c, move.c
            tabu = append(tabu, square{r, c})
            moves = append(moves, move)
        }

        // compute pheromone amount to leave
        for i := range moves {
            moves[i].t = float64(len(moves)-i) / float64(completeTour-i)
        }

        // return tour found for this cycle
        tourCh <- moves
    }
}

Output:

Board size: 8
Cycles per repeat: 27000
          Unique                        Production   Cumm.
        complete     Cumm.      Total   rate         prod.
Repeat     tours    unique   attempts   this repeat  rate
     1    176027    176027    1728000   0.1019       0.1019
     2    172491    348518    3456000   0.0998       0.1008
     3    201836    550354    5184000   0.1168       0.1062
     4    104574    654928    6912000   0.0605       0.0948
     5    117130    772058    8640000   0.0678       0.0894
...
    95    193387  12424890  164160000   0.1119       0.0757
    96     37647  12462537  165888000   0.0218       0.0751
    97    121887  12584424  167616000   0.0705       0.0751
    98    145072  12729496  169344000   0.0840       0.0752
    99    162658  12892154  171072000   0.0941       0.0754
   100    206599  13098753  172800000   0.1196       0.0758

by Sonia at November 10, 2011 01:07 AM

November 08, 2011

RSC

_rsc: RT @thehistoryguy: BBC reporter: 'This could be the worst crisis Greece has ever known'. There speaks a man without a history degree.

_rsc: RT @thehistoryguy: BBC reporter: 'This could be the worst crisis Greece has ever known'. There speaks a man without a history degree.

November 08, 2011 06:48 PM

November 07, 2011

Airs – Ian Lance Taylor

Anonymous

There is no chance that Edward de Vere, the Earl of Oxford, wrote the plays attributed to William Shakespeare.

That said, I found the movie Anonymous to be reasonably watchable, although I thought many of Vanessa Redgrave’s scenes as the older Queen Elizabeth were ridiculous. But since the movie claims (perhaps as a joke) to be seriously advocating the position that Oxford wrote the plays, I was surprised that they did such a poor job of supporting the theory.

Oxford was shown as being tutored at length on topics other than poetry. He traveled abroad, he intrigued at court. When would he have had time to write the plays and the sonnets? The movie essentially presents Oxford as being mysterious gifted by the ability to write; he speaks of continual voices in his head. That could happen to anybody, and perhaps describes the real Shakespeare–if anybody could have written Shakespeare’s plays, then why not Shakespeare himself?

Oxford is shown as using the plays to support his court intrigues. Is it possible to imagine Shakespeare, with his clear vision of humanity, thinking that he could achieve such ends through his plays? One of the strongest examples of that in the movie was the suggestion that it was odd that Shakespeare portrayed Richard III as a hunchback, but even I know that Richard III was popularly (and probably falsely) considered to be a hunchback long before Shakespeare’s time.

Of course it’s conceivable if unlikely that somebody else wrote Shakepeare’s plays. But the undercurrent of the Oxford theory has always been that a member of the nobility would be more likely as the playwright than a commoner. But this reverses reality. The nobility were highly trained from birth in their roles in society. They were busy people with lots to do. It was far less likely that an earl could write the plays than a member of the middle class. As far as I know only one member of the English nobility ever achieved any note as an author: Lord Dunsany, who lived much later.

The movie did have a couple of nice (non-Shakespearean) lines, one of which, by the Ben Jonson character, was simply the truth: the only reason future ages remember the people who lived then was because they were alive when Shakespeare was writing.


by Ian Lance Taylor at November 07, 2011 06:09 PM

November 03, 2011

RSC

_rsc: RT @Eris: When you think about it, the Bat Signal was probably the first cloud-based push notification.

_rsc: RT @Eris: When you think about it, the Bat Signal was probably the first cloud-based push notification.

November 03, 2011 03:21 AM

November 02, 2011

Go's official blog

Writing scalable App Engine applications

Back in May, we announced the Go runtime for App Engine. Since then, we've opened it up for everyone to use, added many new APIs, and improved performance. We have been thrilled by all the interesting ways that people are using Go on App Engine.

One of the key benefits of the Go runtime, apart from working in a fantastic language, is that it has high performance. Go applications compile to native code, with no interpreter or virtual machine getting between your program and the machine.

Making your web application fast is important because it is well known that a web site's latency has a measurable impact on user happiness, and Google web search uses it as a ranking factor. Also announced in May was that App Engine would be leaving its Preview status and transitioning to a new pricing model, providing another reason to write efficient App Engine applications.

To make it easier for Go developers using App Engine to write highly efficient, scalable applications, we recently updated some existing App Engine articles to include snippets of Go source code and to link to relevant Go documentation.

By David Symonds, November 2011

by Andrew Gerrand (noreply@blogger.com) at November 02, 2011 01:49 AM

October 31, 2011

Go's official blog

Debugging Go programs with the GNU Debugger

Last year we reported that Go's gc/ld toolchain produces DWARFv3 debugging information that can be read by the GNU Debugger (GDB). Since then, work has continued steadily on improving support for debugging Go code with GDB.

Among the improvements are the ability to inspect goroutines and to print native Go data types, including structs, slices, strings, maps, interfaces, and channels.

To learn more about Go and GDB, see the Debugging with GDB article.

by Andrew Gerrand (noreply@blogger.com) at October 31, 2011 02:06 AM

October 29, 2011

codegrunt.co.uk

Brain Thaw

After writing my last post, I again worked my arse off this week to study both the Stanford AI and ML classes in a less crunchy fashion than the week before. Unfortunately the crunch avoidance did not come to pass and I was up until 3am this morning (well, 2am if you take into account the clocks going back :-) cramming on AI.

Since I actually want to learn about AI (and apply it to weak) I am not finding this cramming aspect of the study particularly beneficial. In my experience, cramming simply wears you out and gets you ready for a given short-term task (e.g. homework, later exams) before the information you’ve learned largely plops out of your brain.

For that reason, and for the sake of keeping this doable, I’ve decided to drop the machine learning course and focus fully on AI. There is an irony in that - I have found the AI work to be considerably more involved and challenging than machine learning (that’s not a comment on the relative quality of one course vs. the other by the way), however a. AI is of more relevance to what I want to do, and b. focusing on one thing reduces the stressful feeling of distraction that maintaing another course which requires homework, etc. brings.

I am still very interested in machine learning, so I might very well go over the machine learning course material at a later date. Additionally, the AI course contains some cross-over material so I won’t do entirely without.

Another aspect is that I would like to maintain my notes to a better standard than I have (and which more time would allow me to achieve), rather than having to rush them out because of time constraints. There are almost certainly better notes for the course out there, however there is nothing quite like having a set of notes to refer to you which you’ve written in your own style and to your own tastes. Not invented here syndrome, perhaps!

Meta-Blog Tediousness

On an entirely different subject, you may have noticed I’ve deleted a number of posts. This is because I felt they were of little value and often quite negative. This blog is meant to be a diary of where I’m at in my tech life, and yes that involves both positive and negative, however there is nothing more tedious than wading through either rather mediocre technical posts, or moany personal ones. I’d like to maximise the chances of me (and to some degree others of course :-) actually enjoying reading this blog.

I can only apologise to those few whose comments have consequently been deleted from this site. Hopefully you can understand why I’ve done it.

October 29, 2011 11:00 PM

October 23, 2011

Journal

Two weeks with Sprint

The lure of Sprint was too strong to pass up, but ultimately ended in letdown and abandon.

Sprint's pitch was simple. Where else can you get an all you can eat data plan and ample minutes for $75.00 including the government’s taxation scheme that Karl Marx would be proud of? Done. Sign me up.

Choosing a phone for Sprint's network was not as easy. They offer three compelling choices.

First, you can speak random thoughts of consciousness in a Sprint store, and if a customer has pressed the Siri button on the display model iPhone 4s, you'll likely get verbal confirmation to choose it.

For those flirting with escape from Jobs’ utopia, the gorilla glass encased false god of Android is eager to entice, with a Samsung Galaxy S II. Like a hollywood celeb it's light weight and beautiful. 

But for some, age brings truth, hence the third option, a year old Samsung Nexus S anointed with the purity of plain Gingerbread. I chose this option.

Despite the sweetness of Gingerbread, there is nothing tasty about Samsung’s Nexus S. Extremely poor antennas for wifi, 3g and 4g? Check. A battery that can’t last ½ day with light to moderate use? Check. Screen jitter while scrolling through your twitter feed? Check.

Needless to say the phone went back to the Sprint store. This left two choices left. The iPhone 4s and the Galaxy S II. I opted with the Galaxy S II.

The Galaxy S II represents everything that is right and wrong about Android, the device manufacturers, and the carriers.

On the Galaxy S II, gone is the jerky scrolling of the Nexus S. The screen is vibrant and huge. Samsung retained its use of a plastic enclosure, yet improved its sturdiness, a move that makes the device feel better quality. The antennas are dramatically better, resulting in greater wifi availability and improved call quality.

Unfortunately, the battery still sucks. And Samsung managed to load the device up with crap ware that you cannot delete from the application menu.

To mitigate the battery life, I bought a few chargers to plant at strategic locations at home, in the car and at the office. And most of the crap ware can be stashed in a folder on the desktop. This uncluttered  things enough to pass inspection.

Just as I was getting used to the device, and on the last day of my free trial, Sprint dropped the mother of all bombs. They announced the ending of the unlimited data plan. Furthermore, current users would not be grandfathered in, and thus would be subject to huge data overage charges.

This triggered my immediate cancellation of the Sprint plan.

The data plan was the only reason why I went to Sprint in the first place. With Sprint customers faced a trade-off. You received unlimited data for a unreliable network that has poor coverage in rural areas.

With their latest decision, there is no tradeoff, and therefore no logical reason why anyone whould choose Sprint over another network.

I leveraged the "winback" program AT&T offers customers who made the transgression of choosing another carrier. They put you back on the program exactly as you left it at no charge. 

So here I am, back with AT&T using a feature phone with nothing but time to ponder my next move.

Do I wait three weeks for Samsung’s Galaxy Nexus that boasts Ice Cream Sandwich, Google’s new OS? Do l let lust get the best of me, and choose a Windows Phone 7, with the Mango update, knowing I'll likely be ultiamtely dissapointed with its feature incompleteness? Or do I return to utopia with an iPhone 4s?

The reality is that in this temporal existence, utopia doesn’t exist and there is no perfect phone. Therefore, I’m sticking with my wife’s old feature phone.

by Charles Thompson at October 23, 2011 07:40 PM

October 22, 2011

codegrunt.co.uk

AI/ML Brain Melt

Am currently attempting to do both the Stanford AI and Machine Learning courses at once. My brain is melting.

I’m keeping my ongoing rough notes on github, I’m writing them in markdown so they render relatively nicely there.

I have realised that, if I am actually going to see both courses through to the end, I am going to have to put everything else on hold until they’re done.

Luckily, they both pertain to greater or lesser degrees to weak so its not entirely a pause on other projects. Also, I might choose to hack on weak from time-to-time since I am chomping at the bit to get it moving.

October 22, 2011 11:00 PM

October 18, 2011

Airs – Ian Lance Taylor

Corporate Unions

In an ordinary employer-employee relationship with a large company, the employer has most of the power. When any individual employee seeks a higher wage, he or she has no leverage; for a large company to lose a single employee makes little difference. In the U.S., unions have been a way for employees to get more leverage. The large company can not ignore the effect of many employees working together.

However, many people dislike unions, because unions are only effective when the union members work together. Many people feel that this takes away individual rights, as indeed it does.

It recently occurred to me that there is a different way to look at the issue. Think of the union as a company itself, a special sort of company which operates as a monopsony. When you join the employer, you are actually joining two companies: the employer and the company which provides employees to the employer. The union company and the regular company have a tight relationship, but this is no different from an ordinary monopsony supplier situation, such as is widely found in, e.g., the automotive business. Union companies tend to be more democratic than most companies, but this is not a fundamental difference.

One can of course have multiple union companies providing labor to the parent company. However, it is perfectly reasonable for the parent company to negotiate only with union companies for labor, rather than with individuals. After all, only in exceptional situations would a company purchase non-labor supplies from an individual. Why should labor be any different? Thus the “closed shop” has a clear support: it’s a matter of efficiency for the parent company.

This perspective may remove some of the traditional complaints against unions. They are replaced by a different issue, which is that every employee has two loyalties. However, in reality we all have multiple loyalties in our lives—to our families, our sports teams, etc.

Try thinking of the matter this way the next time you feel angry about unions. They are just doing what regular companies do.


by Ian Lance Taylor at October 18, 2011 02:10 PM

October 17, 2011

Sonia Codes

Another version of #69

As Andrew said, there are many different ways of approaching this.  Following Rog’s comment to my last post, I tried a new version.  This one starts a goroutine for each URL fetched, but manages to get by with only a single channel communication per URL.  Is it better?  Hard to say.  For the dummied data set, of course none of this matters.  “Better” would make much more sense in the context of a real problem.

Anyway, one more way:

type request struct {
    url   string
    depth int
}

type result struct {
    req  *request
    body string
    urls []string
    err  os.Error
}

func Crawl(url string, depth int, f Fetcher, maxWorkers int) {
    // resCh is the only channel used.
    // one result will be sent for each url fetched,
    // and this is the only synchronization needed.
    resCh := make(chan *result)

    // fetch function literal fetches a single url and
    // returns the result on resCh.
    fetch := func(req *request) {
        body, urls, err := f.Fetch(req.url)
        resCh <- &result{req, body, urls, err}
    }

    // initial condition is that a fetch is started on the top url,
    // the url is noted as having been seen, the fetch is noted as
    // outstanding, and there is otherwise no backlog of requests.
    go fetch(&request{url, depth})
    seen := map[string]bool{url: true}
    outstanding := 1
    var backlog []*request

    for outstanding > 0 {
        // block and wait for a result.  print results when it arrives,
        // and then one less request is known to be outstanding.
        res := <-resCh
        if res.err != nil {
            fmt.Println(res.err)
        } else {
            fmt.Printf("found: %s %q\n", res.req.url, res.body)
        }
        outstanding--

        // check if any urls in the result need to be crawled.
        if res.req.depth <= 1 {
            continue // at depth limit
        }
        for _, u := range res.urls {
            if seen[u] {
                continue // don't fetch the same url twice
            }
            // at this point we know this is a new url we want
            // to crawl.  mark it as seen and prepare a request.
            seen[u] = true
            req := &request{u, res.req.depth - 1}

            // if we are under the concurrency limit, start a
            // fetch immediately.  otherwise backlog it.
            if outstanding < maxWorkers {
                go fetch(req)
                outstanding++
            } else {
                backlog = append(backlog, req)
            }
        }

        // after handling a result, it is possible that it
        // resulted in no new requests, but that there was
        // an existing backlog.  with that oustanding request
        // done, we can pick one request off the backlog.
        if len(backlog) > 0 && outstanding < maxWorkers {
            go fetch(backlog[len(backlog)-1])
            backlog = backlog[:len(backlog)-1]
            outstanding++
        }
    }
}

by Sonia at October 17, 2011 08:35 PM

Going Along

Almost renewed vim

I have been using Acme for many years and almost forgot how complex
the program editors could be. But last several months I have been
digging into headless embedded system, where only Vim is accessible
from serial line (ssh included). I used to be a man full of Vim, but
that days are long gone. Now I am a simple man who is contented with
simple things. So I cannot believe I have spent a whole week reading
'A Byte of Vim' while forcing myself using it for all my code works on
Windows/Linux/Macs, only because Plan9port stopped working after
upgrading to Mac OS X Lion. No. It is a mistake learnt the hard way.
Vim is gone away from me that no force can renew it back. Give up. I'm
a happy Acmer again, even that means I need to run Virtualbox ubuntu
on top of Lion, install Plan9port in order to get back my dear Acme.
Yes. Ubuntu is configured to log on directly into 'Recovery console',
where .profile runs rio & acme & google-chrome &, and xshove the
latter two to full screen, which I am used to alt-tab swapping them.

by Fango (noreply@blogger.com) at October 17, 2011 04:33 AM

October 15, 2011

Sonia Codes

Communication diagrams for Go

As long as I’ve been programming, I’ve often found it helpful to sketch little diagrams to help me puzzle out programs. I’ve been known to draw plain old flowcharts for example, sometimes to the amusement of others. In my object oriented days, I assembled a few very elaborate object hierarchy diagrams, because they seemed a crucial foundation for the elaborate programs based on them. Thankfully none of these are in the pile of scratch paper on my desk currently (or posted on my wall!)

Programming in Go, a type of diagram I have occasionally found useful though, is one showing communication between goroutines. I put a goroutine name in a box and draw a vertical line down from it, vaguely suggesting a sequential process. I represent channels as horizontal lines that go from one vertical line to another. Then I annotate the diagram with whatever I think is relevant. I’ve found pencil and paper best for doing this. Anytime I’ve tried using drawing or diagramming software I’ve wasted huge amounts of time and ultimately not been pleased with the result. For showing examples here, I’m resorting to ASCII art! For exhibit 1, here’s a diagram of Andrew Gerrand’s solution to exercise 64 in A Tour of Go.

                                         one per url to be fetched!
                                                 +---------------+
+------+ url                       (url string) +---------------+|
| main |--------------------------------------->| crawler.fetch |+
+------+                                        +---------------+
|                 n = workers                                   |
|                  +-------------+                              |
|                 +-------------+|                              |
|                 | crawler.run |+                              |
|                 +-------------+                               |
|                               | c.req      string      c.req  |
|                               |-range-----------------------<-|
|                               |                               |
| c.res     *result      c.res  |
|-range-----------------------<-|
|                               |

Note that while lines representing channels go from one vertical line to another, There is a line from the main box to the crawler.fetch box. This doesn’t represent a channel, but data passed as a parameter at the invocation of the goroutine. Without that, it’s not obvious where crawler.fectch gets its data. I put an annotation about this data in parentheses reminiscent of function call syntax.

When there are multiple instances of a goroutine, I use a shadowed box and put an annotation above the box saying something about how many instances are created or are running.

The main annotation for a channel is its type, which I always put above line near the middle. Channel communication generally goes in one direction, which is shown with an arrow on the channel line. If you can arrange goroutines so that communication goes from right to left on the diagram, then the arrows look like send and receive communication operations. I’ve found it useful to annotate both ends of the channel lines with the name of the channel in that goroutine. In this case both ends use the same variable name, so it might seem a little redundant, but I think that’s helpful information.

So, here’s another. This is Rog’s solution.

                                            one per url fetched!
                                                 +-----------+
+------+                           {url string} +-----------+|
| main |--------------------------------------->| "fetcher" |+
+------+                                        +-----------+
|                                                           |
| limiter <-         struct{}, 10                 <-limiter |
|------------------------>>>>>------------------------------|
|                                                           |
|                                                           |
| rc                         result                      r  |
|-range---------------------------------------------------<-|

The fetcher goroutine here happens to be an anonymous function literal. I put “fetcher” in quotes to show that fetcher isn’t really the name of the function in the source code, but is just kind of a descriptive name used in the diagram. Also, the data supplied from the main goroutine is not passed as a parameter but as a free variable to the closure so I used brace brackets, kind of suggestive of the literal.

The limiter channel poses a problem for my diagramming scheme because data goes from left to right. Arrows on the channel line cutely suggestive of channel syntax don’t work because they would go the wrong way. In fact I used to draw them the wrong way, but as I got more used to reading Go code, I found this disturbing. Here I’m experimenting with just putting the channel operators in their correct relation to the channel names and then putting a bold sort of arrow under the channel type. (I’m making this all up as I go along, you see…)

So how does Rog’s approach compare to Andrew’s from this perspective? At a glance it seems nicer because it uses fewer goroutines. Rate limiting is done by putting tokens in a buffered channel and Andrew’s “worker” goroutines are not needed. If you look at the amount of channel communication going on however, it’s about the same. In each case there are two channel communications per url fetched. Really I’m not sure we can do better for a rate limited algorithm.

Can we do worse? Always! For a great example, I offer my previous blog post. Diagrams aren’t even needed to notice the difference in the amount of channel communication going on. I’m having all my concurrent goroutines do a bunch of concurrency stuff that can be avoided with a different algorithm.

A shocking recommendation appeared on Go Nuts recently to “avoid concurrency as much as possible.” This heresy was immediately challenged. The clarification is that of course you use concurrency when you have to, but only when you have to. Synchronization is expensive. If you don’t have to share something, don’t! If there’s a way to avoid sharing something, do it.

Anyway, back to the two diagrams presented above.  There is one thing that bothers me.  There is a separate goroutine created for each individual url.  Is that really necessary?  Looking at all the solutions presented in the Go Nuts thread, they all do this.  My sad solution of the previous post does this.  Goroutines are pretty cheap, true, but still I understand there’s a 4k stack, and who knows what else it takes to get one started.  It seems a lot of churn.

Is there a solution with a diagram like this? It would still have two channel communications per url, but no separate goroutine per url, just main and a small constant number of worker goroutines.

+------+                                         n = workers
| main |                                          +----------+
+------+                                         +----------+|
|                                                | "worker" |
|                                                +----------+
|                                                           |
| reqCh <-              *request                    <-reqCh |
|------------------------->>>>>-----------------------------|
|                                                           |
|                                                           |
|  resCh               *result                       resCh  |
|<--------------------------------------------------------<-|
|                                                           |

Well yes. Here is one example:

type request struct {
    url   string
    depth int
}

type result struct {
    req  *request
    body string
    urls []string
    err  os.Error
}

func Crawl(url string, depth int, f Fetcher, workers int) {
    reqCh := make(chan *request)
    resCh := make(chan *result)
    for i := 0; i < workers; i++ {
        go func() {
            for r := range reqCh {
                body, urls, err := f.Fetch(r.url)
                resCh <- &result{r, body, urls, err}
            }
        }()
    }

    seen := map[string]bool{url: true}
    backlog := []*request{&request{url, depth}}
    outstanding := 1
    for outstanding > 0 {
        var res *result
        for len(backlog) > 0 {
            // feed backlog to reqCh until a result arrives.
            select {
            case reqCh <- backlog[len(backlog)-1]:
                backlog = backlog[:len(backlog)-1]
            case res = <-resCh:
                goto gotResult
            }
        }
        res = <-resCh // no backlog.  block until result arrives.
    gotResult:
        outstanding--
        if res.err != nil {
            fmt.Println(res.err)
        } else {
            fmt.Printf("found: %s %q\n", res.req.url, res.body)
        }
        if res.req.depth > 1 {
            for _, u := range res.urls {
                if !seen[u] {
                    seen[u] = true
                    outstanding++
                    backlog = append(backlog,
                        &request{u, res.req.depth - 1})
                }
            }
        }
    }
}

The magic is all in using a select statement. We need main to be handling data in both directions depending on what data is available. That’s exactly what select does.

These communication diagrams don’t describe algorithms. They don’t even suggest them. They do however give you a synopsis of goroutines and channel communication, and this may prompt you to consider alternatives. Is everything in the diagram needed? Can you solve the problem with less?


by Sonia at October 15, 2011 03:56 AM

October 12, 2011

Go's official blog

Go App Engine SDK 1.5.5 released

Today we released version 1.5.5 the Go App Engine SDK. You can download it from the App Engine downloads page.

This release includes changes and improvements to the App Engine APIs and brings the supporting Go tool chain to release.r60.2 (the current stable release). Also included in this release are the godoc, gofmt, and gofix tools from the Go tool chain. They can be found in the root directory of the SDK.

Some changes made in this release are backwards-incompatible, so we have incremented the SDK api_version to 3. Existing apps will require code changes when migrating to api_version 3.

The gofix tool that ships with the SDK has been customized with App Engine-specific modules. It can be used to automatically update Goapps to work with the latest appengine packages and the updated Go standard library. To update your apps, run:


/path/to/sdk/gofix /path/to/your/app

The SDK now includes the appengine package source code, so you can use the local godoc to read App Engine API documentation:


/path/to/sdk/godoc appengine/datastore Get

Important note: We have deprecated api_version 2. Go apps that use api_version 2 will stop working after 16 December 2011. Please update your apps to use api_version 3 before then.

See the release notes for a full list of changes. Please direct any questions about the new SDK to the Go App Engine discussion group.

by Andrew Gerrand (noreply@blogger.com) at October 12, 2011 04:32 AM

A preview of Go version 1

We want to be able to provide a stable base for people using Go. People should be able to write Go programs and expect that they will continue to compile and run without change, on a timescale of years. Similarly, people should be able to write books about Go, be able to say which version of Go the book is describing, and have that version number still be meaningful much later. None of these properties is true for Go today.

We propose to issue a Go release early next year that will be called “Go version 1”, Go 1 for short, that will be the first Go release to be stable in this way. Code that compiles in Go version 1 should, with few exceptions, continue to compile throughout the lifetime of that version, as we issue updates and bug fixes such as Go version 1.1, 1.2, and so on. It will also be maintained with fixes for bugs and security flaws even as other versions may evolve. Also, production environments such as Google App Engine will support it for an extended time.

Go version 1 will be a stable language with stable libraries. Other than critical fixes, changes made to the library and packages for versions 1.1, 1.2 and so on may add functionality but will not break existing Go version 1 programs.

Our goal is for Go 1 to be a stable version of today’s Go, not a wholesale rethinking of the language. In particular, we are explicitly resisting any efforts to design new language features “by committee.”

However, there are various changes to the Go language and packages that we have intended for some time and prototyped but have not deployed yet, primarily because they are significant and backwards-incompatible. If Go 1 is to be long-lasting, it is important that we plan, announce, implement, and test these changes as part of the preparation of Go 1, rather than delay them until after it is released and thereby introduce divergence that contradicts our goals.

Today, we are publishing our preliminary plan for Go 1 for feedback from the Go community. If you have feedback, please reply to the thread on the golang-nuts mailing list.

by rsc (noreply@blogger.com) at October 12, 2011 04:17 AM

October 10, 2011

RSC

_rsc: RT @bmarkovic79: #golang is most fun I've had with a compiled PL since I've discovered Turbo Pascal as a kid.

_rsc: RT @bmarkovic79: #golang is most fun I've had with a compiled PL since I've discovered Turbo Pascal as a kid.

October 10, 2011 10:21 PM

Sonia Codes

A Tour of Go #69 Exercise: Web Crawler

I can’t resist. The difficulty level of the exercise is similar to much of what I’ve posted on this blog and I just want to talk about it.

For any of you who don’t regularly follow the Go Blog or Go Nuts, I’m talking about the final exercise, #69, of the recently published A Tour of Go. This is a perfect little introduction to the language that starts with the most basic elements of the language and progresses through some of the best and most interesting elements of the language. There are exercises along the way. The first exercises are easy, then they become more challenging. I believe most people new to Go will find the final exercise in particular to be a satisfying challenge. If you haven’t taken the tour yet, I encourage you to do so and to attempt the exercises, especially #69, the subject of my post here.

Done? Good. :) Of the two TODO items, “Don’t fetch the same URL twice” is easy to add.  One quick and dirty solution is to replace the Crawl function with the following,

func Crawl(url string, depth int, fetcher Fetcher) {
    c2(url, depth, fetcher, map[string]bool{url: true})
}

func c2(url string, depth int, fetcher Fetcher, m map[string]bool) {
    // TODO: Fetch URLs in parallel.
    if depth <= 0 {
        return
    }
    body, urls, err := fetcher.Fetch(url)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("found: %s %q\n", url, body)
    for _, u := range urls {
        if !m[u] {
            m[u] = true
            c2(u, depth-1, fetcher, m)
        }
    }
    return
}

This works, but some people might not like a couple of things here. It introduces a new function, c2, and c2 has a fourth parameter. Do you agonize about the clutter of another function or the overhead of pushing another parameter on the stack? There are other ways to do it. You could make the map a package variable. Bad idea. I won’t even show it. An excellent and popular technique is a recursive closure:

func Crawl(url string, depth int, fetcher Fetcher) {
    m := map[string]bool{url: true}
    var c2 func(string, int)
    c2 = func(url string, depth int) {
        // TODO: Fetch URLs in parallel.
        if depth <= 0 {
            return
        }
        body, urls, err := fetcher.Fetch(url)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Printf("found: %s %q\n", url, body)
        for _, u := range urls {
            if !m[u] {
                m[u] = true
                c2(u, depth-1)
            }
        }
        return
    }
    c2(url, depth)
}

Alternatively, you could use a struct and a method:

func Crawl(url string, depth int, fetcher Fetcher) {
    c := &crawlHistory{fetcher, map[string]bool{url: true}}
    c.c2(url, depth)
}

type crawlHistory struct {
    fetcher Fetcher
    m       map[string]bool
}

func (c *crawlHistory) c2(url string, depth int) {
    // TODO: Fetch URLs in parallel.
    if depth <= 0 {
        return
    }
    body, urls, err := c.fetcher.Fetch(url)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("found: %s %q\n", url, body)
    for _, u := range urls {
        if !c.m[u] {
            c.m[u] = true
            c.c2(u, depth-1)
        }
    }
    return
}

Which of these forms appeal to you is likely to depend on what other languages you know. If you know a language with closures, you probably like the first one. If your experience is object oriented, the second probably looks good to you. It really doesn’t matter.

One elegant little change that can be made to the second version is to embed Fetcher in crawlHistory. Remove the field name from Fetcher so that the struct definition reads simply

type crawlHistory struct {
    Fetcher
    m map[string]bool
}

The call to Fetch can now be written

    body, urls, err := c.Fetch(url)

Embedding wasn’t covered in the tour, but that’s okay because we’re not going to complete this exercise without a couple of other things that weren’t covered in the tour.

Next on the TODO list is fetching URLs in parallel. We’re going to start multiple goroutines and let them crawl different urls found on the page. A pretty big thing not covered in the tour is that concurrent map access is not safe without synchronization. That means these multiple goroutines need to access to the map one at a time. If we don’t serialize access to the map, we risk wrong answers or even corrupt memory and a crash.

Also not covered is that fmt.Println should be synchronized as well. It turns out that we have no guarantee from the operating system even that output from multiple threads won’t get interleaved.

Finally, covered so subtly that you probably missed it is that a Go program terminates when main terminates, without waiting for other goroutines. (Take another look at the output of example 61.) We will start goroutines to crawl urls found on the page, but then we need to do something to wait for them to complete.

So we’re going to need lots of synchronization to do this exercise! The tour did mention the sync package, and there is stuff there to do the job, but we can do the exercise easily without the sync package so I won’t go into it just yet. Go channels, as shown in the tour, represent a very general purpose mechanism that easily handle all of the synchronization needs for this exercise.

The pattern I use for serializing access to the shared resources—the map and fmt.Println—uses channels with capacity=1. One channel is created for a resource that is to be shared and this one channel is passed to all goroutines that want to access the resource. When a goroutine wants to access the resource, it must take (some reference to) the resource from the channel, use the resource, and then return the resource to the channel. It’s like the salt shaker on the table. Only one person uses it at a time, and otherwise it sits in a place where everyone can reach it.

The pattern I use to wait for goroutine completion is that if a function launches goroutines, it must also create a channel for them to report completion. It counts the number of goroutines it launches, then waits for exactly that many completion reports.

Simple enough? Here’s the code:

// struct renamed crawlShared to reflect that it provides access to
// things that are shared across goroutines (the goroutines being
// instances of c2, specifically.)
type crawlShared struct {
    Fetcher     // embedded for Fetch method.

    // the "salt shaker" pattern:  a channel holds the shared map
    mapAccess   chan map[string]bool

    // about the same pattern, except that there is nothing obvious
    // to put in the channel.  instead, a boolean value is used as
    // a token.  the value is ignored.  all that matters is the
    // presence or absence of the token in the channel.
    printAccess chan bool
}

func Crawl(url string, depth int, fetcher Fetcher) {
    c := &crawlShared{
        fetcher,
        make(chan map[string]bool, 1),
        make(chan bool, 1),
    }

    // put the salt shaker on the table.  that is, put the map
    // in the channel, making it available to goroutines.
    c.mapAccess <- map[string]bool{url: true}

    // same with the token to serialize printing
    c.printAccess <- true

    // run goroutine to crawl top level url.
    // since we are starting exactly one goroutine here, we wait
    // for a single completion report.  receipt means that all
    // lower levels have also completed and it is safe to return
    // --and allow the caller to return, in this case, main().
    done := make(chan bool)
    go c.c2(url, depth, done)
    <-done
}

func (c *crawlShared) c2(url string, depth int, pageDone chan bool) {
    // the function has multiple return points.  all of them must
    // report goroutine completion by sending a value on pageDone.
    if depth <= 0 {
        pageDone <- true
        return
    }

    body, urls, err := c.Fetch(url)
    if err != nil {
        // here's how to print:
        // take the token (waiting for it if it's not there.)
        <-c.printAccess
        // do whatever you need to do while other goroutines are
        // excluded from printing.
        fmt.Println(err)
        // put the token back, allowing others to print again.
        c.printAccess <- true

        pageDone <- true
        return
    }

    // same sequence of steps to print found message
    <-c.printAccess
    fmt.Printf("found: %s %q\n", url, body)
    c.printAccess <- true

    // "found" means the url was fetched without error and that urls
    // on the fetched page are collected in the slice "urls."
    // synchronization to crawl these urls in parallel is implemeted
    // with the uDone channel.  create the channel, count the number of
    // goroutines started, then wait for exactly that many completions.
    uDone := make(chan bool)
    uCount := 0

    // salt shaker pattern for map access:  get the map from
    // the channel, and then hold it while iterating over urls.
    // this works with the assumption that all of the operations here
    // take trivial time compared to the relatively lengthy time
    // to fetch a url.  other than map access (which is what we need
    // exclusive access for!) the only operations are iterating over
    // a string slice, incrementing an integer, and starting
    // a goroutine.  these all run very fast so it is reasonable and
    // best to hold "the lock" that is, exclusive map access, while
    // running through this loop.
    m := <-c.mapAccess
    for _, u := range urls {
        if !m[u] {
            m[u] = true
            uCount++
            go c.c2(u, depth-1, uDone)
        }
    }
    c.mapAccess <- m

    // wait for the number of goroutines started just above.
    for ; uCount > 0; uCount-- {
        <-uDone
    }

    // and finally, report completion of this level
    pageDone <- true
}

So that’s kind of the safety scissors version. I think it shows a correct solution while introducing a minimum of concepts that were not covered in the tour.

I’ll leave you with this alternative that uses a few more tricks. The tricks require greater knowledge of the language and libraries, but make for code that is both more concise and faster. This version dispenses with the struct by going back to the recursive closure, trades printAccess for a little trick with the log package, trades mapAccess for a sync.Mutex, and replaces all the done channels with a single sync.WaitGroup. With the waitGroup there is no longer any need to run the first call to c2 as a goroutine, mm, and there’s a defer statement in there too.

import (
    "log"
    "os"
    "sync"
)

var fmt = log.New(os.Stdout, "", 0)

func Crawl(url string, depth int, fetcher Fetcher) {
    m := map[string]bool{url: true}
    var mx sync.Mutex
    var wg sync.WaitGroup
    var c2 func(string, int)
    c2 = func(url string, depth int) {
        defer wg.Done()
        if depth <= 0 {
            return
        }
        body, urls, err := fetcher.Fetch(url)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Printf("found: %s %q\n", url, body)
        mx.Lock()
        for _, u := range urls {
            if !m[u] {
                m[u] = true
                wg.Add(1)
                go c2(u, depth-1)
            }
        }
        mx.Unlock()
    }
    wg.Add(1)
    c2(url, depth)
    wg.Wait()
}

Oh, and without the fmt package, there’s a line in fakeFetcher.Fetch that now has to read

    return "", nil, os.NewError("not found: " + url)

by Sonia at October 10, 2011 01:56 AM

October 08, 2011

codegrunt.co.uk

Bitboards

My open source chess engine project, Weak, is progressing well as I build up the absolute fundamentals.

As discussed in the readme, the current approach is to get to the point where the engine can play legal (against a subset of the rules of chess), random, moves. Once this is implemented then I will slowly improve the engine until it gets to the point where it can actually play competitive chess.

In pursuit of this, I have been taking full advantage of the amazing power of the internet to provide information on any subject you can possibly think of, no matter how niche, and the best resource I have found so far is the rather excellent chess programming wiki.

As a guide to how to proceed, I have been working through the getting started section of the wiki, starting with the board representation, which is arguably the most fundamental choice in the design of a chess engine.

I have chosen to use a bitboard representation. Bitboards work by assigning 64-bit values to each piece type, where each bit of each value indicates whether a piece of the type in question is present on a certain square of the board. Representing the board this way permits the use of bitwise logic to interact with different board positions + due to the ease with which processors can perform these operations, analysis can be performed efficiently (in theory, at least).

Here is the actual board data structure:-

struct board {
  uint64_t pawns, rooks, knights, bishops, queen, king;
};

The engine stores separate instances of this for white and black.

I have chosen to map board positions to bit positions as follows:-

bit = rank * 8 + file

E.g.:-

Index of E1 = 0 * 8 + 4 = 4

Index of H3 = 2 * 8 + 7 = 23

Over the coming week I hope to actually bring the engine to the point where it can play a game :)

Forgive the sparseness of this post, I would have liked to put some more graphics + effort into it, however I am so ridiculously busy at the moment with this + my other projects that I simply do not have the time!

October 08, 2011 11:00 PM

Adam Langley

Classifying solutions to the certificate problem

This is something that I wrote up internally, but which Chris Palmer suggested would be useful to post publicly for reference. It presents some, somewhat artificial, axis on which I believe that all proposed solutions to addressing weaknesses in the current certificate ecosystem fall. By dividing the problem into those decisions, I hope to make the better solutions clearer.

Axis 1: Private vs public signing

At the moment we have private signing. A CA can sign a certificate and tell nobody about it and the certificate is still valid. This is the reason that we have to go crawl the Internet in order to figure out how many intermediate CA certs there are.

In public signing, certificates aren't valid unless they're public.

There are degrees of how public schemes are. Convergence is a public scheme: certificates have to be visible to the notaries, but it's a lot less public than a scheme where all certificates have to be published. And published where? Highly public schemes imply some centralisation.

Private schemes don't protect us from CAs acting in bad faith or CAs which have been compromised. Public schemes help a lot in these cases, increasingly so the more public they are. Although a certificate might be valid for a short time, evidence of misbehavior is recorded. Public schemes also allow each domain to monitor all the certificates for their domain. Fundamentally, the only entity that can answer the question of whether a certificate is legitimate is the subject of the certificate itself. (See this tale of a Facebook certificate.)

Private schemes have the advantage of protecting the details of internal networks (i.e. not leaking the name www.corp.example.com).

Axis 2: Side channels or not

Revocation checking which calls back to the CA is a side channel. Convergence notaries are too. OCSP stapling is not, because the server provides the client with everything that it needs (assuming that OCSP stapling worked).

Side channels which need to be a hard fail are a problem: it's why revocation checking doesn't actually work. Private networks, hotel networks and server downtime are the issues. Side-channels are also a privacy and performance concern. But they're easier on the server operator and so more likely to be deployed.

In the middle are soft-fail side-channels. These offer limited protection because the connection proceeds even when the side-channel fails. They often queue the certificate up for later reporting.

Axis 3: Clocks or not

OCSP with nonces doesn't need clock sync. OCSP with time stamps does.

Keeping clocks in sync is a problem in itself, but it allows for short lived statements to be cached. It can also be a useful security advantage: Nonces require that a signing key be online because it has to sign a constant stream of requests. With clocks, a single response can be served up repeatedly and the key kept largely off-line. That moves the online key problem to the clock servers, but that's a smaller problem. A compromised clock server key can be handled by querying several concurrently and picking the largest cluster of values.

Examples

OCSP today is {private,side-channel,clock}. The 'let's fix OCSP' solutions are typically {private,side-channel,no-clock} (with an option for no-side-channel). Convergence is {mostly-public,side-channel,no-clock}.

I think the answer lies with {public,no-side-channel,clock}, but it's a trek to get there. Maybe something for a future post.

October 08, 2011 07:00 AM

October 06, 2011

RSC

_rsc: A preview of Go version 1. http://t.co/TEmIwVX9 #golang @go_nuts

_rsc: A preview of Go version 1. http://t.co/TEmIwVX9 #golang @go_nuts

October 06, 2011 01:23 PM

October 04, 2011

Go's official blog

Learn Go from your browser

We are excited to announce A Tour of Go, a guided tour of the Go programming language you can run from your browser.

The tour is hands-on, demonstrating the language through code samples that you can modify, compile, and run from the tour itself. (The technology behind the Go Playground does the work.)

The tour has three sections. The first section covers basic syntax and data structures; the second discusses methods and interfaces; and the third introduces Go's concurrency primitives. Each section concludes with a few exercises so you can practice what you've learned.

So, what are you waiting for? Get started now!

by Andrew Gerrand (noreply@blogger.com) at October 04, 2011 11:18 PM

October 03, 2011

Adam Langley

False Start: Brocade broken again

I wrote previously that Brocade had released a firmware update for their ServerIron SSL terminators which fixed an incompatibility with False Start. However, it now appears that they didn't fix the underlying issue, rather they special cased Chrome's current behaviour.

Sadly, due to Chrome implementing a workaround for the BEAST attack, their special casing doesn't work anymore and breaks Chrome 15.

So, if you run a Brocade ServerIron you should contact me ASAP. I'll be adding sites running this hardware to the False Start blacklist for Chrome 15 to allow Brocade to release another firmware update.

October 03, 2011 07:00 AM

October 01, 2011

Sonia Codes

Dining Savages

“Dining Savages” is another toy problem from The Little Book of Semaphores.  Quoting from the PDF,

A tribe of savages eats communal dinners from a large pot that
can hold M servings of stewed missionary1. When a savage wants to
eat, he helps himself from the pot, unless it is empty. If the pot is
empty, the savage wakes up the cook and then waits until the cook
has refilled the pot.

So…the pot is a shared integer.  Sure you could make one special place in memory and do synchronization stuff to control access to it, but that’s not the Go way.  In Go, we share by communicating—in this case, that means we pass the integer around.  We pass it around by passing it through channels.  One channel I call potHolder. It’s a buffered channel with capacity=1. It’s a place to put the pot. Let all of the savages access the pot holder and the result is that any of them can pick up the pot, take from it, (decrement the int) and then return the pot to the pot holder. If a savage finds it empty, he uses another channel to give it to the cook.

package main

import (
    "log"
    "os"
    "rand"
    "sync"
    "time"
)

var fmt = log.New(os.Stdout, "", log.Lmicroseconds)

const (
    potCap             = 5
    nSavages           = 3
    nServingsPerSavage = 4
    cookTime           = 2e8
    avgEatTime         = 1e8
)

type pot int

func main() {
    rand.Seed(time.Nanoseconds())

    fillReq := make(chan pot)
    go cook(fillReq)

    potHolder := make(chan pot, 1)
    potHolder <- pot(0)
    var s sync.WaitGroup
    s.Add(nSavages)
    for i := 1; i <= nSavages; i++ {
        // savages wander into camp at random intervals
        time.Sleep(rand.Int63n(cookTime) / nSavages)
        go savage(i, potHolder, fillReq, &s)
    }
    s.Wait()
}

func savage(i int, potHolder, fillReq chan pot, s *sync.WaitGroup) {
    // some savages eat faster than others
    eatTime := avgEatTime/2 + rand.Int63n(avgEatTime)

    for s := 0; s < nServingsPerSavage; s++ {
        fmt.Println("savage", i, "hungry!")
        pot := <-potHolder
        if pot == 0 {
            fmt.Println("savage", i, "finds empty pot and wakes cook")
            fillReq <- pot
            pot = <-fillReq
        }
        fmt.Println("savage", i, "eating")
        potHolder <- pot - 1
        // time passes while eating
        time.Sleep(eatTime)
    }
    fmt.Println("savage", i, "happy")
    s.Done()
}

func cook(fillReq chan pot) {
    for {
        fmt.Println("cook sleeping")
        <-fillReq
        fmt.Println(`cook grumbles "i'm cooking"`)
        time.Sleep(cookTime) // time passes while cooking
        fmt.Println(`cook mutters "here, ya ungrateful savages"`)
        fillReq <- potCap
    }
}

The algorithm here is almost exactly the same as Downey’s, and yet Downey’s explanation includes a mutex, semaphores, rendezvous, and a “scoreboard pattern.” Is it really that hard? I think mostly the wording is hard. In general, I find the wording of Go easier.

Go also helps by holding related things together. A mutex is a thing that synchronizes access to some data object. They are separate and the declaration of the mutex generally has no dependence on the declaration of the data object. You’re on your own to use some coding convention to hold them together. A Go channel on the other hand is typed. A channel can fulfill the synchronization role of a mutex, but is typed consistent with the data object.

This simple pattern I use when I translate a mutex-based algorithm into Go is to create a buffered channel of capacity=1 to hold the data object.

mutex.wait() (with no reference to the data object being controlled) in Downey’s pseudocode becomes pot := <-potHolder in my code. It’s so nice to not have to worry about where the data object is, what it is called, or who else might be messing with it.  The synchronization operation (channel receive) serves up the data object and hands it to you!  It’s synchronization and communication.

The channel savages use for communicating with the cook is not buffered and is also not in a select statement on either end. Both send and receive are therefore blocking, which makes this an example of a rendezvous. The term is used to describe synchronous communication between threads. Synchronous means that a savage goroutine will be at the line fillReq <- pot at the same time that the cook goroutine is at the line <-fillReq. Which ever goroutine arrives at its line first will block and wait for the other goroutine to arrive at its line. Data is then exchanged through the channel and both goroutines can proceed.

Why do we even care? Well, we don’t strictly need to know the term rendezvous, but in this case it is nice to know that the communication operation is blocking. It lets us use the same channel for communication in both ways. Once the savage has handed over the pot to the cook, he stands there ready to take the pot back with the same hand, so to speak. This only works because the pot is guaranteed to be in complete possession of the cook before the savage can attempt to take it back again.

This guarantee comes from the Go memory model specification which defines the concept “happens before” which provides guarantees such as this about the order of events in Go’s concurrent environment. The document says for example, “A send on a channel happens before the corresponding receive from that channel completes.” This is comforting, but then very interestingly it says, “A receive from an unbuffered channel happens before the send on that channel completes.” Here is the guarantee of synchronous communication. It’s saying that once the savage starts handing the pot over, that the cook getting the pot out of the channel happens before the the savage is free to do anything else. That is, before the next line of savage code attempts to take the pot back.


by Sonia at October 01, 2011 12:17 AM

September 29, 2011

Go's official blog

The Go image/draw package

Package image/draw defines only one operation: drawing a source image onto a destination image, through an optional mask image. This one operation is surprisingly versatile and can perform a number of common image manipulation tasks elegantly and efficiently.

Composition is performed pixel by pixel in the style of the Plan 9 graphics library and the X Render extension. The model is based on the classic "Compositing Digital Images" paper by Porter and Duff, with an additional mask parameter: dst = (src IN mask) OP dst. For a fully opaque mask, this reduces to the original Porter-Duff formula: dst = src OP dst. In Go, a nil mask image is equivalent to an infinitely sized, fully opaque mask image.

The Porter-Duff paper presented 12 different composition operators, but with an explicit mask, only 2 of these are needed in practice: source-over-destination and source. In Go, these operators are represented by the Over and Src constants. The Over operator performs the natural layering of a source image over a destination image: the change to the destination image is smaller where the source (after masking) is more transparent (that is, has lower alpha). The Src operator merely copies the source (after masking) with no regard for the destination image's original content. For fully opaque source and mask images, the two operators produce the same output, but the Src operator is usually faster.

Geometric Alignment

Composition requires associating destination pixels with source and mask pixels. Obviously, this requires destination, source and mask images, and a composition operator, but it also requires specifying what rectangle of each image to use. Not every drawing should write to the entire destination: when updating an animating image, it is more efficient to only draw the parts of the image that have changed. Not every drawing should read from the entire source: when using a sprite that combines many small images into one large one, only a part of the image is needed. Not every drawing should read from the entire mask: a mask image that collects a font's glyphs is similar to a sprite. Thus, drawing also needs to know three rectangles, one for each image. Since each rectangle has the same width and height, it suffices to pass a destination rectangle r and two points sp and mp: the source rectangle is equal to r translated so that r.Min in the destination image aligns with sp in the source image, and similarly for mp. The effective rectangle is also clipped to each image's bounds in their respective co-ordinate space.

The DrawMask function takes seven arguments, but an explicit mask and mask-point are usually unnecessary, so the Draw function takes five:
// Draw calls DrawMask with a nil mask.
func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)

func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point,
mask image.Image, mp image.Point, op Op)

The destination image must be mutable, so the image/draw package defines a draw.Image interface which has a Set method.
type Image interface {
image.Image
Set(x, y int, c image.Color)
}

Filling a Rectangle

To fill a rectangle with a solid color, use an image.ColorImage source. The ColorImage type re-interprets a Color as a practically infinite-sized Image of that color. For those familiar with the design of Plan 9's draw library, there is no need for an explicit "repeat bit" in Go's slice-based image types; the concept is subsumed by ColorImage.
// image.ZP is the zero point -- the origin.
draw.Draw(dst, r, &image.ColorImage{c}, image.ZP, draw.Src)

To initialize a new image to all-blue:
m := image.NewRGBA(640, 480)
blue := image.RGBAColor{0, 0, 255, 255}
draw.Draw(m, m.Bounds(), &image.ColorImage{blue}, image.ZP, draw.Src)

To reset an image to transparent (or black, if the destination image's color model cannot represent transparency), use image.Transparent, which is an image.ColorImage:
draw.Draw(m, m.Bounds(), image.Transparent, image.ZP, draw.Src)


Copying an Image

To copy from a rectangle sr in the source image to a rectangle starting at a point dp in the destination, convert the source rectangle into the destination image's co-ordinate space:
r := image.Rectangle{dp, dp.Add(sr.Size())}
draw.Draw(dst, r, src, sr.Min, Src)

Alternatively:
r := sr.Sub(sr.Min).Add(dp)
draw.Draw(dst, r, src, sr.Min, Src)

To copy the entire source image, use sr = src.Bounds().

Scrolling an Image

Scrolling an image is just copying an image to itself, with different destination and source rectangles. Overlapping destination and source images are perfectly valid, just as Go's built-in copy function can handle overlapping destination and source slices. To scroll an image m by 20 pixels:
b := m.Bounds()
p := image.Pt(0, 20)
// Note that even though the second argument is b,
// the effective rectangle is smaller due to clipping.
draw.Draw(m, b, m, b.Min.Add(p), draw.Src)
dirtyRect := b.Intersect(image.Rect(b.Min.X, b.Max.Y-20, b.Max.X, b.Max.Y))


Converting an Image to RGBA

The result of decoding an image format might not be an image.RGBA: decoding a GIF results in an image.Paletted, decoding a JPEG results in a ycbcr.YCbCr, and the result of decoding a PNG depends on the image data. To convert any image to an image.RGBA:
b := src.Bounds()
m := image.NewRGBA(b.Dx(), b.Dy())
draw.Draw(m, m.Bounds(), src, b.Min, Src)


Drawing Through a Mask

To draw an image through a circular mask with center p and radius r:
type circle struct {
p Point
r int
}

func (c *circle) ColorModel() image.ColorModel {
return image.AlphaColorModel
}

func (c *circle) Bounds() image.Rectangle {
return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
}

func (c *circle) At(x, y int) image.Color {
xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
if xx*xx+yy*yy < rr*rr {
return image.AlphaColor{255}
}
return image.AlphaColor{0}
}

draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over)


Drawing Font Glyphs

To draw a font glyph in blue starting from a point p, draw with an image.ColorImage source and an image.Alpha mask. For simplicity, we aren't performing any sub-pixel positioning or rendering, or correcting for a font's height above a baseline.
src := &image.ColorImage{image.RGBAColor{0, 0, 255, 255}}
mask := theGlyphImageForAFont()
mr := theBoundsFor(glyphIndex)
draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, Over)


Performance

The image/draw package implementation demonstrates how to provide an image manipulation function that is both general purpose, yet efficient for common cases. The DrawMask function takes arguments of interface types, but immediately makes type assertions that its arguments are of specific struct types, corresponding to common operations like drawing one image.RGBA image onto another, or drawing an image.Alpha mask (such as a font glyph) onto an image.RGBA image. If a type assertion succeeds, that type information is used to run a specialized implementation of the general algorithm. If the assertions fail, the fallback code path uses the generic At and Set methods. The fast-paths are purely a performance optimization; the resultant destination image is the same either way. In practice, only a small number of special cases are necessary to support typical applications.

By Nigel Tao, September 2011

by Nigel Tao (noreply@blogger.com) at September 29, 2011 04:50 PM

Airs – Ian Lance Taylor

Nuclear Irrationality

I was recently thinking about my college studies of nuclear warfare. At the time it seemed like a relevant topic, and I took two courses on it. Like everything, the more you look into it the more complex it gets. The depth of the thinking in nuclear warfare planning was both impressive and appalling.

One of the more interesting cases was driven by the fear of a Soviet invasion of Western Europe. In retrospect we know there was never a danger of that, but at the time it was a real concern. The western strategists feared that in a conventional war, the Soviet tanks would rapidly rout the smaller European armies. The use of nuclear weapons, or at least their potential use, was an obvious way to counter this threat.

However, most of the nuclear weapons were in the U.S. It was clear that no U.S. president would launch nuclear weapons at the Soviet Union in order to forestall an invasion of Europe. The U.S. promised to support Europe, but if the war actually started, a nuclear attack on the U.S.S.R. could only end in a nuclear counter-attack on the U.S. That would never happen. England and France had a few nuclear weapons, but would their leaders really launch them, knowing that they would face certain death in the overwhelming nuclear counter-attack? A bold and calculating leader of the Soviet Union might be willing to risk that nobody would take the nuclear option, and be willing to gamble that they would win a conventional war (again, this was the fear of the U.S. and Europe, the Soviet Union knew perfectly well that they could not win such a war). How could the U.S. and Europe use nuclear weapons as a credible deterrent to a conventional invasion?

The answer was, as I said, both impressive and appalling. NATO distributed low-yield nuclear weapons throughout Europe (they even had nuclear landmines). In the event of an invasion, complete control over the weapons was handed over to local commanders. The decision to use nuclear weapons would not be in the hands of an elected leader far from the war zone. It would be in the hands of a local colonel facing the immediate loss of his command. The Soviet Union might gamble (so the thinking went) on the reactions of a few political leaders they could study closely. They would never gamble on the reactions of several hundred local military commanders. Although the weapons were relatively low-yield, the expectation was that once a war went nuclear, the only thing that would stop it from escalating would be a quick complete cessation of hostilities.

This is a nice example of achieving your goal by explicitly giving up your ability to act rationally.


by Ian Lance Taylor at September 29, 2011 04:27 AM

September 27, 2011

Sonia Codes

Barber Shop

In a Go-nuts discussion a while back, Dmitry Vyukov mentions in passing,

Btw, it’s possible to implement Barber Shop with only channels in a
quite idiomatic way (IMHO):

seats := make(chan *Customer, 4) // 4 seats

func (self *Customer) EnterShop() bool {
select {
case seats <- self:
default: return false
}

}

The Barber Shop problem to which he refers has a Wikipedia page and is also covered in “The Little Book of Semaphores.”  If you’re not familiar with the problem, I encourage you to go read the WP entry at least, then look at Dmitry’s code snippet.  He’s using a buffered channel as a data structure that conveniently has safe concurrent access already built in.  The elements of the channel represent the waiting room seats in the barber shop.  Each one can hold one customer.  Customers can contend for the seats, but the language will see to it that there is never more than one customer per seat.  The barber can attend to them in order, and more customers can seat themselves only as waiting room seats become available.  This solution has no mutexes or semaphores, just a single Go channel.

The technique is nice and is really just that simple.  The rest of this post takes a closer (if pedantic) look.

So it’s nice, but it hasn’t been mentioned yet what real-world problems this might apply to. The buffered channel represents a thread-safe FIFO queue. That should certainly have wide applicability. The opening paragraph of the WP article says it’s “inter-process communication and synchronization problem between multiple operating system processes.” Well wahoo, that covers a lot of territory. Thankfully, the problem as usually stated is much narrower. The specifics are usually start something like,

  • There are a number of “customer” processes which can run concurrently.
  • There is one “barber” process which runs concurrently with customer processes.
  • The barber process can do some unit of useful work “haircut” for customer processes.
  • There is a fixed capacity FIFO queue that customer processes must use to get the barber process to perform the haircut.

So in other words, it’s a job queuing system. Now we’re getting somewhere. Additional details are usually specified or implied:

  • The customer process “gets the barber to perform the haircut” by enqueueing some data object. The data object represents a command for the barber process to perform the haircut work unit for that customer.
  • The barber process performs the action of executing the haircut command; that is, it does the work.
  • The barber executes only one command at a time and processes it to completion before accepting the next command.
  • If the queue is empty and the barber is not executing a command, the barber sleeps. That is, the barber process enters some sort of idle state.
  • If a customer process enqueues a haircut command when the barber is sleeping, the barber wakes.
  • In the awake state, and while there are haircut commands enqueued, the barber sequentially takes a command and executes it.
  • If a customer process attempts to enqueue a haircut command when the queue is already full, the attempt fails. The customer process handles this failure in some graceful way, but does not wait for a space in the queue.

This describes a mechanism—a concurrent algorithm—enabling concurrent processes to ask a single, shared, sequential worker process to perform a specified work unit for them, presumably something they can’t do for themselves.  This is much more specific, and yet there are still loose ends unspecified.  Notice most problem descriptions say nothing about a haircut “command” and yet certainly this is the meaning of a customer sitting in a chair.  Notice also most problem descriptions gloss over any distinction between the customer process, a customer data object, and the haircut command.  What are these things, what creates or instantiates them, what is their lifetime?  These kinds of issues are important in the real world.  Often you don’t get to pick the answers, but rather they are fixed for your application.  A toy program that makes one set of assumptions may not be easily adapted to a different set of constraints.

Without thinking about these things too hard just yet, here is a complete program written around Dimitry’s code snippet.

package main

import (
    "fmt"
    "sync"
)

const (
    nWaitingRoomChairs = 2
    nCustomers         = 10
)

type customer int

var seats = make(chan customer, nWaitingRoomChairs)

func (c customer) enterShop() bool {
    select {
    case seats <- c:
    default:
        fmt.Println(c, "no cut")
        return false
    }
    return true
}

var wg sync.WaitGroup

func main() {
    go barber()
    wg.Add(nCustomers)
    for i := customer(1); i <= nCustomers; i++ {
        go customerGr(i)
    }
    wg.Wait()
}

func barber() {
    for {
        c := <-seats
        fmt.Println(c, "cut")
        wg.Done()
    }
}

func customerGr(c customer) {
    if !c.enterShop() {
        wg.Done()
    }
}

You can see Dmitry’s code in lines 15-25; everything else there is just to complete a working program. I’ll explore specifics of what creates what in an expanded program, but first I want to simplify this one a bit. Barber Shop problem statements often specify the “enterShop” method, but in the simple program above, it looks rather pointless. It is only called from the customerGr function, so all the code from enterShop could just be included in customerGr. The customer type then is left with no methods, and it was just an int anyway, so I think this type can be dispensed with altogether. Here’s the simplified version:

package main

import (
    "fmt"
    "sync"
)

const (
    nWaitingRoomChairs = 2
    nCustomers         = 10
)

var wg sync.WaitGroup
var wr = make(chan int, nWaitingRoomChairs)

func main() {
    go barber()
    wg.Add(nCustomers)
    for i := 1; i <= nCustomers; i++ {
        go customer(i)
    }
    wg.Wait()
}

func barber() {
    for {
        c := <-wr
        fmt.Println(c, "cut")
        wg.Done()
    }
}

func customer(i int) {
    select {
    case wr <- i:
    default:
        fmt.Println(i, "no cut")
        wg.Done()
        return
    }
}

That version may or may not be enough to get credit on a homework assignment, but it certainly doesn’t answer all the questions you might have.  I’ll present an expanded version now that is instrumented with lots of print statements to better show the goroutine interactions.  To make things clear regardless of how the scheduler schedules goroutines, I’ll add some time.Sleeps to simulate events at random times and to simulate work getting done.

package main

import (
    "log"
    "os"
    "rand"
    "sort"
    "sync"
    "time"
)

var fmt = log.New(os.Stdout, "", log.Lmicroseconds)

const (
    nWaitingRoomChairs = 2
    nCustomers         = 10
    avgCutTime         = 1e8
    shopOpenTime       = 10e8
)

type haircutCommand struct {
    customerId int
    cutDone    chan bool
}

func main() {
    rand.Seed(time.Nanoseconds())

    fmt.Println(nWaitingRoomChairs, "chairs in the waiting room")
    wr := make(chan *haircutCommand, nWaitingRoomChairs)

    go barberGr(wr)

    fmt.Println(nCustomers, "customers will arrive")
    arrivals := make([]float64, nCustomers)
    for i := range arrivals {
        arrivals[i] = rand.Float64()
    }
    sort.Float64s(arrivals)

    var wg sync.WaitGroup // wait for each customer goroutine to finish
    wg.Add(nCustomers)

    shopT0 := time.Nanoseconds()
    for i, arrivalTime := range arrivals {
        delay := int64(arrivalTime*shopOpenTime) -
            (time.Nanoseconds() - shopT0)
        if delay > 0 {
            time.Sleep(delay)
        }
        go customerGr(i+1, wr, &wg)
    }

    wg.Wait()
}

func barberGr(wr chan *haircutCommand) {
    var napStart int64
    for {
        if len(wr) == 0 {
            napStart = time.Nanoseconds()
            fmt.Println("no customers.  barber sleeps")
        }
        cmd := <-wr
        if napStart > 0 {
            fmt.Println("barber wakes.  nap:",
                time.Nanoseconds()-napStart)
            napStart = 0
        }
        fmt.Printf("barber begins cutting customer %d's hair\n",
            cmd.customerId)
        time.Sleep(avgCutTime/2 + rand.Int63n(avgCutTime))
        fmt.Printf("barber finishes customer %d\n", cmd.customerId)
        cmd.cutDone <- true
    }
}

func customerGr(i int, wr chan *haircutCommand, wg *sync.WaitGroup) {
    fmt.Println("customer", i, "arrives looking shaggy")
    defer wg.Done()
    cutDone := make(chan bool)
    select {
    case wr <- &haircutCommand{i, cutDone}:
    default:
        fmt.Println("customer", i, "sees full shop and goes away")
        return
    }
    fmt.Println("customer", i, "takes a seat")
    <-cutDone
    fmt.Println("customer", i, "leaves looking neat")
}

A couple of changes are in the general interest of code clean up. I serialize output, just to be safe, with the log package. Also main() now declares wr and wg, which were formerly package variables, and passes them to functions as needed.

I add avgCutTime and shopOpenTime to the package level const list. These values are in nanoseconds rather than simulated barber shop minutes or hours. This is convenient for passing to time.Sleep of course, and really it’s just the ratios of the these numbers in the const list that affect the output. (It’s interesting to play with them.)

I define haircutCommand as a struct type. This change illustrates the distinction I mentioned earlier between the customer object and the request for a haircut. I still represent the customer object simply as an int, although it’s easy to see that it could be a reference to some other object, potentially with fields and methods and such. The haircutCommand contains the customer object and also the bookkeeping needed to execute the command. In this case it is a channel for signaling completion of the haircut.

Within main(), one significant change is construction of the slice arrivals. I want to simulate customers arriving at random independent times, so I have main fill a slice with uniformly distributed values and then sort them to get the times at which to simulate customers arriving. main() then sequences customers by starting goroutines as indicated by the arrivals array. Doing it this way, the goroutnes are not started all at once but are staged. There will never be a time when nCustomers instances of customerGr will all be running at once.

The barber goroutine now prints a message when it sleeps. Before, this wasn’t really clear. We could read the code and see that the barber would block on the channel read, but there was no indication of this in the output. We can also now see where we could add code that might do something associated with sleeping, like maybe spinning down a disk drive and starting it back up again.

The customer goroutine creates a new cutDone channel and handles the interaction between this channel and the WaitGroup.

Within a toy program like this, it’s hard to maintain a distinction between what you are trying to demonstrate and the mechanism you are using to demonstrate it. The WaitGroup, for example, is there to keep the program from terminating before all the goroutines have completed. This seems pretty clearly part of the demonstration mechanism so I’d rather not have it integrated with either the customer object (which can otherwise be demonstrated as just an int) or the haircut command object. Still, the barber, as part of the job queuing system being demonstrated, needs to signal when the cut is complete. This is more properly done with a separate mechanism then, the cutDone channel.

Sadly, I’m leaving the distinction between the demonstrator and the demonstrated a bit muddled in the the case of the barber and the customer goroutines. Originally, we had the enterShop method, which was clearly something being demonstrated. But really, it’s just the select statement. I think it’s enough to just observe that that’s where the job queuing happens. The other thing being demonstrated there is waiting for work unit completion. Again, it’s just a single statement. Other than those two statements, customerGr is concerned with the simulation, printing messages and doing stuff with the WaitGroup.

How about the barber goroutine, where is the distinction between demonstrator and demonstrated there? Interestingly, everything there is something being demonstrated. Even the print statements can be considered placeholders for more practical functions, perhaps named enterSleepState, restoreCapabilitesAfterSleeping, and doUnitOfMeaningfulWork.

Breaking apart customerGr and barberGr into these pieces is way too much work to do for this blog post, but it should be clear by now how to do it, should you need to for some real job queuing problem, or some real homework problem.


by Sonia at September 27, 2011 02:02 AM

September 25, 2011

codegrunt.co.uk

An Update and Introducing Weak

Phew. Having had a break I have got over my mini-crisis/burnout and feel ready to get back into the wonderful world of coding.

I haven’t been keeping this blog wonderfully updated, nor have I been making great progress on my projects. So, to address this, I have refocused and committed to working on 2 projects in particular (as well as study, see below) - go, an excellent systems programming language from google, which I humbly hope to contribute to further, and weak, a chess engine I am building from scratch.

An aside - yes, lorix is being put on the sidelines. I do intend to come back to it, however it is just far too much work for me to be able to reasonably work on it at the moment. I have deleted its github page for now; however I will restore it when I get back to working on it. There is a certain irony in that I talked about my track record on promises in the lorix post, however I did say that I was not making any promises. You were warned! :-) Astute readers will note that I am in fact promising things here, however I do very much intend to keep these promises as I am very focused on these projects. Ahem!

I have also committed to following the Stanford AI and Machine Learning courses, both of which I am studying on the advanced track (i.e. voluntarily doing homework + exams). I am also studying (principally algorithmic/data structure) fundamentals with the high bar of being able to pass a google interview at some point in the future. Note this is a theoretical bar - this is no comment on whether I do or do not actually intend to attempt to execute that in practice, or whether I am in fact at all capable of mashing a keyboard let alone writing an efficient program :-)

So a busy, busy boy. I also intend to update this blog far more frequently. Translation - I intend to actually update this blog from time-to-time :-)

Weak

Weak is a chess engine, i.e. a program which plays chess. I intend to start with a very naive approach, then gradually reduce the naivety throughout until it is as efficient as I can make it. The guiding principles of weak are - a. strong play (as strong possible) and b. an obsessive focus on optimisation at all points in the code. As a result of point b, I am writing weak in C (and perhaps assembly where necessary).

Yes, I know, in many ways this seems a perfect fit for go; however, I intend weak to be as much an exercise in learning C and low-level optimal programming as just a chess engine project. As I want to absolutely control performance throughout, I want to use a language which is as close to the metal as possible. Go is an absolutely brilliant language (y’know, so much so that I’m actually trying to contribute to it), however it is still young and it takes away some control.

In general, I don’t intend to defend my programming choices on practicality grounds; this is as much a learning project as it is just a straight up coding project. I intend to, at some point, take a look at GPGPU programming with an eye to expanding weak’s capabilities. For now, however, that is merely conjecture.

I suspect that an eye towards optimisation from day 1 will have an overall positive impact on performance, as all the little optimisations add up. The endlessly paraded ‘premature optimisation is the root of all evil’ Knuth quote is somewhat misleading I think - algorithmic optimisation matters throughout, and it’s difficult to go back on poor performance choices if they are littered throughout your code and underlie everything. Having said that, I intend to steer clear of silly micro-optimisations, and also intend to profile ruthlessly to avoid guessing where performance issues arise from.

Looking Forward

I will be writing a series of blog posts as I progress with the various projects, both as a diary for myself and somewhere I can point interested people at. Turning 30 has given me a real focus on what I want to pursue, and I am very much committed to following it. What makes me considerably more confident about these goals compared to previous ones is that I’ve established a ‘head fake’ - I only permit myself to change my mind about what I pursue once a year at my birthday; in between I focus on the projects I have set for myself. This reduces procrastination and uncertainty and ensures I work hard on projects without the inevitable self-doubt and worry affecting things.

September 25, 2011 11:00 PM

September 23, 2011

Adam Langley

Chrome and the BEAST

Thai Duong and Juliano Rizzo today demoed an attack against TLS 1.0's use of cipher block chaining (CBC) in a browser environment. The authors contacted browser vendors several months ago about this and so, in order not to preempt their demo, I haven't discussed any details until now.

Contrary to several press reports, Duong and Rizzo have not found, nor do they claim, any new flaws in TLS. They have shown a concrete proof of concept for a flaw in CBC that, sadly, has a long history. Early reports of the problem date back nearly ten years ago and Bard published two papers detailing the problem.

The problem has been fixed in TLS 1.1 and a workaround for SSL 3.0 and TLS 1.0 is known, so why is this still an issue?

The workaround (prepending empty application data records) is perfectly valid according to the protocol but several buggy implementations of SSL/TLS misbehaved when it was enabled. After Duong and Rizzo notified us, we put the same workaround back into Chrome to see if the state of the Internet had improved in the years since the last attempt. Sadly it was still infeasible to implement this workaround and the change had to be reverted.

Use of TLS 1.1 would also have solved the issue but, despite it being published in 2006, common SSL/TLS libraries still don't implement it. But even if there was widespread deployment of TLS 1.1, it wouldn't have helped avoid problems like this. Due to a different, common bug in SSL 3.0 implementations (nearly 12 years after SSL 3.0 was obsoleted by TLS 1.0), browsers still have to perform SSL 3.0 downgrades to support buggy servers. So even with a TLS 1.1 capable browser and server, an attacker can trigger a downgrade to SSL 3.0 and bypass the protections of TLS 1.1.

Finally, the CBC attacks were believed to be largely theoretical but, as Duong and Rizzo have pointed out today, that's no longer the case.

Initially the authors identified HTML5 WebSockets as a viable method of exploiting the CBC weakness but, due to unrelated reasons, the WebSockets protocol was already in the process of changing in such a way that stopped it. The new WebSockets protocol shipped with Chrome 14 but several plugins have been found to offer features that might allow the attack to be performed.

Duong and Rizzo confirmed that the Java plugin can be used, but Chrome already blocks the execution of Java by default. Other plugins, if installed, can be disabled on the about:plugins page if the user wishes.

The attack is still a difficult one; the attacker has to have high-bandwidth MITM access to the victim. This is typically achieved by being on the same wireless network as the victim. None the less, it's a much less serious issue than a problem which can be exploited by having the victim merely visit a webpage. (Incidentally, we pushed out a fix to all Chrome users for such a Flash bug only a few days ago.)

Also, an attacker with MITM abilities doesn't need to implement this complex attack. SSL stripping and mixed-scripting issues are much easier to exploit and will take continued, sustained effort to address. Duong and Rizzo have highlighted this fact by choosing to attack one of the few HSTS sites.

Thanks to an idea suggested by Xuelei Fan, we have another workaround for the problem which will, hopefully, cause fewer incompatibility problems. This workaround is currently being tested on the Chrome dev and beta channels but we haven't pushed it on the stable channel yet. Since we don't really know if the fix will cause problems it's not something that we want to drop on people without testing. The benefit of a fix to Chrome's TLS stack is also limited as Chrome already uses the newer WebSockets protocol and it doesn't fix problems in plugins.

If it turns out that we've misjudged something we can quickly react, thanks to Chrome's auto-update mechanism.

It's also worth noting that Google's servers aren't vulnerable to this problem. In part due to CBC's history, Google servers have long preferred RC4, a cipher that doesn't involve CBC mode. Mention has also been made of Chrome's False Start feature but, since we don't believe that there are any vectors using Chrome's stack, that's immaterial.

September 23, 2011 07:00 AM

September 22, 2011

Sonia Codes

Algorithms are code, not types

Russ Cox wrote a very popular post on the Go-Nuts list recently in a thread titled “How can Go solve a problem that is well served by multiple inheritance?”  From a complex discussion of Dijkstra’s shortest path algorithm, multiple inheritance, constrained genericity, and generic types, Russ cut away nonsense to show how “generic” algorithms can be written with clarity in Go using interfaces.  I put generic in quotes there because Russ wrote this long post without ever using the word generic once.  The word is loaded, of course, with preconceptions from other languages which is a good reason to avoid the word in the context of Go, but just as a word in the English language, I think it describes well this style of programming that Go interfaces enable.  It’s a style that separates code from data, allowing the same piece of code to work with a whole class (English language definition) of different types of data.

Of special interest to me, the original post was prompted by a Rosetta Code task, Maze Solving.  I’ll come back to this task later, but first I’ll point out a few other RC tasks that I think are relevant.  One that popped in my mind as I was reading Russ’s post is Parametric Polymorphism.  This is a fancy computer science term and so to complete the RC task in Go I did a bit of homework first.  For a little insight into the task author’s thinking, I checked the original implementation languages.  Haskell, OCaml, and C++.  Uh huh.  Then I followed the Wikipedia links in search of some consensus on the meaning of the term and how I might interpret the concepts within Go.

Wading through all that, my conclusion seemed simple.  A parameter is an input to a function.  Parameter polymorphism is when a parameter can be of different types.  That makes it an interface type in Go.  You can pass objects of different concrete types to the function, and the function will access them generically through a common interface.  We can do that.

The task description suggests implementing a container type, a tree specifically, and a map function that traverses the tree.  Most language examples on the page managed this much, but the task also says to “write … a short bit of code that uses [the parametric type].”  This I found lacking or at least not obvious in most of the examples.  Further, it seemed to me that to really illustrate polymorphism, you should show that this “bit of code” working on multiple types, and thus that you need to actually implement more than one type.  “Poly” as a word prefix, anyone?  So, as I’ve done often on RC, I wrote more than other people wrote.  My example goes a bit farther than others, perhaps farther than the task author was thinking, but as far as I felt was necessary to fully demonstrate the task.

package main

import "fmt"

func average(c intCollection) float64 {
    var sum, count int
    c.mapElements(func(n int) {
        sum += n
        count++
    })
    return float64(sum) / float64(count)
}

func main() {
    t1 := new(binaryTree)
    t2 := new(bTree)
    a1 := average(t1)
    a2 := average(t2)
    fmt.Println("binary tree average:", a1)
    fmt.Println("b-tree average:", a2)
}

type intCollection interface {
    mapElements(func(int))
}

type binaryTree struct {
    // dummy representation details
    left, right bool
}

func (t *binaryTree) mapElements(visit func(int)) {
    // dummy implementation
    if t.left == t.right {
        visit(3)
        visit(1)
        visit(4)
    }
}

type bTree struct {
    // dummy representation details
    buckets int
}

func (t *bTree) mapElements(visit func(int)) {
    // dummy implementation
    if t.buckets >= 0 {
        visit(1)
        visit(5)
        visit(9)
    }
}

I wanted to offer “average” as an example of a generic algorithm. It is my “bit of code” for this task that is parameterized. Everyone knows the algorithm: add up all all the numbers and divide by how many there are. The generic part is that it doesn’t matter where the numbers are stored or how they are organized. All that matters is that you can get them.

Reviewing this RC task inspired me to update another, Knuth Shuffle.  The minimal task requirement is to shuffle an integer array, and so that’s what my initial implementation did.  At the time (January 2011) I didn’t bother with the “if possible, an array of any type” because hey, Go doesn’t do generics, right?  Oh, but this algorithm is so easy to write generically.

Unlike the algorithm for computing averages, this one does have some constraints about the way data is organized.  Specifically, the shuffle doesn’t really make sense unless data is organized in some kind of sequence.  It’s enough to assume that data can be indexed with an integer.  What else is needed?  The number of elements is handy; also the algorithm needs to swap data elements.  The easy way to specify this is by index.  It turns out that’s enough!  Here is what’s needed of a collection:

type shuffler interface {
    Len() int      // number of things in the collection
    Swap(i, j int) // swap the two things indexed by i and j
}

And here’s the algorithm, written generically, parametrically polymorphized, or jargon free–as a simple Go function that uses an interface.

func shuffle(s shuffler) {
    for i := s.Len() - 1; i >= 1; i-- {
        j := rand.Intn(i + 1)
        s.Swap(i, j)
    }
}

As a little aside here, note that shuffler is a subset of sort.Interface. I didn’t have to carefully craft that, or even think about it as I wrote my code, but it kind of makes sense that shuffling and sorting might have similar requirements. Really sorting just has one more, the Less method, which pays attention to the values of the elements. It kind of makes sense that shuffling doesn’t care about the values. A consequence of this is that any type you might happen to have made sortable by ensuring that it implements sort.Interface is automatically shufflable with the shuffle function and interface above. The author of Go’s sort package didn’t have to do anything special to make this possible. You with your sortable type didn’t have to do anything beyond implementing the sort interface, and yet just by including the code above for shuffle and shuffler, you can shuffle anything that you can sort. That’s reusable code.

A number of RC tasks are like Parametric Polymorphism in that they cite a computer science term and ask for examples of it. Constrained Genericity was one I enjoyed finding. I didn’t know the term, but it was plain that Go interfaces do exactly what the task asks for. Abstract type is another one. After puzzling out all the text of the task description and the WP article on the term you can finally say, “huh. So it’s just an interface.”

Returning to the Maze Solving task on RC and the “how multiple inheritance” thread on Go-Nuts, Dijkstra’s shortest path algorithm was held up as a solution and the discussion followed. One comment I liked was the observation that Dijkstra’s algorithm is overkill for the problem—it is designed for weighted graphs, and the maze problem on RC is an equal-weight problem.  Simple breadth-first search would be a fine choice.  (I might code it up soon, as after all the attention from Go-Nuts, no one has bothered to submit a solution to RC.)

Another comment I liked was that even for Dijkstra’s algorithm, a heap-based version is likely overkill.  The observation was that the set of candidate end-points under consideration at any time is likely far smaller than the number of vertices, and that a simple linear search of the candidates is likely sufficient.  Indeed I found this to be true.

I was sure I had coded up Dijkstra’s algorithm fairly recently but when I searched the RC site, I couldn’t find my code.  I finally realized I had coded it not for RC, but for Project Euler.  I located my code then, and sure enough, it used linear search.  The graph for that problem was an 80×80 grid and my program gave the answer (the minimal path from corner to corner) in 13ms.  My comments included a note that I wanted to use container/heap but had problems.  Now with a higher Go-Fu belt, I went back and added the heap optimization.  New run time?  13ms.  In the Go-Nuts thread, a 10k x 10k grid was cited.  Maybe then it would matter.  Not for 80×80 though, and certainly not for 8×11 or whatever one cares to show for the RC task.

Um, so, speaking of Project Euler, sorry for spamming people with password protected blog posts.  No one complained, but someone probably should have.  Anyway, I moved them to a separate wordpress.com blog, linked to at the top of this one.  My Dijkstra code was done for problem 83, now at (still password protected) http://gopeuler.wordpress.com/2011/09/20/pe-83/


by Sonia at September 22, 2011 11:24 PM

Go's official blog

The Go image package

Package image defines a number of types: Color and ColorModel describe colors, Point and Rectangle describe basic 2-D geometry, and Image brings the two concepts together to represent a rectangular grid of colors. A separate blog post will cover image composition with the image/draw package.

Colors and ColorModels

Color is an interface that defines the minimal method set of any type that can be considered a color: one that can be converted to red, green, blue and alpha values. The conversion may be lossy, such as converting from CMYK or YCbCr color spaces.

type Color interface {
RGBA() (r, g, b, a uint32)
}

There are three important subtleties about the return values. First, the red, green and blue are alpha-premultiplied: a fully saturated red that is also 25% transparent is represented by RGBA returning a 75% r. Second, the channels have a 16-bit effective range: 100% red is represented by RGBA returning an r of 65535, not 255, so that converting from CMYK or YCbCr is not as lossy. Third, the type returned is uint32, even though the maximum value is 65535, to guarantee that multiplying two values together won't overflow. Such multiplications occur when blending two colors according to an alpha mask from a third color, in the style of Porter and Duff's classic algebra:

dstr, dstg, dstb, dsta := dst.RGBA()
srcr, srcg, srcb, srca := src.RGBA()
_, _, _, m := mask.RGBA()
const M = 1<<16 - 1
// The resultant red value is a blend of dstr and srcr, and ranges in [0, M].
// The calculation for green, blue and alpha is similar.
dstr = (dstr*(M-m) + srcr*m) / M

The last line of that code snippet would have been more complicated if we worked with non-alpha-premultiplied colors, which is why image.Color uses alpha-premultiplied values.

The image package also defines a number of concrete color types that implement the Color interface. For example, image.RGBA is a struct that represents the classic "8 bits per channel" color.

type RGBAColor struct {
R, G, B, A uint8
}

Note that the R field of an RGBAColor is an 8-bit alpha-premultiplied color in the range [0, 255]. RGBAColor satisfies the Color interface by multiplying that value by 0x101 to generate a 16-bit alpha-premultiplied color in the range [0, 65535]. Similarly, the NRGBAColor struct type represents an 8-bit non-alpha-premultiplied color, as used by the PNG image format. When manipulating an NRGBAColor's fields directly, the values are non-alpha-premultiplied, but when calling the RGBA method, the return values are alpha-premultiplied.

A ColorModel is simply something that can convert Colors to other Colors, possibly lossily. For example, the GrayColorModel can convert any Color to a desaturated GrayColor. A PalettedColorModel can convert any Color to one from a limited palette.

type ColorModel interface {
Convert(c Color) Color
}

type PalettedColorModel []Color

Points and Rectangles

A Point is an (x, y) co-ordinate on the integer grid, with axes increasing right and down. It is neither a pixel nor a grid square. A Point has no intrinsic width, height or color, but the visualizations below use a small colored square.

type Point struct {
X, Y int
}

p := image.Point{2, 1}

A Rectangle is an axis-aligned rectangle on the integer grid, defined by its top-left and bottom-right Point. A Rectangle also has no intrinsic color, but the visualizations below outline rectangles with a thin colored line, and call out their Min and Max Points.

type Rectangle struct {
Min, Max Point
}

For convenience, image.Rect(x0, y0, x1, y1) is equivalent to image.Rectangle{image.Point{x0, y0}, image.Point{x1, y1}}, but is much easier to type.

A Rectangle is inclusive at the top-left and exclusive at the bottom-right. For a Point p and a Rectangle r, p.In(r) if and only if r.Min.X <= p.X && p.X < r.Max.X, and similarly for Y. This is analagous to how a slice s[i0:i1] is inclusive at the low end and exclusive at the high end. (Unlike arrays and slices, a Rectangle often has a non-zero origin.)

r := image.Rect(2, 1, 5, 5)
// Dx and Dy return a rectangle's width and height.
fmt.Println(r.Dx(), r.Dy(), image.Pt(0, 0).In(r)) // prints 3 4 false

Adding a Point to a Rectangle translates the Rectangle. Points and Rectangles are not restricted to be in the bottom-right quadrant.

r := image.Rect(2, 1, 5, 5).Add(image.Pt(-4, -2))
fmt.Println(r.Dx(), r.Dy(), image.Pt(0, 0).In(r)) // prints 3 4 true

Intersecting two Rectangles yields another Rectangle, which may be empty.

r := image.Rect(0, 0, 4, 3).Intersect(image.Rect(2, 2, 5, 5))
// Size returns a rectangle's width and height, as a Point.
fmt.Printf("%#v\n", r.Size()) // prints image.Point{X:2, Y:1}

Points and Rectangles are passed and returned by value. A function that takes a Rectangle argument will be as efficient as a function that takes two Point arguments, or four int arguments.

Images

An Image maps every grid square in a Rectangle to a Color from a ColorModel. "The pixel at (x, y)" refers to the color of the grid square defined by the points (x, y), (x+1, y), (x+1, y+1) and (x, y+1).

type Image interface {
// ColorModel returns the Image's ColorModel.
ColorModel() ColorModel
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
Bounds() Rectangle
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
At(x, y int) Color
}

A common mistake is assuming that an Image's bounds start at (0, 0). For example, an animated GIF contains a sequence of Images, and each Image after the first typically only holds pixel data for the area that changed, and that area doesn't necessarily start at (0, 0). The correct way to iterate over an Image m's pixels looks like:

b := m.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; y < b.Max.X; x++ {
doStuffWith(m.At(x, y))
}
}

Image implementations do not have to be based on an in-memory slice of pixel data. For example, a ColorImage is an Image of enormous bounds and uniform color, whose in-memory representation is simply that color. Similarly, a Tiled is a struct containing an Image and a Point offset.

type ColorImage struct {
C Color
}

type Tiled struct {
I Image
Offset Point
}

Typically, though, programs will want an image based on a slice. Struct types like RGBA and Gray (which other packages refer to as image.RGBA and image.Gray) hold slices of pixel data and implement the Image interface.

type RGBA struct {
// Pix holds the image's pixels, in R, G, B, A order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].
Pix []uint8
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect Rectangle
}

These types also provide a Set(x, y int, c Color) method that allows modifying the image one pixel at a time.

m := image.NewRGBA(640, 480)
m.Set(5, 5, image.RGBAColor{255, 0, 0, 255})

If you're reading or writing a lot of pixel data, it can be more efficient, but more complicated, to access these struct type's Pix field directly.

The slice-based Image implementations also provide a SubImage method, which returns an Image backed by the same array. Modifying the pixels of a sub-image will affect the pixels of the original image, analagous to how modifying the contents of a sub-slice s[i0:i1] will affect the contents of the original slice s.

m0 := image.NewRGBA(8, 5)
m1 := m0.SubImage(image.Rect(1, 2, 5, 5)).(*image.RGBA)
fmt.Println(m0.Bounds().Dx(), m1.Bounds().Dx()) // prints 8, 4
fmt.Println(m0.Stride == m1.Stride) // prints true

For low-level code that works on an image's Pix field, be aware that ranging over Pix can affect pixels outside an image's bounds. In the example above, the pixels covered by m1.Pix are shaded in blue. Higher-level code, such as the At and Set methods or the image/draw package, will clip their operations to the image's bounds.

Image Formats

The standard package library supports a number of common image formats, such as GIF, JPEG and PNG. If you know the format of a source image file, you can decode from an io.Reader directly.

import (
"image/jpeg"
"image/png"
"io"
"os"
)

// convertJPEGToPNG converts from JPEG to PNG.
func convertJPEGToPNG(w io.Writer, r io.Reader) os.Error {
img, err := jpeg.Decode(r)
if err != nil {
return err
}
return png.Encode(w, img)
}

If you have image data of unknown format, the image.Decode function can detect the format. The set of recognized formats is constructed at run time and is not limited to those in the standard package library. An image format package typically registers its format in an init function, and the main package will "underscore import" such a package solely for the side effect of format registration.

import (
"image"
"image/png"
"io"
"os"

_ "image/jpeg"
_ "vp8-go.googlecode.com/hg/webp"
)

// convertToPNG converts from any recognized format to PNG.
func convertToPNG(w io.Writer, r io.Reader) os.Error {
img, _, err := image.Decode(r)
if err != nil {
return err
}
return png.Encode(w, img)
}

By Nigel Tao, September 2011

by Andrew Gerrand (noreply@blogger.com) at September 22, 2011 09:18 PM

September 19, 2011

Adam Langley

DNSSEC Certificates now in Chrome Stable

A few months back I described DNSSEC authenticated HTTPS in Chrome. This allows sites to use DNSSEC, rather than traditional, certificates and is aimed at sites which currently use no HTTPS, or self-signed certificates. Since Chrome 14 is now stable, all Chrome users now have this experimental feature.

(Also, the serialisation format has been documented.)

September 19, 2011 07:00 AM

September 18, 2011

Command Center

User experience

[We open in a well-lit corporate conference room. A meeting has been running for a while. Lots has been accomplished but time is running out.]

[The door opens and a tall, tow-headed twenty-something guy in glasses walks in, carrying a Mac Air and a folder.]

Manager:
Oh, here he is. This is Richard. I asked him to join us today. Glad he could make it. He's got some great user experience ideas.

Richard:
Call me Dick.

Manager:
Dick's done a lot of seminal UX work for us.

Engineer:
Hey, aren't you the guy who's arguing we shouldn't have search in e-books?

Dick:
Absolutely. It's a lousy idea.

Engineer:
What?

Dick:
Books are the best UI ever created. They've been perfected over more than 500 years of development. We shouldn't mess with success.

Product manager:
Well, this is a new age. We should be allowed to ...

Dick:
Books have never had search. If we add search, we'll just confuse the user.

Product manager:
Oh, you're right. We don't want to do that.

Engineer:
But e-books aren't physical books. They're not words on paper. They're just bits, information.

Dick:
Our users don't know that.

Engineer:
Yes they do! They don't want simple books, they want the possibilities that electronic books can bring. Do you know about information theory? Have you even heard of Claude Shannon?

Dick:
Isn't he the chef at that new biodynamic tofu restaurant in North Beach?

Engineer:
Uhh, yeah, OK. But look, you're treating books as a metaphor for your user interface. That's as lame as using a trash can to throw away files and folders. We can do so much more!

Dick:
You misunderstand. Our goal is to make computers easier to use, not to make them more useful.

Product manager:
Wow, that's good.

Engineer:
Wow.

Manager:
Let's get back on track. Dick, you had some suggestions for us?

Dick:
Yeah. I was thinking about the work we did with the Notes iPhone app. Using a font that looked like a felt marker was a big help for users.

Engineer:
Seriously?

Dick:
Yes, it made users feel more comfortable about keeping notes on their phone. Having a font that looks like handwriting helps them forget there's a computer underneath.

Engineer:
I see....

Dick:
Yes, so... I was thinking for the Address Book app for Lion, we should change the look to be like a...

Manager:
Can you show us?

Dick:
Yeah, sure. I have a mock-up here.
[Opens laptop, turns it to face the room.]

Product manager:
An address book! That's fantastic. Look at the detail! Leather, seams at the corners, a visible spine. This is awesome!

Engineer:
It's just a book. It's a throwback. What are you doing? Why does it need to look like a physical address book?

Dick:
Because it is an address book!

Engineer:
No it's not, it's an app!

Dick:
It's a book.

Engineer:
You've made it one. This time it's not even a metaphor - it's literally a book. You're giving up on the possibility of doing more.

Dick:
As I said, users don't care about functionality. They want comfort and familiarity. An Address Book app that looks like an address book will be welcome. Soothing.

Engineer:
If they want a paper address book, they can buy one.

Dick:
Why would they do that if they have one on their desktop?

Engineer:
Can they at least change the appearance? Is there a setting somewhere?

Dick:
Oh, no. We know better than the user - otherwise why are we here? Settings are just confusing.

Engineer:
I ... I really don't understand what's going on.

Manager:
That's OK, you don't have to, but I'd like to give you the action item to build it. End of the quarter OK?

Engineer:
Uhhh, sure.

Manager.
Dick, do you have the requirements doc there?

Dick:
Right here.
[Pushes the folder across the desk.]

Engineer:
Can't you just mail it to me?

Dick:
It's right there.

Engineer:
I know, but... OK.

Manager:
That's a great start, Dick. What else do you have?

Dick:
Well, actually, maybe this is the time to announce that I'm moving on. Today is my last day here.

Manager, Product manager:
[Unison] Oh no!

Dick:
Yeah, sorry about that. I've had an amazing time here changing the world but it's tiem for me to seek new challenges.

Manager:
Do you have something in mind?

Dick:
Yes, I'm moving north. Microsoft has asked me to head a group there. They've got some amazing new ideas around paper clips.

FADE

by rob (noreply@blogger.com) at September 18, 2011 10:27 PM

September 17, 2011

Airs – Ian Lance Taylor

Sudoku

I’ve been playing Sudoku on Google+. i’ve more or less mastered the easy and medium levels, but it takes me about 30 minutes to do a hard level, and I haven’t tried expert yet. Sudoku is a fairly dumb game in some ways; as a colleague of mine pointed out, it’s trivial to write a computer program which will win every time. But I find the game somewhat interesting because it mirrors, in reverse, the way I think about programming.

You can write a computer program more or less any way you like. So I tend to think of a program in terms of constraints. Typical constraints are: the desired behaviour; the available runtime; the algorithmic complexity; the available libraries; the language; maintainability; who is going to review the code and what they will accept. Write a program is a matter of finding the simplest solution which meets the constraints. Difficult programming problems are ones where the constraints come into conflict, and it’s hard to see your way through.

Sudoku works the same way, only in reverse. In programming you are allowed to write any code that meets the constraints. In Sudoku you know that there is only one solution, so you have to look for moves that are forced by the constraints. Solving a Sudoku puzzle is a matter of looking deeper and deeper into the problem until you have eliminated all moves but one.

My hope is that practice in this area will subconsciously encourage me to look deeper for constraints when writing code, which will save time in the long run because I will have to throw away less code. I doubt this will actually work, but it seems worth a try.

Also Sudoku is a good way to exercise short term memory, as I’m avoiding writing anything down while solving the puzzle. I used to play cards regularly (bridge, whist) and I was able to remember the location of many of the cards in other people’s hands. I noticed that I lost that facility as I’ve failed to practice it. As i write this I realize that short term memory is not too important in today’s world, but at least it makes me feel smarter.


by Ian Lance Taylor at September 17, 2011 01:27 AM

September 15, 2011

jra's thoughts

Testing Go’s HTTP server for CVE-2011-3192 vulnerability

The recent DoS attack on Apache is caused by sending in a malformed Range header. I decided to send the same header into Go’s range header parser and see what happened. It passed with flying colors, giving the “invalid range” error, which would result in the Go webserver sending back HTTP response code 416 to the HTTP client.

I didn’t test this under load (which is part of what the DoS was doing), but my quick reading of parseRange leads me to believe that the only effect of sending Range headers in like this is that garbage is created under control of the attacker (for example, due to strings.Split(s[len(b):], “,”)). Of course, that’s bad and should be limited. However, this risk is no greater than other places in the server. For example an attacker could send an unlimited number of headers, or headers of unlimited length. This is already mentioned in the net/textproto package’s reader.go.

I did notice something strange while looking into this, which is that there’s a constant named maxHeaderLines (1024) that is never referenced. This is probably because the (incorrectly non-plurally-named) textproto.ReadMIMEHeader is now in charge of reading all the headers, but it does not accept a max headers parameter.

The fact that this particular constructed header causes no problem is no great surprise, because this attack was highly targeted at Apache. But it is nice to see that Go’s webserver is already (somewhat) hardened against this type of attack.

PS: Here’s a diff showing how I tested this. It might be interesting for beginner Go folks to see how to generate test data at test time.

diff --git a/src/pkg/http/range_test.go b/src/pkg/http/range_test.go
--- a/src/pkg/http/range_test.go
+++ b/src/pkg/http/range_test.go
@@ -6,13 +6,16 @@

 import (
 	"testing"
+	"fmt"
 )

-var ParseRangeTests = []struct {
+type testDef struct {
 	s      string
 	length int64
 	r      []httpRange
-}{
+}
+
+var ParseRangeTests = []testDef{
 	{"", 0, nil},
 	{"foo", 0, nil},
 	{"bytes=", 0, nil},
@@ -34,6 +37,16 @@
 	{"bytes=500-700,601-999", 10000, []httpRange{{500, 201}, {601, 399}}},
 }

+func init() {
+  var p string
+  for k:=0 ; k<1300 ; k++ {
+	p += ",5-"
+	p += fmt.Sprintf("%d", k)
+  }
+  x := "bytes=0-" + p
+  ParseRangeTests = append(ParseRangeTests, testDef{ x, 10, []httpRange{} })
+}
+
 func TestParseRange(t *testing.T) {
 	for _, test := range ParseRangeTests {
 		r := test.r

As set up right now, it won’t pass. This is because I wanted to see the exact error it was returning. If you change the []httpRange{} to nil, it will pass.

PPS: Looks like someone else has been thinking about this as well.

by jra at September 15, 2011 07:53 AM

September 07, 2011

golang.jp

翻訳更新のお知らせ

いくつか日本語訳を更新しました。

by noboru at September 07, 2011 08:59 AM

Adam Langley

Why not Convergence?

In light of recent events, I've had several requests to implement Convergence in Chrome. For those who don't know and, frankly, for anyone interested in SSL, I highly recommend watching Moxie's talk on the subject from this year's Black Hat. You can also check out the project website.

Moxie, having actually thought about the issue and coded something up, has already done a thousand times more to address the problem than almost anyone else. But I don't think that Convergence is something we would add in Chrome:

Although the idea of trust agility is great, 99.99% of Chrome users would never change the default settings. (The percentage is not an exaggeration.) Indeed, I don't believe that an option for setting custom notaries would even meet the standards for inclusion in the preferences UI.

Given that essentially the whole population of Chrome users would use the default notary settings, those notaries will get a large amount of traffic. Also, we have a very strong interest for the notaries to function, otherwise Chrome stops working. Combined, that means that Google would end up running the notaries. So the design boils down to Chrome phoning home for certificate validation. That has both unacceptable privacy implications and very high uptime requirements on the notary service.

It also doesn't address the two problems that Moxie highlights: internal servers and captive portals. It's not clear how either would work in this design, at least without giving up on security and asking the user. (These two problems, captive portals esp, are the bane of many an idea in this area.)

None of the above argues against allowing Convergence as an extension for those who wish to run it. We don't currently have an extension API for controlling certificate decisions and I'm not inherently opposed to one. It would be additional complexity and something that we would have to support in the future, so it's not without costs, but mostly it's not there because nobody has written it and I'm afraid that I don't have any plans to do so.

September 07, 2011 07:00 AM

September 06, 2011

Go's official blog

Error handling and Go

If you have written any Go code you have probably encountered the os.Error type. Go code uses os.Error values to indicate an abnormal state. For example, the os.Open function returns a non-nil os.Error value when it fails to open a file.

func Open(name string) (file *File, err Error)

The following function uses os.Open to open a file. If an error occurs it calls log.Fatal to print the error message and stop.

func main() {
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
// do something with the open *File f
}

You can get a lot done in Go knowing just this about os.Error, but in this article we'll take a closer look at os.Error and discuss some good practices for error handling in Go.

The Error type

The os.Error type is an interface type. An os.Error variable represents any value that can describe itself as a string. Here is the interface’s declaration:

package os

type Error interface {
String() string
}

There's nothing special about os.Error. It is just a widely used convention.

The most common os.Error implementation is the os package's unexported errorString type.

type errorString string

func (s errorString) String() string { return string(s) }

You can construct one of these values with the os.NewError function. It takes a string that it converts to an os.errorString and returns as an os.Error value.

func NewError(s string) Error { return errorString(s) }

Here's how you might use os.NewError:

func Sqrt(f float64) (float64, os.Error) {
if f < 0 {
return 0, os.NewError("math: square root of negative number")
}
// implementation
}

A caller passing a negative argument to Sqrt receives a non-nil os.Error value (whose concrete representation is an os.errorString value). The caller can access the error string ("math: square root of...") by calling the os.Error's String method, or by just printing it:

f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
}

The fmt package can print anything with a "String() string" method, which includes os.Error values.

It is the error implementation’s responsibility to summarize the context. The error returned by os.Open formats as "open /etc/passwd: permission denied," not just "permission denied." The error returned by our Sqrt is missing information about the invalid argument.

To add that information, a useful function is the fmt package's Errorf. It formats a string according to Printf's rules and returns it as an os.Error created by os.NewError.

if f < 0 {
return 0, fmt.Errorf("math: square root of negative number %g", f)
}

In many cases fmt.Errorf is good enough, but because os.Error is an interface, you can use elaborate data structures as error values, to allow callers to inspect the details of the error.

For instance, our hypothetical callers might want to recover the invalid argument passed to Sqrt. We can enable that by defining a new error implementation instead of using os.errorString:

type NegativeSqrtError float64

func (f NegativeSqrtError) String() string {
return fmt.Sprintf(“math: square root of negative number %g”, float64(f))
}

A sophisticated caller can then use a type assertion to check for a NegativeSqrtError and handle it specially, while callers that just pass the error to fmt.Println or log.Fatal will see no change in behavior.

As another example, the json package specifies a SyntaxError type that the json.Decode function returns when it encounters a syntax error parsing a JSON blob.

type SyntaxError struct {
msg string // description of error
Offset int64 // error occurred after reading Offset bytes
}

func (e *SyntaxError) String() string { return e.msg }

The Offset field isn’t even shown in the default formatting of the error, but callers can use it to add file and line information to their error messages:

if err := dec.Decode(&val); err != nil {
if serr, ok := err.(*json.SyntaxError); ok {
line, col := findLine(f, serr.Offset)
return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
}
return err
}

(This is a slightly simplified version of some actual code from the Camlistore project.)

The os.Error interface requires only a String method; specific error implementations might have additional methods. For instance, the net package returns errors of type os.Error, following the usual convention, but some of the error implementations have additional methods defined by the net.Error interface:

package net

type Error interface {
os.Error
Timeout() bool // Is the error a timeout?
Temporary() bool // Is the error temporary?
}

Client code can test for a net.Error with a type assertion and then distinguish transient network errors from permanent ones. For instance, a web crawler might sleep and retry when it encounters a temporary error and give up otherwise.

if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(1e9)
continue
}
if err != nil {
log.Fatal(err)
}

Simplifying repetitive error handling

In Go, error handling is important. The language's design and conventions encourage you to explicitly check for errors where they occur (as distinct from the convention in other languages of throwing exceptions and sometimes catching them). In some cases this makes Go code verbose, but fortunately there are some techniques you can use to minimize repetitive error handling.

Consider an App Engine application with an HTTP handler that retrieves a record from the datastore and formats it with a template.

func init() {
http.HandleFunc("/view", viewRecord)
}

func viewRecord(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key := datastore.NewKey("Record", r.FormValue("id"), 0, nil)
record := new(Record)
if err := datastore.Get(c, key, record); err != nil {
http.Error(w, err.String(), 500)
return
}
if err := viewTemplate.Execute(w, record); err != nil {
http.Error(w, err.String(), 500)
}
}

This function handles errors returned by the datastore.Get function and viewTemplate's Execute method. In both cases, it presents a simple error message to the user with the HTTP status code 500 ("Internal Server Error"). This looks like a manageable amount of code, but add some more HTTP handlers and you quickly end up with many copies of identical error handling code.

To reduce the repetition we can define our own HTTP appHandler type that includes an <ode>os.Error</ode> return value:

type appHandler func(http.ResponseWriter, *http.Request) os.Error

Then we can change our viewRecord function to return errors:

func viewRecord(w http.ResponseWriter, r *http.Request) os.Error {
c := appengine.NewContext(r)
key := datastore.NewKey("Record", r.FormValue("id"), 0, nil)
record := new(Record)
if err := datastore.Get(c, key, record); err != nil {
return err
}
return viewTemplate.Execute(w, record)
}

This is simpler than the original version, but the http package doesn't understand functions that return os.Error. To fix this we can implement the http.Handler interface's ServeHTTP method on appHandler:

func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := fn(w, r); err != nil {
http.Error(w, err.String(), 500)
}
}

The ServeHTTP method calls the appHandler function and displays the returned error (if any) to the user. Notice that the method’s receiver, fn, is a function. (Go can do that!) The method invokes the function by calling the receiver in the expression fn(w, r).

Now when registering viewRecord with the http package we use the Handle function (instead of HandleFunc) as appHandler is an http.Handler (not an http.HandlerFunc).

func init() {
http.Handle("/view", appHandler(viewRecord))
}

With this basic error handling infrastructure in place, we can make it more user friendly. Rather than just displaying the error string, it would be better to give the user a simple error message with an appropriate HTTP status code, while logging the full error to the App Engine developer console for debugging purposes.

To do this we create an appError struct containing an os.Error and some other fields:

type appError struct {
Error os.Error
Message string
Code int
}

Next we modify the appHandler type to return *appError values:

type appHandler func(http.ResponseWriter, *http.Request) *appError

(It's usually a mistake to pass back the concrete type of an error rather than os.Error, for reasons to be discussed in an upcoming post, but it's the right thing to do here because ServeHTTP is the only place that sees the value and uses its contents.)

And make appHandler's ServeHTTP method display the appError's Message to the user with the correct HTTP status Code and log the full Error to the developer console:

func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil { // e is *appError, not os.Error.
c := appengine.NewContext(r)
c.Errorf("%v", e.Error)
http.Error(w, e.Message, e.Code)
}
}

Finally, we update viewRecord to the new function signature and have it return more context when it encounters an error:

func viewRecord(w http.ResponseWriter, r *http.Request) *appError {
c := appengine.NewContext(r)
key := datastore.NewKey("Record", r.FormValue("id"), 0, nil)
record := new(Record)
if err := datastore.Get(c, key, record); err != nil {
return &appError{err, "Record not found", 404}
}
if err := viewTemplate.Execute(w, record); err != nil {
return &appError{err, "Can't display record", 500}
}
return nil
}

This version of viewRecord is the same length as the original, but now each of those lines has specific meaning and we are providing a friendlier user experience.

It doesn't end there; we can further improve the error handling in our application. Some ideas:
  • give the error handler a pretty HTML template,
  • make debugging easier by writing the stack trace to the HTTP response when the user is an administrator,
  • write a constructor for appError that stores the stack trace for easier debugging,
  • recover from panics inside the appHandler, logging the error to the console as "Critical," while simply telling the user "a serious error has occurred." This is a nice touch to avoid exposing the user to inscrutable error messages caused by programming errors. See the Defer, Panic, and Recover article for more details.

Conclusion

Proper error handling is an essential requirement of good software. By employing the techniques described in this post you should be able to write more reliable and succinct Go code.

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:54 PM

Go for App Engine is now generally available

The Go and App Engine teams are excited to announce that the Go runtime for App Engine is now generally available. This means you can take that Go app you've been working on (or meaning to work on) and deploy it to App Engine right now with the new 1.5.2 SDK.

Since we announced the Go runtime at Google I/O we have continued to improve and extend Go support for the App Engine APIs and have added the Channels API. The Go Datastore API now supports transactions and ancestor queries, too. See the Go App Engine documentation for all the details.

For those who have been using the Go SDK already, please note that the 1.5.2 release introduces api_version 2. This is because the new SDK is based on Go release.r58.1 (the current stable version of Go) and is not backwards compatible with the previous release. Existing apps may require changes as per the r58 release notes. Once you've updated your code, you should redeploy your app with the line 'api_version: 2' in its app.yaml file. Apps written against api_version 1 will stop working after the 18th of August.

Finally, we owe a huge thanks to our trusted testers and their many bug reports. Their help was invaluable in reaching this important milestone.

The fastest way to get started with Go on App Engine is with the Getting Started guide.

Note that the Go runtime is still considered experimental; it is not as well-supported as the Python and Java runtimes.

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:53 PM

"First Class Functions in Go" and new Go course notes

We would like to announce some new and revised Go learning materials.

Programmers new to Go are often surprised by its support for function types, functions as values, and closures. The First Class Functions in Go code walk demonstrates these features with a simulation of the dice game Pig. It is a pretty program that uses the language to great effect, and a fun read for Go beginners and veterans alike.

The Go course is a three part series that describes the language in detail, including the basic syntax, type system, and concurrency primitives. These notes were available at launch in 2009, but since then the language has changed a bit and the course notes had become an outdated and unreliable resource. We recently revised all three slide decks in their entirety, updating them to match contemporary Go syntax and semantics. The course is ideal for those who want to get the full picture and a great resource for teaching the language to others.

These resources and many more are available at golang.org.

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:42 PM

Profiling Go Programs

At Scala Days 2011 a few weeks ago, Robert Hundt presented a paper titled “Loop Recognition in C++/Java/Go/Scala.” The paper implemented a specific loop finding algorithm, such as you might use in a flow analysis pass of a compiler, in C++, Go, Java, Scala, and then used those programs to draw conclusions about typical performance concerns in these languages. The Go program presented in that paper runs quite slowly, making it an excellent opportunity to demonstrate how to use Go's profiling tools to take a slow program and make it faster.

By using Go's profiling tools to identify and correct specific bottlenecks, we can make the Go loop finding program run an order of magnitude faster and use 6x less memory.

Hundt's paper does not specify which versions of the C++, Go, Java, and Scala tools he used. In this blog post, we will be using the most recent weekly snapshot of the 6g Go compiler and the version of g++ that ships with the Ubuntu Natty distribution. (We will not be using Java or Scala, because we are not skilled at writing efficient programs in either of those languages, so the comparison would be unfair. Since C++ was the fastest language in the paper, the comparisons here with C++ should suffice.)

$ 6g -V
6g version weekly.2011-06-16 8787
$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
...
$

The programs are run on a Lenovo X201s with a 2.13 GHz Core i7-based CPU and 4 GB of RAM running Ubuntu Natty's Linux 2.6.38-8-generic kernel. The machine is running with CPU frequency scaling disabled via

$ sudo bash
# for i in /sys/devices/system/cpu/cpu[0-9]
do
echo performance > $i/cpufreq/scaling_governor
done
#

We've taken Hundt's benchmark programs in C++ and Go, combined each into a single source file, and removed all but one line of output. We'll time the program using Linux's time utility with a format that shows user time, system time, real time, and maximum memory usage:

$ cat xtime
#!/bin/sh
/usr/bin/time -f '%Uu %Ss %er %MkB %C' "$@"
$

$ make havlak1cc
g++ -O3 -o havlak1cc havlak1.cc
$ xtime havlak1cc
# of loops: 76002 (total 3800100)
loop-0, nest: 0, depth: 0
27.37u 0.08s 27.47r 716864kB havlak1cc
$

$ make havlak1
6g havlak1.go
6l -o havlak1 havlak1.6
$ xtime havlak1
# of loops: 76000 (including 1 artificial root node)
56.63u 0.26s 56.92r 1642640kB havlak1
$

The C++ program runs in 27.47 seconds and uses 700 MB of memory. The Go program runs in 56.92 seconds and uses 1604 MB of memory. (These measurements are difficult to reconcile with the ones in the paper, but the point of this post is to explore how to use gopprof, not to reproduce the results from the paper.)

To start tuning the Go program, we have to enable profiling. If the code used the Go testing package's benchmarking support, we could use gotest's standard -cpuprofile and -memprofile flags. In a standalone program like this one, we have to import runtime/pprof and add a few lines of code:

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
...

The new code defines a flag named cpuprofile, calls the Go flag library to parse the command line flags, and then, if the cpuprofile flag has been set on the command line, starts CPU profiling redirected to that file. The profiler requires a final call to StopCPUProfile to flush any pending writes to the file before the program exits; we use defer to make sure this happens as main returns.

After adding that code, we can run the program with the new -cpuprofile flag and then run gopprof to interpret the profile.

$ make havlak1.prof
havlak1 -cpuprofile=havlak1.prof
# of loops: 76000 (including 1 artificial root node)
$ gopprof havlak1 havlak1.prof
Welcome to pprof! For help, type 'help'.
(pprof)

The gopprof program is a slight variant of Google's pprof C++ profiler. The most important command is topN, which shows the top N samples in the profile:

(pprof) top10
Total: 5758 samples
1028 17.9% 17.9% 1161 20.2% hash_lookup
696 12.1% 29.9% 697 12.1% scanblock
565 9.8% 39.8% 1042 18.1% hash_insert_internal
420 7.3% 47.0% 4278 74.3% main.FindLoops
225 3.9% 51.0% 1149 20.0% main.DFS
225 3.9% 54.9% 226 3.9% memhash
198 3.4% 58.3% 437 7.6% sweep
172 3.0% 61.3% 1902 33.0% runtime.mallocgc
102 1.8% 63.1% 500 8.7% runtime.MCache_Alloc
102 1.8% 64.8% 102 1.8% runtime.memmove
(pprof)

When CPU profiling is enabled, the Go program stops about 100 times per second and records a sample consisting of the program counters on the currently executing goroutine's stack. The profile has 5758 samples, so it was running for a bit over 57 seconds. In the gopprof output, there is a row for each function that appeared in a sample. The first two columns show the number of samples in which the function was running (as opposed to waiting for a called function to return), as a raw count and as a percentage of total samples. The hash_lookup function was running during 1028 samples, or 17.9%. The top10 output is sorted by this sample count. The third column shows the running total during the listing: the first three rows account for 39.8% of the samples. The fourth and fifth columns show the number of samples in which the function appeared (either running or waiting for a called function to return). The main.FindLoops function was running in 7.3% of the samples, but it was on the call stack (it or functions it called were running) in 74.3% of the samples.

To sort by the fourth and fifth columns, use the -cum (for cumulative) flag:

(pprof) top5 -cum
Total: 5758 samples
0 0.0% 0.0% 4301 74.7% main.main
0 0.0% 0.0% 4301 74.7% runtime.initdone
0 0.0% 0.0% 4301 74.7% runtime.mainstart
0 0.0% 0.0% 4278 74.3% main.FindHavlakLoops
420 7.3% 7.3% 4278 74.3% main.FindLoops
(pprof)

In fact the total for main.FindLoops and main.main should have been 100%, but each stack sample only includes the bottom 100 stack frames; during about a quarter of the samples, the recursive main.DFS function was more than 100 frames deeper than main.main so the complete trace was truncated.

The stack trace samples contain more interesting data about function call relationships than the text listings can show. The web command writes a graph of the profile data in SVG format and opens it in a web browser. (There is also a gv command that writes PostScript and opens it in Ghostview. For either command, you need graphviz installed.)

(pprof) web

A small fragment of the full graph looks like:



Each box in the graph corresponds to a single function, and the boxes are sized according to the number of samples in which the function was running. An edge from box X to box Y indicates that X calls Y; the number along the edge is the number of times that call appears in a sample. If a call appears multiple times in a single sample, such as during recursive function calls, each appearance counts toward the edge weight. That explains the 69206 on the self-edge from main.DFS to itself.

Just at a glance, we can see that the program spends much of its time in hash operations, which correspond to use of Go's map values. We can tell web to use only samples that include a specific function, such as hash_lookup, which clears some of the noise from the graph:

(pprof) web hash_lookup



If we squint, we can see that the calls to runtime.mapaccess1 are being made by main.FindLoops and main.DFS.

Now that we have a rough idea of the big picture, it's time to zoom in on a particular function. Let's look at main.DFS first, just because it is a shorter function:

(pprof) list DFS
Total: 5758 samples
ROUTINE ====================== main.DFS in /home/rsc/g/benchgraffiti/havlak/havlak1.go
samples 225 Total 2296 (flat / cumulative)
3 3 240: func DFS(currentNode *BasicBlock, nodes []*UnionFindNode, number map[*BasicBlock]int, last []int, current int) int {
18 19 241: nodes[current].Init(currentNode, current)
. 166 242: number[currentNode] = current
. . 243:
2 2 244: lastid := current
167 167 245: for _, target := range currentNode.OutEdges {
17 508 246: if number[target] == unvisited {
10 1157 247: lastid = DFS(target, nodes, number, last, lastid+1)
. . 248: }
. . 249: }
7 273 250: last[number[currentNode]] = lastid
1 1 251: return lastid
. . 252: }
(pprof)

The listing shows the source code for the DFS function (really, for every function matching the regular expression DFS). The first three columns are the number of samples taken while running that line, the number of samples taken while running that line or in code called from that line, and the line number in the file. The related command disasm shows a disassembly of the function instead of a source listing; when there are enough samples this can help you see which instructions are expensive. The weblist command mixes the two modes: it shows a source listing in which clicking a line shows the disassembly.

Since we already know that the time is going into map lookups implemented by the hash runtime functions, we care most about the second column. A large fraction of time is spent in recursive calls to DFS (line 247), as would be expected from a recursive traversal. Excluding the recursion, it looks like the time is going into the accesses to the number map on lines 242, 246, and 250. For that particular lookup, a map is not the most efficient choice. Just as they would be in a compiler, the basic block structures have unique sequence numbers assigned to them. Instead of using a map[*BasicBlock]int we can use a []int, a slice indexed by the block number. There's no reason to use a map when an array or slice will do.

Changing number from a map to a slice requires editing seven lines in the program and cut its run time by nearly a factor of two:

$ make havlak2
6g havlak2.go
6l -o havlak2 havlak2.6
rm havlak2.6
$ xtime havlak2 # diff from havlak1
# of loops: 76000 (including 1 artificial root node)
30.88u 0.24s 31.14r 1564608kB havlak2
$

We can run the profiler again to confirm that main.DFS is no longer a significant part of the run time:

$ make havlak2.prof
havlak2 -cpuprofile=havlak2.prof
# of loops: 76000 (including 1 artificial root node)
$ gopprof havlak2 havlak2.prof
Welcome to pprof! For help, type 'help'.
(pprof) top5
Total: 3099 samples
626 20.2% 20.2% 626 20.2% scanblock
309 10.0% 30.2% 2839 91.6% main.FindLoops
176 5.7% 35.9% 1732 55.9% runtime.mallocgc
173 5.6% 41.4% 397 12.8% sweep
101 3.3% 44.7% 111 3.6% main.DFS
(pprof)

main.DFS still appears in the profile, but its total time has dropped from 20.0% to 3.6%. The rest of the program runtime has dropped too. Now the program is spending most of its time allocating memory and garbage collecting (runtime.mallocgc, which both allocates and runs periodic garbage collections, accounts for 55.9% of the time). To find out why the garbage collector is running so much, we have to find out what is allocating memory. One way is to add memory profiling to the program. We'll arrange that if the -memprofile flag is supplied, the program stops after one iteration of the loop finding, writes a memory profile, and exits:

var memprofile = flag.String("memprofile", "", "write memory profile to this file")
...

FindHavlakLoops(cfgraph, lsgraph)
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}


We invoke the program with -memprofile flag to write a profile:

$ make havlak3.mprof # diff from havlak2
havlak3 -memprofile=havlak3.mprof
$

We use gopprof exactly the same way. Now the samples we are examining are memory allocations, not clock ticks.

$ gopprof havlak3 havlak3.mprof
Adjusting heap profiles for 1-in-524288 sampling rate
Welcome to pprof! For help, type 'help'.
(pprof) top5
Total: 118.3 MB
66.1 55.8% 55.8% 103.7 87.7% main.FindLoops
30.5 25.8% 81.6% 30.5 25.8% main.*LSG·NewLoop
10.0 8.5% 90.1% 10.0 8.5% main.NewBasicBlock
6.5 5.5% 95.6% 6.5 5.5% main.*SimpleLoop·AddNode
2.1 1.7% 97.3% 12.1 10.2% main.*CFG·CreateNode
(pprof)

Gopprof reports that FindLoops has allocated approximately 66.1 of the 118.3 MB in use; NewLoop accounts for another 30.5 MB. To reduce overhead, the memory profiler only records information for approximately one block per half megabyte allocated (the “1-in-524288 sampling rate”), so these are approximations to the actual counts.

To find the memory allocations, we can list those functions.

(pprof) list FindLoops
Total: 118.3 MB
ROUTINE ====================== main.FindLoops in /home/rsc/g/benchgraffiti/havlak/havlak3.go
MB 66.1 Total 103.7 (flat / cumulative)
...
. . 267:
1.9 1.9 268: nonBackPreds := make([]map[int]bool, size)
3.8 3.8 269: backPreds := make([][]int, size)
. . 270:
1.0 1.0 271: number := make([]int, size)
1.0 1.0 272: header := make([]int, size, size)
1.0 1.0 273: types := make([]int, size, size)
1.0 1.0 274: last := make([]int, size, size)
1.9 1.9 275: nodes := make([]*UnionFindNode, size, size)
. . 276:
. . 277: for i := 0; i < size; i++ {
5.5 5.5 278: nodes[i] = new(UnionFindNode)
. . 279: }
...
. . 286: for i, bb := range cfgraph.Blocks {
. . 287: number[bb.Name] = unvisited
48.0 48.0 288: nonBackPreds[i] = make(map[int]bool)
. . 289: }
...
(pprof) list NewLoop
Total: 118.3 MB
ROUTINE ====================== main.*LSG·NewLoop in /home/rsc/g/benchgraffiti/havlak/havlak3.go
. . 578: func (lsg *LSG) NewLoop() *SimpleLoop {
2.5 2.5 579: loop := new(SimpleLoop)
7.5 7.5 580: loop.basicBlocks = make(map[*BasicBlock]bool)
20.5 20.5 581: loop.Children = make(map[*SimpleLoop]bool)
...
. . 588: }
(pprof)

It looks like the current bottleneck is the same as the last one: using maps where simpler data structures suffice. FindLoops is allocating about 48 MB of maps, and NewLoop is allocating another 20 MB.

As an aside, if we run gopprof with the --inuse_objects flag, it will report allocation counts instead of sizes:


$ gopprof --inuse_objects havlak3 havlak3.mprof
Adjusting heap profiles for 1-in-524288 sampling rate
Welcome to pprof! For help, type 'help'.
(pprof) list NewLoop
Total: 1604080 objects
ROUTINE ====================== main.*LSG·NewLoop in /home/rsc/g/benchgraffiti/havlak/havlak3.go
. . 578: func (lsg *LSG) NewLoop() *SimpleLoop {
54613 54613 579: loop := new(SimpleLoop)
75678 75678 580: loop.basicBlocks = make(map[*BasicBlock]bool)
207530 207530 581: loop.Children = make(map[*SimpleLoop]bool)
...
. . 588: }
(pprof)

Since the 200,000 maps account for 20 MB, it looks like the initial map allocation takes about 100 bytes. That's reasonable when a map is being used to hold key-value pairs, but not when a map is being used as a stand-in for a simple set, as it is here.

Instead of using a map, we can use a simple slice to list the elements. In all but one of the cases where maps are being used, it is impossible for the algorithm to insert a duplicate element. In the one remaining case, we can write a simple variant of the append built-in function:

func appendUnique(a []int, x int) []int {
for _, y := range a {
if x == y {
return a
}
}
return append(a, x)
}

In addition to writing that function, changing the Go program to use slices instead of maps requires changing just a few lines of code.


$ xtime havlak4 # diff from havlak3
# of loops: 76000 (including 1 artificial root node)
18.35u 0.11s 18.48r 575792kB havlak4
$

We're now at 3x faster than when we started. Let's look at a CPU profile again.


$ gopprof havlak4 havlak4.prof
Welcome to pprof! For help, type 'help'.
(pprof) top10
Total: 1851 samples
283 15.3% 15.3% 283 15.3% scanblock
233 12.6% 27.9% 1622 87.6% main.FindLoops
142 7.7% 35.5% 1054 56.9% runtime.mallocgc
112 6.1% 41.6% 276 14.9% sweep
111 6.0% 47.6% 115 6.2% main.DFS
85 4.6% 52.2% 661 35.7% runtime.growslice
84 4.5% 56.7% 84 4.5% runtime.memmove
69 3.7% 60.5% 281 15.2% runtime.MCache_Alloc
67 3.6% 64.1% 84 4.5% MCentral_Alloc
67 3.6% 67.7% 93 5.0% MCentral_Free
(pprof)

Now memory allocation and the consequent garbage collection (runtime.mallocgc) accounts for 56.9% of our run time. Another way to look at why the system is garbage collecting is to look at the allocations that are causing the collections, the ones that spend most of the time in mallocgc:


(pprof) web mallocgc



It's hard to tell what's going on in that graph, because there are many nodes with small sample numbers obscuring the big ones. We can tell gopprof to ignore nodes that don't account for at least 10% of the samples:


$ gopprof --nodefraction=0.1 6.out prof
Welcome to pprof! For help, type 'help'.
(pprof) web mallocgc



We can follow the thick arrows easily now, to see that FindLoops is triggering most of the garbage collection. If we list FindLoops we can see that much of it is right at the beginning:


(pprof) list FindLoops
. . 270: func FindLoops(cfgraph *CFG, lsgraph *LSG) {
. . 271: if cfgraph.Start == nil {
. . 272: return
. . 273: }
. . 274:
. . 275: size := cfgraph.NumNodes()
. . 276:
. 17 277: nonBackPreds := make([][]int, size)
. 82 278: backPreds := make([][]int, size)
. . 279:
. 2 280: number := make([]int, size)
. 1 281: header := make([]int, size, size)
. 61 282: types := make([]int, size, size)
. . 283: last := make([]int, size, size)
. 58 284: nodes := make([]*UnionFindNode, size, size)
. . 285:
2 2 286: for i := 0; i < size; i++ {
. 261 287: nodes[i] = new(UnionFindNode)
. . 288: }
...
(pprof)

Every time FindLoops is called, it allocates some sizable bookkeeping structures. Since the benchmark calls FindLoops 50 times, these add up to a significant amount of garbage, so a significant amount of work for the garbage collector.

Having a garbage-collected language doesn't mean you can ignore memory allocation issues. In this case, a simple solution is to introduce a cache so that each call to FindLoops reuses the previous call's storage when possible. (In fact, in Hundt's paper, he explains that the Java program needed just this change to get anything like reasonable performance, but he did not make the same change in the other garbage-collected implementations.)

We'll add a global cache structure:

var cache struct {
size int
nonBackPreds [][]int
backPreds [][]int
number []int
header []int
types []int
last []int
nodes []*UnionFindNode
}

and then have FindLoops consult it as a replacement for allocation:



if cache.size < size {
cache.size = size
cache.nonBackPreds = make([][]int, size)
cache.backPreds = make([][]int, size)
cache.number = make([]int, size)
cache.header = make([]int, size)
cache.types = make([]int, size)
cache.last = make([]int, size)
cache.nodes = make([]*UnionFindNode, size)
for i := range cache.nodes {
cache.nodes[i] = new(UnionFindNode)
}
}

nonBackPreds := cache.nonBackPreds[:size]
for i := range nonBackPreds {
nonBackPreds[i] = nonBackPreds[i][:0]
}
backPreds := cache.backPreds[:size]
for i := range nonBackPreds {
backPreds[i] = backPreds[i][:0]
}
number := cache.number[:size]
header := cache.header[:size]
types := cache.types[:size]
last := cache.last[:size]
nodes := cache.nodes[:size]

Such a global variable is bad engineering practice, of course: it means that concurrent calls to FindLoops are now unsafe. For now, we are making the minimal possible changes in order to understand what is important for the performance of our program; this change is simple and mirrors the code in the Java implementation. The final version of the Go program will use a separate LoopFinder instance to track this memory, restoring the possibility of concurrent use.

$ xtime havlak5 # diff from havlak4
# of loops: 76000 (including 1 artificial root node)
12.59u 0.07s 12.67r 584496kB havlak5
$

There's more we can do to clean up the program and make it faster, but none of it requires profiling techniques that we haven't already shown. The work list used in the inner loop can be reused across iterations and across calls to FindLoops, and it can be combined with the separate “node pool” generated during that pass. Similarly, the loop graph storage can be reused on each iteration instead of reallocated. In addition to these performance changes, the final version is written using idiomatic Go style, using data structures and methods. The stylistic changes have only a minor effect on the run time: the algorithm and constraints are unchanged.

The final version runs in 3.84 seconds and uses 257 MB of memory:

$ xtime havlak6
# of loops: 76000 (including 1 artificial root node)
3.79u 0.04s 3.84r 263472kB havlak6
$

That's nearly 15 times faster than the program we started with. Even if we disable reuse of the generated loop graph, so that the only cached memory is the loop finding bookeeping, the program still runs 10x faster than the original and uses 2.5x less memory.

$ xtime havlak6 -reuseloopgraph=false
# of loops: 76000 (including 1 artificial root node)
5.74u 0.10s 5.84r 617040kB havlak6 -reuseloopgraph=false
$

Of course, it's no longer fair to compare this Go program to the original C++ program, which used inefficient data structures like sets where vectors would be more appropriate. As a sanity check, we translated the final Go program into equivalent C++ code. Its execution time is similar to the Go program's:

$ xtime havlak6cc
# of loops: 76000 (including 1 artificial root node)
4.04u 0.38s 4.42r 387744kB havlak6cc
$

The Go program runs slightly faster because the C++ program is using automatic deletes and allocation instead of an explicit cache. That makes the C++ program a bit shorter and easier to write, but not dramatically so:

$ wc havlak6.cc; wc havlak6.go
401 1220 9040 havlak6.cc
461 1441 9467 havlak6.go
$

Benchmarks are only as good as the programs they measure. We used gopprof to study an inefficient Go program and then to improve its performance by an order of magnitude and to reduce its memory usage by a factor of six. A subsequent comparison with an equivalently optimized C++ program shows that Go can be competitive with C++ when programmers are careful about how much garbage is generated by inner loops.

The program sources, Linux x86-64 binaries, and profiles used to write this post are available in the benchgraffiti project on Google Code.

As mentioned above, gotest includes these profiling flags already: define a benchmark function and you're all set. There is also a standard HTTP interface to profiling data. In an HTTP server, adding

import _ "http/pprof"

will install handlers for a few URLs under /debug/pprof/. Then you can run gopprof with a single argument—the URL to your server's profiling data—and it will download and examine a live profile.

gopprof http://localhost:6060/debug/pprof/profile # 30-second CPU profile
gopprof http://localhost:6060/debug/pprof/heap # heap profile

- Russ Cox

by rsc (noreply@blogger.com) at September 06, 2011 11:42 PM

Spotlight on external Go libraries

While the Go authors have been working hard at improving Go's standard library, the greater community has created a growing ecosystem of external libraries. In this post we look at some popular Go libraries and how they can be used.

Mgo (pronounced "mango") is a MongoDB database driver. MongoDB is a document-oriented database with a long list of features suitable for a broad range of uses. The mgo package provides a rich, idiomatic Go API for working with MongoDB, from basic operations such as inserting and updating records to the more advanced MapReduce and GridFS features. Mgo has a bunch of cool features including automated cluster discovery and result pre-fetching - see the mgo homepage for details and example code. For working with large data sets Go, MongoDB, and mgo are a powerful combination.

Authcookie is a web library for generating and verifying user authentication cookies. It allows web servers to hand out cryptographically secure tokens tied to a specific user that will expire after a specified time period. It has a simple API that makes it straightforward to add authentication to existing web applications. See the README file for details and example code.

Go-charset provides support for converting between Go's standard UTF-8 encoding and a variety of character sets. The go-charset package implements a translating io.Reader and io.Writer so you can wrap existing Readers and Writers (such as network connections or file descriptors), making it easy to communicate with systems that use other character encodings.

Go-socket.io is a Go implementation of Socket.IO, a client/server API that allows web servers to push messages to web browsers. Depending on the capabilities of the user's browser, Socket.IO uses the best transport for the connection, be it modern websockets, AJAX long polling, or some other mechanism. Go-socket.io bridges the gap between Go servers and rich JavaScript clients for a wide range of browsers. To get a feel for go-socket.io see the chat server example.

It's worth mentioning that these packages are goinstallable. With an up-to-date Go installation you can install them all with a single command:
 
goinstall launchpad.net/mgo \
github.com/dchest/authcookie \
go-charset.googlecode.com/hg/charset \
github.com/madari/go-socket.io

Once goinstalled, the packages can be imported using those same paths:
 
import (
"launchpad.net/mgo"
"github.com/dchest/authcookie"
"go-charset.googlecode.com/hg/charset"
"github.com/madari/go-socket.io"
)

Also, as they are now a part of the local Go system, we can inspect their documentation with godoc:
 
godoc launchpad.net/mgo Database # get docs for the Database type

Of course, this is just the tip of the iceberg; there are more great Go libraries listed on the package dashboard and many more to come.

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:42 PM

Two Go Talks: "Lexical Scanning in Go" and "Cuddle: an App Engine Demo"

On Tuesday night Rob Pike and Andrew Gerrand each presented at the Sydney Google Technology User Group.

Rob's talk, "Lexical Scanning in Go", discusses the design of a particularly interesting and idiomatic piece of Go code, the lexer component of the new template package.

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="345" src="http://www.youtube.com/embed/HxaD_trXwRE" width="560"></iframe>

The slides are available here. The new template package is available as exp/template in Go release r59. In a future release it will replace the old template package.

Andrew's talk, "Cuddle: an App Engine Demo", describes the construction of a simple real-time chat application that uses App Engine's Datastore, Channel, and Memcache APIs. It also includes a question and answer session that covers Go for App Engine and Go more generally.

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="345" src="http://www.youtube.com/embed/HQtLRqqB-Kk" width="560"></iframe>

The slides are available here. The code is available at the cuddle Google Code project.

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:41 PM

The Laws of Reflection

Reflection in computing is the ability of a program to examine its own structure, particularly through types; it's a form of metaprogramming. It's also a great source of confusion.

In this article we attempt to clarify things by explaining how reflection works in Go. Each language’s reflection model is different (and many languages don’t support it at all), but this article is about Go, so for the rest of this article the word "reflection" should be taken to mean "reflection in Go".

Types and interfaces

Because reflection builds on the type system, let's start with a refresher about types in Go.

Go is statically typed. Every variable has a static type, that is, exactly one type known and fixed at compile time: int, float32, *MyType, []byte, and so on. If we declare

type MyInt int
var i int
var j MyInt

then i has type int and j has type MyInt. The variables i and j have distinct static types and, although they have the same underlying type, they cannot be assigned to one another without a conversion.

One important category of type is interface types, which represent fixed sets of methods. An interface variable can store any concrete (non-interface) value as long as that value implements the interface’s methods. A well-known pair of examples is io.Reader and io.Writer, the types Reader and Writer from the io package:

// Reader is the interface that wraps the basic Read method.
type Reader interface {
Read(p []byte) (n int, err os.Error)
}

// Writer is the interface that wraps the basic Write method.
type Writer interface {
Write(p []byte) (n int, err os.Error)
}

Any type that implements a Read (or Write) method with this signature is said to implement io.Reader (or io.Writer). For the purposes of this discussion, that means that a variable of type io.Reader can hold any value whose type has a Read method:

var r io.Reader
r = os.Stdin
r = bufio.NewReader(r)
r = new(bytes.Buffer)
// and so on

It's important to be clear that whatever concrete value r may hold, r's type is always io.Reader: Go is statically typed and the static type of r is io.Reader.

An extremely important example of an interface type is the empty interface:

interface{}

It represents the empty set of methods and is satisfied by any value at all, since any value has zero or more methods.

Some people say that Go's interfaces are dynamically typed, but that is misleading. They are statically typed: a variable of interface type always has the same static type, and even though at run time the value stored in the interface variable may change type, that value will always satisfy the interface.

We need to be precise about all this because reflection and interfaces are closely related.

The representation of an interface

Russ Cox has written a detailed blog post about the representation of interface values in Go. It's not necessary to repeat the full story here, but a simplified summary is in order.

A variable of interface type stores a pair: the concrete value assigned to the variable, and that value’s type descriptor. To be more precise, the value is the underlying concrete data item that implements the interface and the type describes the full type of that item. For instance, after

var r io.Reader
tty, err = os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil { return nil, err }
r = tty

r contains, schematically, the (value, type) pair, (tty, *os.File). Notice that the type *os.File implements methods other than Read; even though the interface value provides access only to the Read method, the value inside carries all the type information about that value. That's why we can do things like this:

var w io.Writer
w = r.(io.Writer)

The expression in this assignment is a type assertion; what it asserts is that the item inside r also implements io.Writer, and so we can assign it to w. After the assignment, w will contain the pair (tty, *os.File). That's the same pair as was held in r. The static type of the interface determines what methods may be invoked with an interface variable, even though the concrete value inside may have a larger set of methods.

Continuing, we can do this:

var empty interface{}
empty = w

and our empty interface value e will again contain that same pair, (tty, *os.File). That's handy: an empty interface can hold any value and contains all the information we could ever need about that value.

(We don't need a type assertion here because it's known statically that w satisfies the empty interface. In the example where we moved a value from a Reader to a Writer, we needed to be explicit and use a type assertion because Writer’s methods are not a subset of Reader’s.)

One important detail is that the pair inside an interface always has the form (value, concrete type) and cannot have the form (value, interface type). Interfaces do not hold interface values.

Now we're ready to reflect.

The first law of reflection

1. Reflection goes from interface value to reflection object.

At the basic level, reflection is just a mechanism to examine the type and value pair stored inside an interface variable. To get started, there are two types we need to know about in package reflect: Type and Value. Those two types give access to the contents of an interface variable, and two simple functions, called reflect.TypeOf and reflect.ValueOf, retrieve reflect.Type and reflect.Value pieces out of an interface value. (Also, from the reflect.Value it’s easy to get to the reflect.Type, but let’s keep the Value and Type concepts separate for now.)

Let's start with TypeOf:

package main

import (
"fmt"
"reflect"
)

func main() {
var x float64 = 3.4
fmt.Println("type:", reflect.TypeOf(x))
}

This program prints

type: float64

You might be wondering where the interface is here, since the program looks like it’s passing the float64 variable x, not an interface value, to reflect.TypeOf. But it's there; as godoc reports, the signature of reflect.TypeOf includes an empty interface:

// TypeOf returns the reflection Type of the value in the interface{}.
func TypeOf(i interface{}) Type

When we call reflect.TypeOf(x), x is first stored in an empty interface, which is then passed as the argument; reflect.TypeOf unpacks that empty interface to recover the type information.

The reflect.ValueOf function, of course, recovers the value (from here on we'll elide the boilerplate and focus just on the executable code):

var x float64 = 3.4
fmt.Println("value:", reflect.ValueOf(x))

prints

value: <float64 Value>

Both reflect.Type and reflect.Value have lots of methods to let us examine and manipulate them. One important example is that Value has a Type method that returns the Type of a reflect.Value. Another is that both Type and Value have a Kind method that returns a constant indicating what sort of item is stored: Uint, Float64, Slice, and so on. Also methods on Value with names like Int and Float let us grab values (as int64 and float64) stored inside:

var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.Type())
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
fmt.Println("value:", v.Float())

prints

type: float64
kind is float64: true
value: 3.4

There are also methods like SetInt and SetFloat but to use them we need to understand settability, the subject of the third law of reflection, discussed below.

The reflection library has a couple of properties worth singling out. First, to keep the API simple, the "getter" and "setter" methods of Value operate on the largest type that can hold the value: int64 for all the signed integers, for instance. That is, the Int method of Value returns an int64 and the SetInt value takes an int64; it may be necessary to convert to the actual type involved:

var x uint8 = 'x'
v := reflect.ValueOf(x)
fmt.Println("type:", v.Type()) // uint8.
fmt.Println("kind is uint8: ", v.Kind() == reflect.Uint8) // true.
x = uint8(v.Uint()) // v.Uint returns a uint64.

The second property is that the Kind of a reflection object describes the underlying type, not the static type. If a reflection object contains a value of a user-defined integer type, as in

type MyInt int
var x MyInt = 7
v := reflect.ValueOf(x)

the Kind of v is still reflect.Int, even though the static type of x is MyInt, not int. In other words, the Kind cannot discriminate an int from a MyInt even though the Type can.

The second law of reflection

2. Reflection goes from reflection object to interface value.

Like physical reflection, reflection in Go generates its own inverse.

Given a reflect.Value we can recover an interface value using the Interface method; in effect the method packs the type and value information back into an interface representation and returns the result:

// Interface returns v's value as an interface{}.
func (v Value) Interface() interface{}

As a consequence we can say

y := v.Interface().(float64) // y will have type float64.
fmt.Println(y)

to print the float64 value represented by the reflection object v.

We can do even better, though. The arguments to fmt.Println, fmt.Printf and so on are all passed as empty interface values, which are then unpacked by the fmt package internally just as we have been doing in the previous examples. Therefore all it takes to print the contents of a reflect.Value correctly is to pass the result of the Interface method to the formatted print routine:

fmt.Println(v.Interface())

(Why not fmt.Println(v)? Because v is a reflect.Value; we want the concrete value it holds.) Since our value is a float64, we can even use a floating-point format if we want:

fmt.Printf("value is %7.1e\n", v.Interface())

and get in this case

3.4e+00

Again, there's no need to type-assert the result of v.Interface() to float64; the empty interface value has the concrete value's type information inside and Printf will recover it.

In short, the Interface method is the inverse of the ValueOf function, except that its result is always of static type interface{}.

Reiterating: Reflection goes from interface values to reflection objects and back again.

The third law of reflection

3. To modify a reflection object, the value must be settable.

The third law is the most subtle and confusing, but it's easy enough to understand if we start from first principles.

Here is some code that does not work, but is worth studying.

var x float64 = 3.4
v := reflect.ValueOf(x)
v.SetFloat(7.1) // Error: will panic.

If you run this code, it will panic with the cryptic message

panic: reflect.Value.SetFloat using unaddressable value

The problem is not that the value 7.1 is not addressable; it's that v is not settable. Settability is a property of a reflection Value, and not all reflection Values have it.

The CanSet method of Value reports the settability of a Value; in our case,

var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("settability of v:" , v.CanSet())

prints

settability of v: false

It is an error to call a Set method on an non-settable Value. But what is settability?

Settability is a bit like addressability, but stricter. It's the property that a reflection object can modify the actual storage that was used to create the reflection object. Settability is determined by whether the reflection object holds the original item. When we say

var x float64 = 3.4
v := reflect.ValueOf(x)

we pass a copy of x to reflect.ValueOf, so the interface value created as the argument to reflect.ValueOf is a copy of x, not x itself. Thus, if the statement

v.SetFloat(7.1)

were allowed to succeed, it would not update x, even though v looks like it was created from x. Instead, it would update the copy of x stored inside the reflection value and x itself would be unaffected. That would be confusing and useless, so it is illegal, and settability is the property used to avoid this issue.

If this seems bizarre, it's not. It's actually a familiar situation in unusual garb. Think of passing x to a function:

f(x)

We would not expect f to be able to modify x because we passed a copy of x's value, not x itself. If we want f to modify x directly we must pass our function the address of x (that is, a pointer to x):

f(&x)

This is straightforward and familiar, and reflection works the same way. If we want to modify x by reflection, we must give the reflection library a pointer to the value we want to modify.

Let’s do that. First we initialize x as usual and then create a reflection value that points to it, called p.

var x float64 = 3.4
p := reflect.ValueOf(&x) // Note: take the address of x.
fmt.Println("type of p:", p.Type())
fmt.Println("settability of p:" , p.CanSet())

The output so far is

type of p: *float64
settability of p: false

The reflection object p isn't settable, but it's not p we want to set, it's (in effect) *p. To get to what p points to, we call the Elem method of Value, which indirects through the pointer, and save the result in a reflection Value called v:

v := p.Elem()
fmt.Println("settability of v:" , v.CanSet())

Now v is a settable reflection object, as the output demonstrates,

settability of v: true

and since it represents x, we are finally able to use v.SetFloat to modify the value of x:

v.SetFloat(7.1)
fmt.Println(v.Interface())
fmt.Println(x)

The output, as expected, is

7.1
7.1

Reflection can be hard to understand but it's doing exactly what the language does, albeit through reflection Types and Values that can disguise what's going on. Just keep in mind that reflection Values need the address of something in order to modify what they represent.

Structs

In our previous example v wasn't a pointer itself, it was just derived from one. A common way for this situation to arise is when using reflection to modify the fields of a structure. As long as we have the address of the structure, we can modify its fields.

Here's a simple example that analyzes a struct value, t. We create the reflection object with the address of the struct because we'll want to modify it later. Then we set typeOfT to its type and iterate over the fields using straightforward method calls (see package reflect for details). Note that we extract the names of the fields from the struct type, but the fields themselves are regular reflect.Value objects.

type T struct {
A int
B string
}
t := T{23, "skidoo"}
s := reflect.ValueOf(&t).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}

The output of this program is

0: A int = 23
1: B string = skidoo

There’s one more point about settability introduced in passing here: the field names of T are upper case (exported) because only exported fields of a struct are settable.

Because s contains a settable reflection object, we can modify the fields of the structure.

s.Field(0).SetInt(77)
s.Field(1).SetString("Sunset Strip")
fmt.Println("t is now", t)

And here's the result:

t is now {77 Sunset Strip}

If we modified the program so that s was created from t, not &t, the calls to SetInt and SetString would fail as the fields of t would not be settable.

Conclusion

Here again are the laws of reflection:

  1. Reflection goes from interface value to reflection object.
  2. Reflection goes from reflection object to interface value.
  3. To modify a reflection object, the value must be settable.

Once you understand these laws reflection in Go becomes much easier to use, although it remains subtle. It’s a powerful tool that should be used with care and avoided unless strictly necessary.

There's plenty more to reflection that we haven't covered — sending and receiving on channels, allocating memory, using slices and maps, calling methods and functions — but this post is long enough. We'll cover some of those topics in a later article.

By Rob Pike, September 2011

by Andrew Gerrand (noreply@blogger.com) at September 06, 2011 11:40 PM

September 05, 2011

Sonia Codes

Protected: PE 60

This post is password protected. You must visit the website and enter the password to continue reading.


by Sonia at September 05, 2011 12:33 AM

September 04, 2011

Sonia Codes

Protected: PE 42

This post is password protected. You must visit the website and enter the password to continue reading.


by Sonia at September 04, 2011 10:20 PM

September 01, 2011

RSC

_rsc: @kurrik See also goinstall goauth2.googlecode.com/hg/oauth

_rsc: @kurrik See also goinstall goauth2.googlecode.com/hg/oauth

September 01, 2011 03:23 PM

August 31, 2011

codegrunt.co.uk

Turning 30

I’ve not updated this for an age. I have been going through something of a mini-crisis/case of burnout, somewhat related to the fact I’m turning 30 tomorrow. Scary times!

August 31, 2011 11:00 PM

Adam Langley

False Start: past time to fix your servers

A year ago I wrote about False Start in Chrome. In short: Chrome cuts the TLS handshake short to save a round trip in a full handshake. Since then we've posted results that show a 30% drop in SSL handshake latency.

When we enabled False Start by default in Chrome, we also included a list of the very small number of incompatible sites. This list was built into the browser in order to avoid breaking these sites. (See the original post for the reasoning.)

For some time I've been randomly eliminating chunks of that list. Mostly it's been the case that sites have already upgraded. I don't think that they did so specifically with False Start in mind, but that it was just a regular maintainance.

But it's now time that all sites are updated because the list is fading away fast:

  • If you run A10 SSL terminators, ensure that you have firmware >= 2.4.3-p4
  • If you run Brocade SSL terminators, ensure that you have firmware >= 10.2.01y
  • If you run F5 SSL terminators, you need to be running the native SSL stack (which is the default, as opposed to the `compat' stack)
  • If you run FTMG SSL terminators, you need Service Pack 2

August 31, 2011 07:00 AM

August 28, 2011

Miek Gieben

Learning Go for E-readers

Thanks to a patch from Thomas Kappler I can now offer two types of PDFs, one for A4 pages and one for E-readers, like the kindle.

The E-reader variant is suffixed with -kindle:

by Miek Gieben at August 28, 2011 07:45 PM

August 26, 2011

Airs – Ian Lance Taylor

Pay Voting

New plan: let’s let people pay to vote. Everybody gets one vote free, just like today. You can also pay, say, $1000 for another vote, then $2000 for the next one, $3000 for the one after that, etc. Also, you can sell your vote, so a cheaper way to get more votes is to pay a bunch of people $500.

Advantage: it’s no longer necessary to give candidates money so that they can advertise for votes. Currently politicians spend at least half their time asking for money. This would let them spend all their time on their actual job.

Advantage: money paid for votes goes straight to the treasury, rather than to television stations.

Advantage: Many fewer horrible political ads.

Disadvantage: politicians do what rich people want them to do. But wait, that is already the case. So this isn’t a disadvantage at all.

The U.S. is already a plutocracy. Making it explicit is more efficient all around.


by Ian Lance Taylor at August 26, 2011 05:12 AM

August 25, 2011

Going Along

公路惊魂

昨晚睡的还好, 起的是有点早, 忙了整天, 开在高速上很顺, 就有些恍惚. 满脑袋胡思乱想, 就这样突然一大片不知什么东西, 从前面的卡车上掉下来, 擦过一辆摩托, 在路上三翻五滚就冲了过来. 脑子一片空白, 下意识的做了个规避动作, 让那个不知什么的东西从两个前轮间钻过不见了. 车咣当了一下, 看看后视镜, 找找好像没有什么东西甩出去. 正纳闷到底是什么, 前面的摩托骑士频频回头, 才感觉是那什么东西挂在了车下. 赶紧打灯急忙靠路肩停下. 看着侧镜等着一辆辆车飞驰过去了, 才敢下来查看. 一个巨大的沙发靠背, 就这样紧紧的夹在车底, 被拖着磨破了套, 棉絮四溢. 夹的那个紧那怎么也拽不出, 只好动用千斤顶, 汗流浃背满手油污的把车撑起. 好大的一个棉靠背.

突然. 这才刚回过神. 刚才是多么凶险的几秒钟. 如果我不是迎头撞上去, 而是紧急刹车, 或闪到旁边车道, 可能就是连环车祸. 如果不是正好夹在两轮中间, 而是被车轮碾过, 时速100的车很可能会翻. 如果不是棉垫子而是个硬家伙, 如果是砸在前面的摩托上, 如果是其他的如果...... 现在的结果可是意外中的最佳结果, 不幸中的万幸了! 旦夕祸福, 惊魂卜定, 阿弥陀佛.

人说眼一闭不睁一辈子过去了. 那还死得其所睡的安详. 我这可是差点双眼睁睁的就送了命. 如果, 意外, 经历过的, 更珍惜生命了.

by Fango (noreply@blogger.com) at August 25, 2011 02:08 PM

August 23, 2011

Command Center

Regular expressions in lexing and parsing

Comments extracted from a code review. I've been asked to disseminate them more widely.

I should say something about regular expressions in lexing and
parsing. Regular expressions are hard to write, hard to write well,
and can be expensive relative to other technologies. (Even when they
are implemented correctly in N*M time, they have significant
overheads, especially if they must capture the output.)

Lexers, on the other hand, are fairly easy to write correctly (if not
as compactly), and very easy to test. Consider finding alphanumeric
identifiers. It's not too hard to write the regexp (something like
"[a-ZA-Z_][a-ZA-Z_0-9]*"), but really not much harder to write as a
simple loop. The performance of the loop, though, will be much higher
and will involve much less code under the covers. A regular expression
library is a big thing. Using one to parse identifiers is like using a
Ferrari to go to the store for milk.

And when we want to adjust our lexer to admit other character types,
such as Unicode identifiers, and handle normalization, and so on, the
hand-written loop can cope easily but the regexp approach will break
down.

A similar argument applies to parsing. Using regular expressions to
explore the parse state to find the way forward is expensive,
overkill, and error-prone. Standard lexing and parsing techniques are
so easy to write, so general, and so adaptable there's no reason to
use regular expressions. They also result in much faster, safer, and
compact implementations.

Another way to look at it is that lexers and parsing are matching
statically-defined patterns, but regular expressions' strength is that
they provide a way to express patterns dynamically. They're great in
text editors and search tools, but when you know at compile time what
all the things are you're looking for, regular expressions bring far
more generality and flexibility than you need.

Finally, on the point about writing well. Regular expressions are, in
my experience, widely misunderstood and abused. When I do code reviews
involving regular expressions, I fix up a far higher fraction of the
regular expressions in the code than I do regular statements. This is
a sign of misuse: most programmers (no finger pointing here, just
observing a generality) simply don't know what they are or how to use
them correctly.

Encouraging regular expressions as a panacea for all text processing
problems is not only lazy and poor engineering, it also reinforces
their use by people who shouldn't be using them at all.

So don't write lexers and parsers with regular expressions as the
starting point. Your code will be faster, cleaner, and much easier to
understand and to maintain.

by rob (noreply@blogger.com) at August 23, 2011 02:38 AM

August 22, 2011

Andrew Gerrand

PyconAU, Go, and a bit of l'esprit de l'escalier

Yesterday I went to Pycon Australia in Sydney. It was a great conference; well-organized, great speakers, and a fun crowd. I used to write a lot of Python code professionally, so it was both interesting and rewarding for me to get back in touch with the Python community.

I signed up for one of the lightning talk slots at the end of the day, with the contentious title “Love Python? Try Go!” When I asked a friend if it was inappropriate to give a talk about Go at a Python conference, he replied “Giving your talk without pants on is inappropriate. Talking about Go is just obscene!” Undeterred, I prepared and delivered my talk anyway (see the slides).

The talk was surprisingly well-received (I didn’t get booed off the stage). There was the requisite heckling from my friends and colleagues in the front row, but when they judged each of the talks by applause I got a reasonable showing (I expected stony silence).

My talk sparked a number of conversations at the pub afterwards. Were I to do the talk again there are some things I would have done differently:

  • I wouldn’t make such a big deal about static typing.

It turns out that Python programmers like dynamic typing. A lot. Python’s run time typing errors just don’t seem to be a big deal to most Python programmers.

This surprised me, because my number one peeve with dynamic languages is how fragile the code feels. In Go, I can fearlessly refactor code with the confidence that most silly mistakes will be caught by the compiler. I have never had the same confidence when writing Python code.

One counter-argument is that good tests will catch those same kinds of errors. This is true to some extent, but I contend that static type checking saves time during develpoment, too. Unless you write your tests up front (I don’t) you won’t get that important benefit in a dynamic language.

But I digress. The “static vs dynamic typing” discussion is deep and subtle, which makes it a poor choice for a 5-minute lightning talk. I should have left it out.

  • I would have somebody thoroughly proofread my slides.

On the slide about fast compilation I stated that “Typical programs build in <1 second,” except I used a greater-than sign by mistake. That got a laugh, at least, even if I didn’t know why at the time.

  • I would make the point that programming languages are not a religion and that language choice is not a zero-sum game.

Learning a new programming language does not mean you forget the other ones; it just makes you a better programmer by giving you more options and experience.

The intent of my talk was not to coax programmers away from Python to Go. I wanted to tell people about Go and show that it offers some real benefits over other languages for some tasks. While I personally prefer to use Go over other languages for pretty much anything, I certainly don’t expect that everyone should feel the same. I’m not a Go zealot (or a troll), and I hope I didn’t come across that way. :–)

Software systems are mostly heterogeneous, particularly in the web world. For example, Python programs consist of a lot of C code, and most Python web apps are deployed behind a web server written in C. Were I giving the talk again, I would focus on the benefits of adding Go to your software development toolset, and give some real examples of systems that use both Python and Go.

So, in conclusion, if you’re a Pythonista with the need for speed and/or concurrency – or you’re just interested in learning something quite different and new – I (once more) encourage you to try Go. It may not replace Python in your day-to-day life but – like any new tool – it might just make your life easier.

Good starting points are my Real World Go talk and the Go documentation. And, if you’re in Sydney, come along to the Sydney GTUG on Tuesday the 30th of August where Rob Pike and I will be giving a couple of Go presentations.

Permalink | Leave a comment  »

August 22, 2011 12:54 AM

August 19, 2011

RSC

_rsc: RT @dylanbeattie: The 11 in C++11 refers to the number of legs that have now been nailed onto the dog whilst attempting to build a bette ...

_rsc: RT @dylanbeattie: The 11 in C++11 refers to the number of legs that have now been nailed onto the dog whilst attempting to build a bette ...

August 19, 2011 02:49 AM

_rsc: @__nil works great with sshfs. On a mac see macfuse

_rsc: @__nil works great with sshfs. On a mac see macfuse

August 19, 2011 02:47 AM

August 15, 2011

Journal

How to parse command line arguments in Go

Your Go program can easily grab command line arguments. 

Let's say your program prints a person's name that gets passed into the program as a command line argument.

<script src="https://gist.github.com/1145626.js?file=gistfile1.go"></script>

To make this happen, you need to import the "flag" package. Next you need to declare a variable that you will store the flag's value in.

I chose to declare a string variable called "value" for this purpose.

With a variable set, use the flag.StringVar(...) function to set the value (assuming the value you wish to parse is a string). Your other option is to use the flag.IntVar(...) function to bind an integer variable with an integer command line argument.

Note that the flag.StringVar function accepts a reference to a string variable, not a pointer. This reference is used internally by the flag package. Also, not that if you don't specify a command line flag, this function sets one up for you with the default name "The Dude".

Finally, execute flag's Parse() function, and either the flag passed in from the command line, or a default flag will be ready for use.

by Charles Thompson at August 15, 2011 02:59 AM

August 13, 2011

golang.jp

「Goプログラミング言語仕様」翻訳更新のお知らせ

Goプログラミング言語仕様」の翻訳を更新しました。
バージョンは、June 17, 2011です。

あまり大きな変更はありませんんが、変更点です。

  • gotoの制約が変更され、ブロック外からブロック内へのジャンプができなくなった
  • nilチャネルからの送受信信は、永久にブロックされるようになった。
    ただし、コンパイラはまだ未対応で、ブロックされずにパニックが発生
  • nilマップは、要素が追加できない以外は空のマップと同じ扱いになった。
    これもコンパイラはまだ未対応。

by noboru at August 13, 2011 01:20 AM

August 12, 2011

Miek Gieben

VIM setup

After several years I decided to use a different color scheme for VIM. Also I'm going for force myself to use VIM's folding abilities and use make from within VIM.

For good measure I also want to use Omni-completion when writing Go code:

omni completion screenshot

Btw, this screenshots also shows the solarized (dark) colorscheme.

Coloring

Google for solarized. In my .vimrc:

let g:solarized_termcolors=256
colorscheme solarized

Make from VIM

Use :make inside the editor and jump through the errors with:

:cn         // next compile error
:cp         // previous compile error

There are more options, but I want to be able to remember them...

Folding

Settings in .vimrc:

" folding settings
set foldmethod=indent
set foldnestmax=10
set nofoldenable
set foldlevel=0

And the commands that I will probably use most often:

zM          // close all folds
zR          // open all folds

za          // toggle fold under cursor
zA          // toggle fold under cursor recursively

Other important ones:

zo          // open fold under cursor
zO          // open all under cursor recursively

zc          // close fold under cursor
zC          // close all under cursor recursively

Spell checking

Setting in .vimrc:

" toggle spelling control-E -> en, control-N -> dutch (nederlands)                   
map     <C-E>    :setlocal spell! spelllang=en<CR>
imap    <C-E>    <ESC>:setlocal spell! spelllang=en<CR>i
map     <C-N>    :setlocal spell! spelllang=nl<CR>
imap    <C-N>    <ESC>:setlocal spell! spelllang=nl<CR>i

Commands to use (this is the only one I use)

z=          // Show corrections for a word

Toggle switches

Switches for paste-mode, cursorline, numbering disable search highlighting.

set pastetoggle=<F7>

" search hilight
map     <F8>   :nohlsearch<CR>
imap    <F8>   <ESC>:nohlsearch<CR>a
vmap    <F8>   <ESC>:nohlsearch<CR>gv

" numbering
map     <F10>   :set nu!<CR>
imap    <F10>   <ESC>:set nu!<CR>i
vmap    <F10>   <ESC>:set nu!<CR>gv

" toggle cursorline
map     <F9>   :set cursorline!<CR>
imap    <F9>   <ESC>:set cursorline!<CR>

Omni completion (Go specific)

See https://github.com/nsf/gocode. After you've installed that, you can use control-X control-O to bring up the omni completion window. With the VIM files in go/misc/vim you also have the following commands:

    :Fmt                // Gofmt your code
    :Import strings     // add package 'strings' to the import list
    :Drop strings       // drop package 'strings' from the import list

by Miek Gieben at August 12, 2011 08:04 AM

Going Along

Windows with coLinux for Go App Engine SDK


  1. Download coLinux from http://www.colinux.org
  2. Download Ubuntu 9.04 1GB RootFS
  3. Download 7-zip and uncompress Ubuntu-9.04-1gb.7z
  4. Create colinux.bat as "colinux-daemon kernel=vmlinux initrd=initrd.gz root=/dev/cobd0 ro cobd0=Ubuntu-9.04.ext3.1gb.fs eth0=tuptap, run it
  5. Windows should have a Local Area Connection with Device Name (TAP-Win32), this is the tuptap network interface. Now we need to double click the one for Ethernet Adaptor, click 'Properties', then 'Advanced'. Under Internet Connection Sharing (if you have), select the TAP-Win32 above for sharing.
  6. Login: root, Password: root
  7. apt-get update
  8. apt-get install curl unzip
  9. adduser go
  10. sudo - go
  11. curl -o gosdk.zip http://googleappengine.googlecode.com/files/go_appengine_sdk_linux_386-1.5.2.zip
  12. unzip gosdk.zip
  13. cd google_appengine
  14. /sbin/ifconfig eth0, notice inet addr, mine is 192.168.0.88 
  15. ./dev_appserver.py -a 192.168.0.88 demos/go-helloworld/
  16. From windows, browse URL http://192.168.0.88:8080
  17. Optionally, add -d after colinux-daemon in colinux.bat to put it to daemon mode, and use Putty.exe to remote login via SSH




by Fango (noreply@blogger.com) at August 12, 2011 06:01 AM

August 10, 2011

Sonia Codes

Readers Writers

My attention was drawn to the Producer-consumer problem last week reading Allen B. Downey’s The Little Book of Semaphores.  It contains descriptions and solutions of a number of interesting concurrency problems in the form of exercises or what I call “toy” problems.  The second problem posed in the book is the Readers-writers problem.  Like Producer-consumer, it is trivially solved in Go, as Go has a nice RWMutex object in the sync package.  Downey and Wikipedia both describe in detail various solutions with different problems of writer starvation or reader starvation.  I can spare you the pain of working through the examples by just saying that the current Go RWMutex avoids both writer and reader starvation, and better, is wait-free for readers.  It was updated recently by Dmitry Vyukov, who has been making some great contributions to Go recently.  His 1024 cores website and corresponding blog are packed with amazing insights into concurrency.

Here is a little demo program showing the RWMutex in action:

package main

import (
    "log"
    "os"
    "rand"
    "sync"
    "time"
)

// thread safe output
var fmt = log.New(os.Stdout, "", log.Lmicroseconds)

// type representing shared resource.  contents could be anything
type resource struct {
    string
}

// struct wraps pointer to resource with embedded RWMutex, thus
// acquiring RWMutex methods
type access struct {
    sync.RWMutex
    res  *resource
}

func main() {
    acc := &access{res: &resource{"zero"}}
    fmt.Println("Initial value:", acc.res.string)

    gr := new(sync.WaitGroup)
    gr.Add(5) // three readers and two writers
    go reader("A", acc, gr)
    go reader("B", acc, gr)
    go reader("C", acc, gr)
    go writer("X", acc, gr)
    go writer("Y", acc, gr)
    gr.Wait()
    fmt.Println("Final value:", acc.res.string)
}

// reader reads three times at about .1 second intervals, taking
// .02 second to perform the read
func reader(name string, acc *access, gr *sync.WaitGroup) {
    rn := rand.New(rand.NewSource(time.Nanoseconds()))
    for i := 0; i < 3; i++ {
        time.Sleep(8e7 + rn.Int63n(4e7))
        fmt.Println("reader", name, "ready to read")
        acc.RLock()
        fmt.Println("reader", name, "reading")
        time.Sleep(2e7)
        msg := acc.res.string
        acc.RUnlock()
        fmt.Println("reader", name, "read:", msg)
    }
    gr.Done()
}

// writer writes twice
func writer(name string, acc *access, gr *sync.WaitGroup) {
    rn := rand.New(rand.NewSource(time.Nanoseconds()))
    claim := []string{"once", "again"}
    for i := 0; i < 2; i++ {
        time.Sleep(8e7 + rn.Int63n(4e7))
        msg := name + " was here " + claim[i]
        fmt.Println("writer", name, "ready to write")
        acc.Lock()
        fmt.Println("writer", name, "writing:", msg)
        time.Sleep(2e7)
        acc.res.string = msg
        acc.Unlock()
        fmt.Println("writer", name, "done")
    }
    gr.Done()
}

Sample output:

15:48:39.518475 Initial value: zero
15:48:39.610595 reader B ready to read
15:48:39.610617 reader B reading
15:48:39.629587 writer X ready to write
15:48:39.629616 reader C ready to read
15:48:39.630584 reader A ready to read
15:48:39.631582 reader B read: zero
15:48:39.631601 writer X writing: X was here once
15:48:39.639580 writer Y ready to write
15:48:39.652591 writer X done
15:48:39.652609 reader C reading
15:48:39.652618 reader A reading
15:48:39.673577 reader C read: X was here once
15:48:39.673601 writer Y writing: Y was here once
15:48:39.673614 reader A read: X was here once
15:48:39.694576 writer Y done
15:48:39.749585 reader B ready to read
15:48:39.749606 reader B reading
15:48:39.764576 reader A ready to read
15:48:39.764602 reader A reading
15:48:39.767576 writer X ready to write
15:48:39.770575 reader B read: Y was here once
15:48:39.785577 reader A read: Y was here once
15:48:39.785597 writer X writing: X was here again
15:48:39.790576 reader C ready to read
15:48:39.806576 writer X done
15:48:39.806595 reader C reading
15:48:39.809575 writer Y ready to write
15:48:39.827575 reader C read: X was here again
15:48:39.827596 writer Y writing: Y was here again
15:48:39.848572 writer Y done
15:48:39.864573 reader B ready to read
15:48:39.864587 reader B reading
15:48:39.885572 reader B read: Y was here again
15:48:39.887572 reader A ready to read
15:48:39.887588 reader A reading
15:48:39.908572 reader A read: Y was here again
15:48:39.911572 reader C ready to read
15:48:39.911586 reader C reading
15:48:39.932572 reader C read: Y was here again
15:48:39.932593 Final value: Y was here again

by Sonia at August 10, 2011 08:48 PM

August 01, 2011

Miek Gieben

Chaining proxies

Online signing is cool, but slow. Caching queries in a reverse proxy is nice, but useless for something like NSD. But what if you want to do online signing in a fast way?

Enter: proxy chaining.

I already showed FunkenSign (example code is quite old though) and yesterday FunkenShield.

What if you combine the two? That gives the best of both worlds:

  • Online signing;
  • Caching;
  • And it adheres to the true Unix philosophy: do one thing, and do one thing well.

So lets get some figures again.

Nameserver

First start the nameserver:

cd _examples/ns && make
GOMAXPROCS=10 ./ns      # listens on port 8053

Online signing proxy

Next we start our online signing proxy. This proxy only signs answers to questions for c.miek.nl., and leaves other questions alone.

We listen on port 8054 and use the nameserver we started on port 8053:

cd examples/funkensturm && make -f Makefile_sign
# save the exe
cp funkensturm funkensturm_sign
# start it
GOMAXPROCS=10 ./funkensturm_sign -rserver=127.0.0.1:8053 -sserver=127.0.0.1:8054

Reverse proxy

And lastly the reverse proxy. It listens on port 8055 and forwards queries to 8054.

make -f Makefile_rproxy
cp funkensturm funkensturm_rproxy
GOMAXPROCS=10 ./funkensturm_rproxy -rserver=127.0.0.1:8054 -sserver=127.0.0.1:8055

Numbers

So we have:

caching proxy -> signing proxy -> nameserver

And for queryperf we create a data file with three queries:

  1. a.miek.nl A
  2. a.miek.nl AAAA
  3. c.miek.nl A

Where the answer to 3 will include a generated signature.

So lets query the nameserver on port 8053:

./queryperf -d data -s 127.0.0.1 -p 8053 -l 2 
Queries per second:   7298.194728 qps

7000+ qps; a normal number. Next directly query the online signing proxy on port 8054:

./queryperf -d data -s 127.0.0.1 -p 8054 -l 2
Queries per second:   205.991306 qps

205 qps... that's onine signing for you: S L O W.

Next we use the caching proxy which caches the answers, we query on port 8055:

./queryperf -d data -s 127.0.0.1 -p 8055 -l 2
Queries per second:   28521.826761 qps

Thats again more like it.

So we have fast online signing in a clean way.

Note: (excluding godns) the combined line count is 197 lines for ns and 450 for funkensturm.

by Miek Gieben at August 01, 2011 07:38 PM

July 31, 2011

Miek Gieben

Reverse DNS proxy

Have a slow nameserver and want to spice things up? How about a reverse DNS proxy? For lack of a cool name I chose the name FunkenShield. It's (of course) in the early stages, but it works quite nicely already.

This is done with the framework of FunkenSturm. Which is part of GoDNS.

How it works:

You place FunkenShield in front of your nameserver and it will cache the binary packets coming from your server in a local cache.

It is written in Go, and the beauty of it is that Go compiles to static executables, so I can give you (or you can compile it yourself) the exe and you can experiment with it yourself.

Some numbers

GoDNS is a library that helps you create DNS software. In this library some example programs are included, among other, a simple nameserver. Currently this nameserver works with 1 zone, namely "miek.nl". If you run it, it defaults to listening on port 8053:

% ./ns      # start the nameserver

<other terminal>

% dig @127.0.0.1 -p 8053 mx miek.nl
<snip>
;; QUESTION SECTION:
;miek.nl.                       IN      MX

;; ANSWER SECTION:
miek.nl.                345600  IN      MX      20 mail.atoom.net.
miek.nl.                345600  IN      MX      40 mx-ext.tjeb.nl.

;; AUTHORITY SECTION:
<snip>

;; ADDITIONAL SECTION:
miek.nl.                0       IN      TXT     "Proudly served by Go: http://www.golang.org"

So that works. But how fast is it? This queryperf asks two questions: "A a.miek.nl" and "AAAA a.miek.nl":

% ./queryperf -d data -s 127.0.0.1 -p 8053 -l 10
<snip>
Queries per second:   3079.260741 qps

Hmmm, only about 3000. Lets spice things up a bit and utilize Go's multicore features:

% GOMAXPROCS=20 ./ns 
% ./queryperf -d data -s 127.0.0.1 -p 8053 -l 10
Queries per second:   7124.942077 qps

More than doubled. Nice, but still nothing to make NSD afraid.

Enter FunkenShield

We run FunkenShield on port 8054, and allow it to have a multitude of goroutines. Note: "./ns" is still running. If FunkenShield has a cache miss it still needs to ask the nameserver.

% cd _examples/funkensturm && make -f Makefile_rproxy
% GOMAXPROCS=20 ./funkensturm -rserver 127.0.0.1:8053 -sserver 127.0.0.1:8054
% ./queryperf -d data -s 127.0.0.1 -p 8054 -l 10        # port = 8054!
Queries per second:   27506.219188 qps

W00t!

27506 qps.

27000+ qps is not bad for a nameserver.

by Miek Gieben at July 31, 2011 03:09 PM

RSC

_rsc: When open source matters, it really matters. http://t.co/6tDeV0a

_rsc: When open source matters, it really matters. http://t.co/6tDeV0a

July 31, 2011 03:34 AM

July 29, 2011

Airs – Ian Lance Taylor

Voluntary Foreclosure

The otherwise completely forgettable movie Larry Crowne had one scene I found quite interesting. The eponymous protagonist, played by Tom Hanks in his blandest mode, is presented as an all-around good guy. He is gentlemanly, helpful, considerate, and in fact has no flaws except for the rather minor one which starts the little action there is in the movie: he did not go to college. In college he studies, among other things, economics, in a class taught by Lt. Sulu.

Studying economics leads Larry Crowne to the one interesting scene: he abandons his mortgage and returns his house to the bank. The bank representative warns him that this will hurt his credit, but he assures her that it is the right thing for him to do, and that he has the right to do it, provided he vacates the house in 30 days.

The U.S. is a country which famously agreed to pay all its debts when it was founded—the newly created federal government assumed the debts that the various states had accumulated during and before the War of Independence. In general American culture assumes that you are required to pay all your debts. Declaring bankruptcy is a sign of personal failure.

Apparently the financial crisis, and the stagnation of middle class income compared with the increasing price of housing, has now made it acceptable for a mainstream movie character to renege on his debt. This is of course a calculation that businesses make routinely, but not individuals. Admittedly voluntary foreclosure is not quite as bad as bankruptcy. After all, the bank does get the house, which is worth something although it is clearly understood that the bank does not want it.

Given the current main news story, I just have to hope that this acceptance of foreclosure does not carry over to thinking it is OK for the entire country to go into default.


by Ian Lance Taylor at July 29, 2011 02:03 PM

July 19, 2011

Going Along

按下葫芦起了瓢

当老婆怀上小三老四准备请四个月产假时, 她民营企业家老板甩下这么一句:"你老公是做什么的?" 意思是他有三个孩子已经是钱和地位的炫耀了, 一个小小的打工仔凭什么能生这么多?

生则生矣. 天主教反对避孕, 认为生命是上天的恩赐. 我非教友, 也不反对避孕, 认为生命是可控的小概率意外. 一旦发生, 就应该顺其自然, 终止这个过程是非道德的谋杀. 所以, 尽管在经历了第一个孩子的辛苦后感慨 "One is more than enough", 还是满心欢喜的迎接第二个. 但接踵而至的双胞胎确实是意料之外的措手不及.

如今, 每晚每三个小时被某位的哭声唤醒, 迷瞪着喂奶一起挣扎一个小时. 这种日子已经两个星期了. 我还很敬佩自己能白天继续上班, 现在还能继续抽空补充一下Blog 的空白.

去睡了. 太阳明天还依旧要升起.


by Fango (noreply@blogger.com) at July 19, 2011 02:15 PM

Airs – Ian Lance Taylor

CVS SSH

Sorry for the long posting hiatus. I have enough readers now that it’s hard to write the usual random nonsense.

I was recently reminded of an old problem using CVS over SSH, which was an interesting example of various different instances of reasonable behaviour adding up to a bug. It’s possible that this bug has been fixed, but I’ll assume that it hasn’t. The bug is that “cvs diff 2>&1 | less” will sometimes drop data, leaving you looking at an incomplete diff.

When CVS invokes SSH, it sets up file descriptors 0 (standard input) and 1 (standard output), but not 2 (standard error). Thus SSH inherits file descriptor 2 from CVS. This means that any SSH errors get reported to the standard error passed to CVS, which is what you want to have happen. However, when using 2>&1, this means that SSH’s file descriptor 2 will be the same as CVS’s file descriptors 1 and 2.

SSH puts its file descriptors 0, 1, and 2 into nonblocking mode, so that it can use select to send data back and forth without blocking. This means that SSH puts CVS’s file descriptor 2 into nonblocking mode. When using 2>&1, file descriptors 1 and 2 are the same, so this puts CVS’s file descriptor 1 into nonblocking mode.

CVS uses stdio to output data to standard output. When writing to a pipe, the buffer can fill up. CVS naturally never puts the descriptor into nonblocking mode, but when SSH has done it indirectly, and the buffer fills, the data being written will be discarded. This is what causes the bug: the discarded data is never seen by the user.

So what’s the fix? It’s reasonable for SSH to put its descriptors into nonblocking mode. It’s reasonable for CVS to pass its file descriptor 2 to SSH. It’s reasonable for CVS to use stdio to output data. It’s reasonable for stdio to not specially handle a nonblocking file descriptor—any program which wants to use a nonblocking descriptor needs to handle I/O retries itself. It’s reasonable for 2>&1 to mean that file descriptors 1 and 2 refer to the same underlying pipe. It’s reasonable for the user to use 2>&1 when piping cvs diff output to less.

I think the only remaining link in the sequence leading to the bug is that when SSH sets its file descriptor 2 to nonblocking mode, this affects the file in CVS. This is a consequence of the Unix file model, in which file descriptors refer to underlying files. A file descriptor has only one flag: whether it is closed if the exec system cal is run. All the other information is attached to the underlying file. Using 2>&1 means that two file descriptors point to the same file. Forking and execing SSH does not change this–in fact, it adds two more file descriptors, in the SSH process, which point to the same file. Any change in the flags associated with that file is seen by all the associated file descriptors.

This separation of file descriptor and file is what makes 2>&1 work. It’s also what makes >> work; >> opens a file in append mode, and the append flag is inherited by other processes which refer to that file. In any case, what really counts here is not the exec, but the fork; forking a process should not change the flags associated with a file. Further, I’m sure there programs which depend on the fact that changing the flags on a file after a fork affects the file as seen by the parent process.

It’s possible to imagine that file descriptors point to a new shared structure which then points to the underlying file. The file position and some flags would stay with the underlying flie. The new shared structure would just hold some flags which need not always be shared: O_NONBLOCK, O_ASYNC, etc. Calling fork would not create a new shared structure, but calling exec would, copying the existing structure. That would let some flags not be copied across exec process boundaries.

However, that would be a significant change to the Unix file model, a model which has lasted for decades and is not clearly broken. Absent that change, we are left with a complex bug, in which all choices are reasonable.

The workaround for the problem is to invoke SSH with 2>/dev/null, and assume that SSH never writes anything useful to standard error. The 2>/dev/null disassociates SSH file descriptor 2 from CVS file descriptor 2, so CVS file descriptor 1 is not accidentally set into nonblocking mode.


by Ian Lance Taylor at July 19, 2011 06:14 AM

July 18, 2011

Miek Gieben

On programming languages and programmers

Very well written email message from Geoff Teale on the golang mailing list on programmers and progamming languages (thread).

To summarise a long presentation I gave to non-programmers:

  • There are 12 million programmers in the world
  • The majority of those programmers are scarcely qualified
  • Most technology decisions are made by a combination of following the crowd and a false understanding of risk.
  • The high cost and failure rate in software development is no coincidence.

Remember the Stevie Wonder rule - "When you believe in something you don't understand then you suffer". In this case that means "Perhaps making programming language decisions based on what 12 million powerless idiots are doing isn't the golden road to glory and great hacks."

Go is a genuine attempt to improve the state of systems programing language beyond the point they reached in the early 1970s. As a result the sort of people using it are mostly that small community of people who understand and care about the concerns that drive such a development.

You're not going to catch those 12 million people unless you can market heavily enough the idea that their future income depend on jobs/contacts built around go, but that goal just draws resources and energy away from making the language better.

Arguably Java also suffers from it's large community of corporate drones. The slavish tendency to build baroque, mausoleums of intricate classes, dense with state and dripping with verbose XML is a reflection of the unthinking insanity of the 12 million.

I'd rather a tiny community use the language well, built successful applications and organically grew the user base whilst establishing a clean, sane library base that might later be used to improve the lives of a wider population of programmers.

I've said more than enough, I'll trundle back to the twelve million and take my punishment now.

by Miek Gieben at July 18, 2011 12:02 PM

July 07, 2011

Journal

How to find out which Active Directory security group you belong to

This took a while to figure out how to do, so I thought I'd post how to identify which security groups a given user belongs to within a Microsoft Active Directory system.

Assuming you still haven't bought a mac, here are the steps:

  1. Start --> Run -- CMD

First, find out your user info by issuing this command.

C:\Documents and Settings\cthomps> dsquery user -name Charles*

This will emit:

"CN=Charles Thompson,OU=Bellevue,OU=User Accounts,DC=OFFDECK,DC=local"

Last, use the above output string as an input argument for the final command we'll run.

 

C:\Documents and Settings\cthomps>dsget user "CN=Charles Thompson,OU=Bellevue,OU=User Accounts,DC=OFFDECK,DC=local" -memberof -expand
This will emit:
"CN=Foobar,OU=Group Accounts,DC=OFFDECK,DC=local"
"CN=Domain Users,CN=Users,DC=OFFDECK,DC=local"
"CN=Users,CN=Builtin,DC=OFFDECK,DC=local"

 

Now you know that I am part of the Foobar group. 

Happy trails,

Charles

 

by Charles Thompson at July 07, 2011 08:30 PM

July 06, 2011

Miek Gieben

Go DNS (update)

I'm finally back to coding Go DNS and making it work with the latest Go releases. Also the API has changed quite significantly since the last time I blogged about it.

So this I will detail key2ds which is small utility that queries a zone and print any DNSKEY records as DS records on the fly, to show the new API and some sample usage.

% ./key2ds sidn.nl
sidn.nl.    0   IN  DS  42033 8 1 343F74674D36C9B5BE2CEB2C401AC4EDEB2A05B2
sidn.nl.    0   IN  DS  42033 8 2 BF985EC0738FACC89EE0B12FBD9261827C59191D9EA6A9BDFF55F9BDF3DBBFF3
sidn.nl.    0   IN  DS  39274 8 1 E79E031DFDE8E68EF1E2C6CA0943C2CC0DED1889
sidn.nl.    0   IN  DS  39274 8 2 8E8A8CFB40FD0C30BFA82E53752E1C257DAFB7B6206D12B9EDA43AF3EAB2157D

This util uses synchronous queries. I will explain the main-function:

func main() {
        conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
        if len(os.Args) != 2 || err != nil {
                fmt.Printf("%s DOMAIN\n", os.Args[0])
                os.Exit(1)
        }

Read the resolver config from /etc/resolv.conf and check if enough parameters have been given.

    m := new(dns.Msg)
    m.SetQuestion(os.Args[1], dns.TypeDNSKEY)

Prepare a new dns message to send to the other side. I'm interested in the DNSKEYs for the name given on the command line.

    // Set EDNS0's Do bit
    e := new(dns.RR_OPT)
    e.Hdr.Name = "."
    e.Hdr.Rrtype = dns.TypeOPT
    e.SetUDPSize(2048)
    e.SetDo()
    m.Extra = append(m.Extra, e)

This is DNSSEC so I must prepare an EDNS0 section, which is nothing more than adding an OPT RR to the additional section. I'm pondering making EDNS0 easier and provide a few helper functions (ideas welcome!). For now the whole OPT RR must be defined from scratch.

    c := dns.NewClient()
    r := c.Exchange(m, conf.Servers[0])
    if r == nil {
            fmt.Printf("*** no answer received for %s\n", os.Args[1])
            os.Exit(1)
    }

Create a new client and use Exchange() to perform send the query and wait for the reply. Note that we only use the first server defined in /etc/resolv.conf. (There is room for some improvements :-) )

    if r.Rcode != dns.RcodeSuccess {
            fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1])
            os.Exit(1)
    }

If anything is wrong with the answer I bail out here.

    for _, k := range r.Answer {
            if key, ok := k.(*dns.RR_DNSKEY); ok {

Loop through the answer section and check each RR to see if it is a DNSKEY record with a type check.

                    ds := key.ToDS(dns.HashSHA1)
                    ds.Hdr.Ttl = 0
                    fmt.Printf("%v\n", ds)

For each DNSKEY convert it to a SHA1 DS records with ToDS()

                        ds = key.ToDS(dns.HashSHA256)
                        ds.Hdr.Ttl = 0
                        fmt.Printf("%v\n", ds)
                }
        }
}

And do the same for SHA256 and print that too.

by Miek Gieben at July 06, 2011 07:41 AM

July 05, 2011

Miek Gieben

Go DNS (update)

I'm finally back to coding Go DNS and making it work with the latest Go releases. Also the API has changed quite significantly since the last time I blogged about it.

I will detail key2ds which is small utility that queries a zone and print any DNSKEY records as DS records on the fly.

% ./key2ds sidn.nl
sidn.nl.    0   IN  DS  42033 8 1 343F74674D36C9B5BE2CEB2C401AC4EDEB2A05B2
sidn.nl.    0   IN  DS  42033 8 2 BF985EC0738FACC89EE0B12FBD9261827C59191D9EA6A9BDFF55F9BDF3DBBFF3
sidn.nl.    0   IN  DS  39274 8 1 E79E031DFDE8E68EF1E2C6CA0943C2CC0DED1889
sidn.nl.    0   IN  DS  39274 8 2 8E8A8CFB40FD0C30BFA82E53752E1C257DAFB7B6206D12B9EDA43AF3EAB2157D

This util uses synchronous queries. I will detail the main-function:

func main() {
        conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
        if len(os.Args) != 2 || err != nil {
                fmt.Printf("%s DOMAIN\n", os.Args[0])
                os.Exit(1)
        }

Read the resolver config from /etc/resolv.conf and check if enough parameters have been given.

    m := new(dns.Msg)
    m.SetQuestion(os.Args[1], dns.TypeDNSKEY)

Prepare a new dns message to send to the other side. I'm interested in the DNSKEYs for name given on the command line.

    // Set EDNS0's Do bit
    e := new(dns.RR_OPT)
    e.Hdr.Name = "."
    e.Hdr.Rrtype = dns.TypeOPT
    e.SetUDPSize(2048)
    e.SetDo()
    m.Extra = append(m.Extra, e)

This is DNSSEC so I must prepare an EDNS0 section, which is nothing more than adding an OPT RR to the Additional section. I'm pondering making EDNS0 easier and provide a few helper functions. For now the whole OPT RR must be defined from scratch.

    c := dns.NewClient()
    r := c.Exchange(m, conf.Servers[0])
    if r == nil {
            fmt.Printf("*** no answer received for %s\n", os.Args[1])
            os.Exit(1)
    }

Create a new client and use Exchange() to perform send the query and wait for the reply. Note that we only use the first server defined in /etc/resolv.conf. (There is room for some improvements :) )

    if r.Rcode != dns.RcodeSuccess {
            fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1])
            os.Exit(1)
    }

If anything is wrong with the answer I bail out here.

    for _, k := range r.Answer {
            if key, ok := k.(*dns.RR_DNSKEY); ok {

Loop through the answer section and check each RR to see if it is a DNSKEY record with a type check.

                    ds := key.ToDS(dns.HashSHA1)
                    ds.Hdr.Ttl = 0
                    fmt.Printf("%v\n", ds)

For each DNSKEY convert it to a SHA1 DS records with ToDS()

                        ds = key.ToDS(dns.HashSHA256)
                        ds.Hdr.Ttl = 0
                        fmt.Printf("%v\n", ds)
                }
        }
}

And do the same for SHA256 and print that too.

by Miek Gieben at July 05, 2011 08:45 PM

July 04, 2011

Going Along

left, right, left


英文最无聊的是拿left,<wbr></wbr>right找乐,对应翻译,是最头疼不过的事了。

Should I go to the left, where nothing's right? Or to the right, where nothing's left. 
我该左转面对错误,还是右转寻找失去?
My left brain has nothing right; while my right brain has nothing left.
我的左脑没点儿正题,右脑没点儿残余。
War does not determine who is right - only who is left.  (Bertrand Russell)
战争决定的不是对错,只是死活。

我也这样教孩子:
“Use your right hand to write!"

by Fango (noreply@blogger.com) at July 04, 2011 02:09 PM

July 03, 2011

catena

Credo example: from lex and c source to cygwin executable, across directories

This article walks through building a trivial example of lex source
code into C, and then compiling the generated C source code into
object files and an executable in a different directory. The point is
to demonstrate using dodep to generate and customize build scripts
and list dependencies, and credo to execute the build process.

Lines which start with ; are shell commands; lines without are output.
Key commands are displayed in red text.

Starting state

We start with the files lorem, src/dcr.l, src/header.c, src/lex.h,
and an empty obj directory. Each line of lorem ends with a
carriage return (not usually printed), which we want to strip.
header.c is an ordinary C source file to compile and link.
lex.h gives us a local header to look for.

Generate source code

; cd src
; echo flex > dcr.c.lex.env

We first go into the src directory, and set the lex shell variable to flex,
to specify which lexer to use. To demonstrate an unusual feature,
that would be valid across ports to different operating systems,¹
we’ll capture it with this command.
¹ Inferno and Plan 9 store variables by name in the directory /env,
different for each process. This gives it a definite advantage in
capturing dependencies on variables, since we can just list the name
of the shell-variable file as a dependency.

src/dcr.c.lex.env:1: flex

This setting applies (is transformed into a shell variable) when credo
processes the target dcr.c, if dcr.c depends on /env/lex.

; dodep (c lex l c) dcr.c
credo dcr.c

We ask dodep to generate a shell script and list of dependencies with
this command. The content of these files comes from a library,
indexed by the four parameters given before the name of the file we
want to build. In this case, we want to look in the “c” namespace,
at the command “lex”, to translate an “l” file to a “c” file.

The generated dependency list includes the name of the lex source file,
and references to the variables lex and lflags.

src/dcr.c.dep:1: 0 dcr.l
src/dcr.c.dep:2: 0 /env/lex
src/dcr.c.dep:3: 0 /env/lflags

Credo passes the name of its target, the C source file, to the shell script,
which prints the name of the C source file if it was able to create it.

src/dcr.c.do:1: #!/dis/sh
src/dcr.c.do:2: c = $1
src/dcr.c.do:3: (sum l) = `{grep '\.l$' $c^.dep}
src/dcr.c.do:4:
src/dcr.c.do:5: if {no $lex} {lex = lex}
src/dcr.c.do:6:
src/dcr.c.do:7: if {
src/dcr.c.do:8: flag x +
src/dcr.c.do:9: os -T $lex $lflags $l
src/dcr.c.do:10: mv lex.yy.c $c
src/dcr.c.do:11: } {
src/dcr.c.do:12: echo $c
src/dcr.c.do:13: }

The hosted-Inferno command os calls host-OS commands. Under Cygwin,
it calls Cygwin or Windows commands.

Dodep finally prints a credo command to build the requested target.

; credo dcr.c
credo dcr.l
credo dcr.c
os -T flex dcr.l
mv lex.yy.c dcr.c
dcr.c

Credo builds all its non-/env dependencies in parallel. Once these
are complete, it determines whether the checksum of each file on which
it depends is different than the checksum stored for that file the
last time credo built this target.

This credo command creates a build-avoidance marker file dcr.l.redont,
which tells further runs of credo that the file dcr.l does not need
any processing, so exit early. It calls the host’s flex program,
on the path set when the Inferno environment started, to create the
target file dcr.c.

It updates the check sums in the file dcr.c.dep, to reflect content at
the time of compilation. This means that each target has its own view
of its dependencies, and any change to any file on which it depends,
or a change to the list of files on which it depends, prompts the
target to rebuild.

src/dcr.c.dep:1: 79f9925952d9cfb5a58270c0e2b67691 dcr.l
src/dcr.c.dep:2: 897a779351421523cadbafccdce22efe /env/lex
src/dcr.c.dep:3: 0 /env/lflags

It creates these checksum files to detect any changes to the target’s
dependencies or build script.

src/dcr.c.dep.sum:1: c33129421847b7d897d4e244caf1b8c0 dcr.c.dep

src/dcr.c.do.sum:1: 71d94d651922bd5ff5b543bba52f47d0 dcr.c.do

It also checksums the target file itself. If credo finds, the next
time it runs, that the current checksum of the target does not match,
then it assumes that the target was changed by hand, and will not
overwrite it.

src/dcr.c.sum:1: 2fed3667f390fd80993e090a7eb92c75 dcr.c

Compile objects and executable

We now have all the source, so we go to the object-file and executable
directory, and ask dodep to provide do and dep files to build dcr.exe.
From the “c”-language toolset, we use “cc” to compile an “o”bject file
into an “exe”cutable.

; cd ../obj
; dodep (c cc o exe) dcr.exe
credo dcr.exe

This time the dodep command created two *.do files, dcr.exe.dep.do and
dcr.exe.do, instead of a do and dep file for the target dcr.exe.

obj/dcr.exe.dep.do:1: #!/dis/sh
obj/dcr.exe.dep.do:2: dep = $1
obj/dcr.exe.dep.do:3: exe = `{echo $dep | sed 's,\.dep,,'}
obj/dcr.exe.dep.do:4: (stem ext o) = `{crext o $exe}
obj/dcr.exe.dep.do:5:
obj/dcr.exe.dep.do:6: adddep $exe /env/ars /env/cc /env/cflags /env/ldflags /env/objs
obj/dcr.exe.dep.do:7: adddep $exe $o
obj/dcr.exe.dep.do:8: adddep $exe `{/lib/do/c/coneed $o}

dcr.exe.dep.do creates the dependency list dcr.exe.dep for the target
dcr.exe by adding a set of credo-specific (ars and objs) and standard
(cc, cflags, and ldflags) shell variables; the primary object file,
named for the executable; and any other object files which the primary
one needs. The tool coneed does this last bit of analysis by compiling
the primary object file, looking for unresolved symbols in available
source code, compiling the source code to make sure, and printing the
set of matching object files.

obj/dcr.exe.do:1: #!/dis/sh
obj/dcr.exe.do:2: exe = $1
obj/dcr.exe.do:3: if {no $cc} {cc = cc}
obj/dcr.exe.do:4: if {
obj/dcr.exe.do:5: flag x +
obj/dcr.exe.do:6: os -T $cc $cflags -o $exe $objs $ars $ldflags
obj/dcr.exe.do:7: } {
obj/dcr.exe.do:8: echo os -T ./$exe
obj/dcr.exe.do:9: }

To gather enough information to compile the executable, credo relays
information from dependencies back to calling targets through *.relay
files, which are shell scripts that set the environment for calling targets.

Finally, to start the build process, we locate the source code,
record which compilation tools to use, and again call credo.

; srcdir = ../src/
; echo cpp-3 > default.cpp.env
; echo -I../src/ > default.cppflags.env
; echo gcc-3 > default.cc.env
; credo dcr.exe
credo dcr.exe.dep
os -T gcc-3 -I../src/ -c ../src/dcr.c
os -T gcc-3 -I../src/ -c ../src/header.c
credo dcr.o.dep
credo dcr.o
credo header.o.dep
credo header.o
credo dcr.exe
os -T gcc-3 -o dcr.exe dcr.o header.o
os -T ./dcr.exe

Credo first runs dcr.exe.dep.do to find and store the dependencies of
dcr.exe in dcr.exe.dep. dcr.exe.dep is an implicit dependency of dcr.exe,
so credo runs its *.do script automatically.

obj/dcr.exe.dep:1: 0 /env/ars
obj/dcr.exe.dep:2: 131eb9ab2f6a65b34e0158de1b321e3c /env/cc
obj/dcr.exe.dep:3: 0 /env/cflags
obj/dcr.exe.dep:4: 0 /env/ldflags
obj/dcr.exe.dep:5: 84a1a3c306e006dd723bebe9df29ee6c /env/objs
obj/dcr.exe.dep:6: 3f0d90c1c7483ad1805080cf9e48d050 dcr.o
obj/dcr.exe.dep:7: 1db618b791dc87ac0bd5504f69434273 header.o

Coneed uses $srcdir to find dcr.c, and the setting of cppflags in
default.cppflags.env to find lex.h. Once coneed compiles dcr.o,
it finds the unresolved symbol for the header function, finds a
definition of header() in header.c, and compiles header.o to verify
that it defines the symbol. Coneed finds no other unresolved symbols
supplied by other source code in $srcdir (c.f. printf), so it stops.
Coneed uses “dodep (c cc c o)” to compile the source code in $srcdir
into object files in the current directory (obj). This generates
*.o.dep.do and *.o.do for each source file: those for dcr are shown,
the ones for header are identical.

obj/dcr.o.dep.do:1: #!/dis/sh
obj/dcr.o.dep.do:2: dep = $1
obj/dcr.o.dep.do:3: o = `{echo $dep | sed 's,\.dep,,'}
obj/dcr.o.dep.do:4: (stem ext c) = `{crext c $srcdir^$o}
obj/dcr.o.dep.do:5:
obj/dcr.o.dep.do:6: adddep $o /env/cc /env/cflags /env/cppflags
obj/dcr.o.dep.do:7: adddep $o $c
obj/dcr.o.dep.do:8: adddep $o `{/lib/do/c/findh $c}

obj/dcr.o.do:1: #!/dis/sh
obj/dcr.o.do:2: o = $1
obj/dcr.o.do:3: (sum c) = `{grep '\.c$' $o^.dep}
obj/dcr.o.do:4:
obj/dcr.o.do:5: if {no $cc} {cc = cc}
obj/dcr.o.do:6:
obj/dcr.o.do:7: if {
obj/dcr.o.do:8: flag x +
obj/dcr.o.do:9: os -T $cc $cflags $cppflags -c $c
obj/dcr.o.do:10: } {
obj/dcr.o.do:11: echo 'objs = $objs '^$o > $o^.relay
obj/dcr.o.do:12: }

Before credo runs dcr.o.do it runs dcr.o.dep.do to generate dcr.o.dep
(header.o likewise). To find the paths to C header files it calls findh,
which gathers header-file #includes from the C source files,
and searches for them in local and system directories using cppflags
and the search list printed by $cpp -v.

obj/dcr.o.dep:1: 131eb9ab2f6a65b34e0158de1b321e3c /env/cc
obj/dcr.o.dep:2: 0 /env/cflags
obj/dcr.o.dep:3: 9134215aef7b4657beb5c4bb7a20d4a1 /env/cppflags
obj/dcr.o.dep:4: 2fed3667f390fd80993e090a7eb92c75 ../src/dcr.c
obj/dcr.o.dep:5: feea1fa232f248baa7a7d07743ee86c4 ../src/lex.h
obj/dcr.o.dep:6: fb584676de41ee148c938983b2338f5b /n/C/cygwin/usr/include/stdio.h
obj/dcr.o.dep:7: f6409b1008743b1866d4ad8e53f925cc /n/C/cygwin/usr/include/string.h
obj/dcr.o.dep:8: 468b1dd86fef03b044dceea020579940 /n/C/cygwin/usr/include/errno.h
obj/dcr.o.dep:9: 4e6678324ba6b69666eba8376069c950 /n/C/cygwin/usr/include/stdlib.h
obj/dcr.o.dep:10: c7575313e03e7c18f8c84a5e13c01118 /n/C/cygwin/usr/include/inttypes.h
obj/dcr.o.dep:11: a8fd5fa102b8f74d1b96c6c345f0e22d /n/C/cygwin/usr/include/unistd.h

obj/header.o.dep:1: 131eb9ab2f6a65b34e0158de1b321e3c /env/cc
obj/header.o.dep:2: 0 /env/cflags
obj/header.o.dep:3: 9134215aef7b4657beb5c4bb7a20d4a1 /env/cppflags
obj/header.o.dep:4: ecd87752a211e69078e6bc37afbb561b ../src/header.c
obj/header.o.dep:5: feea1fa232f248baa7a7d07743ee86c4 ../src/lex.h
obj/header.o.dep:6: fb584676de41ee148c938983b2338f5b /n/C/cygwin/usr/include/stdio.h

At this point credo has the dependencies for dcr.exe, so it works
through them, calling the *.relay files—generated by the object files’
do scripts—to gather object names in $objs.

obj/dcr.o.relay:1: objs = $objs dcr.o

obj/header.o.relay:1: objs = $objs header.o

There’s not much to do since coneed already compiled the object files,
so it links them, and prints an os command to run the executable,
since Inferno can’t directly run a Cygwin executable.

Clean up

To clean up, we remove generated files in the src and obj directories.

; rm -f *.redoing *.redont *.renew *.reold *.sum

This removes all the temporary state files created by credo.
Once this is done credo will rebuild from scratch, and reconstruct
each target’s view of its dependencies.

; rm -f `{lsdo | sed 's,^c?redo ,,'}

This removes all the targets created by do scripts. The lsdo command
(called by credo with no targets) prints credo commands for all the
credo targets in the current directory. For example:

; cd src; credo
credo dcr.c

; cd obj; credo
credo dcr.exe
credo dcr.exe.dep
credo dcr.o
credo dcr.o.dep
credo header.o
credo header.o.dep

Once the targets are removed, credo will unconditionally generate them.

; rm -f *.do *.dep *.relay

This removes all the instructions credo uses to build files.
These may usually be regenerated by dodep, adddep (which adds
to the given target’s dependency list the given files), and
rmdep (which removes the given files).

A single library script is provided to remove the state, target,
and dodep files.

; rm -f *.env

This removes default and per-target environment settings.

Once we remove these sets of files, the directories contain only the
files present in their starting state.


by catena at July 03, 2011 07:51 PM

July 01, 2011

research!rsc

Floating Point to Decimal Conversion is Easy

<style> pre { margin-left: 4em; } p { line-height: 175%; } </style>

Floating point to decimal conversions have a reputation for being difficult. At heart, they're really very simple and straightforward. To prove it, I'll explain a working implementation. It only formats positive numbers, but expanding it to negative numbers, zero, infinities and NaNs would be very easy.

An IEEE 64-bit binary floating point number is an integer v in the range [252, 253) times a power of two: f = v × 2e. Constraining the fractional part of the unpacked float64 to the range [252, 253) makes the representation unique. We could have used any range that spans a multiplicative factor of two, but that range is the first one in which all the values are integers.

In Go, math.Frexp unpacks a float64 into f = fr × 2exp where fr is in the range [½, 1). (C's frexp does too.) Converting to our integer representation is easy:

fr, exp := math.Frexp(f)
v := int64(fr * (1<<53))
e := exp - 53

To convert the integer to decimal, we'll use strconv.Itoa64 out of laziness; you know how to write the direct code.

buf := make([]byte, 1000)
n := copy(buf, strconv.Itoa64(v))

The allocation of buf reserves space for 1000 digits. In general 1/2e requires about 0.7e non-zero decimal digits to write in full. For a float64, the smallest positive number is 1/21074, so 1000 digits is plenty. The second line sets n to the number of bytes copied in from the string representation of the (integer) v. Throughout, n will be the number of digits in buf. Note that we're working with ASCII decimal digits '0' to '9', not bytes 0-9.

Now we've got the decimal for v stored in buf. Since f = v × 2e, all that remains is to multiply or divide buf by 2 the appropriate number of times (e or -e times).

Here's the loop to handle positive e:

for ; e > 0; e-- {
    δ := 0
    if buf[0] >= '5' {
        δ = 1
    }
    x := byte(0)
    for i := n-1; i >= 0; i-- {
        x += 2*(buf[i] - '0')
        x, buf[i+δ] = x/10, x%10 + '0'
    }
    if δ