docker/libswarm — pull request #95 by aanand (2014-06-19 14:49:30+00:00)
Title: RFC: Server DSL

Includes #94.

This is an exploration of simplifying the `Server` API to make it simpler to use (with less manual handling of `Message`s and `Send`), more self-documenting and less error-prone. It also means that validation of message arguments (e.g. making sure an `Attach` message has exactly one argument) can be done in one place, once we get round to that.

Here's a simple example of the old API versus the new one:

```patch
-func (a *aggregator) ls(ctx *beam.Message) error {
+func (a *aggregator) ls() ([]string, error) {
var children []string

for _, b := range a.backends {
bChildren, err := b.Ls()
if err != nil {
- return err
+ return nil, err
}
children = append(children, bChildren...)
}

- ctx.Ret.Send(&beam.Message{Verb: beam.Set, Args: children})
-
- return nil
+ return children, nil
}
```

So far, I've implemented enough methods to convert `dockerclient.go`, `orchard.go`, `aggregate.go` and `swarmd.go` to use the new DSL, and switched all other backends to using `OnVerb`. The line-for-line savings are negligible, but it's certainly nice to be doing less manual acking and `ctx.Args` fiddling, and I think it'll pay off in the long term.

The one place where the new Server API can't easily mirror the Object API is `Attach`, because `OnAttach` handlers are currently supposed to block until it's time to close the return channel. We might want to revisit that, but I'm not sure.