Skip to content

Source Files

Jens Alfke edited this page Mar 25, 2014 · 3 revisions

This is a quick tour of the Sync Gateway source files, for the benefit of those wanting to dive into the code.

Sub-packages

The gateway's packages are nested under github.com/couchbaselabs/sync_gateway, and the corresponding source directories are under src/github.com/couchbaselabs/sync_gateway. I'll go through them in bottom-up order.

base

Shared utilities used by the other packages.

  • bucket.go: Defines a wrapper that adapts the go-couchbase package's Bucket to match Walrus's Bucket interface, allowing the two to be used interchangeably by the rest of the code. Also has some factory functions for instantiating buckets.
  • error.go: Error handling utilities.
  • http_listener.go: An implementation of an HTTP listener that can limit the number of simultaneous connections it allows.
  • logging.go: A nice logging utility. You'll see calls to base.LogTo() all over the code.
  • logging_bucket.go: A wrapper around the Bucket interface that logs info about every call.
  • set.go: Implements Set, a reusable "set of strings" data type.
  • util.go: Miscellaneous utility functions.

channels

Various types and functions for working with channels.

  • change_log.go: The widely-used LogEntry (represents a change to the database, i.e. a new revision) and ChangeLog (an array of LogEntrys).
  • channelmapper.go: High-level API to a database's sync function. Adds thread-safety to SyncRunner.
  • set.go: Extra functions for using a base.Set as a set of channel names. Includes channel name validation and some utilities for managing the magic * channel name.
  • sync_runner.go: Low-level API to a database's sync function. The actual implementation that calls into JavaScript is here, but this level is not thread-safe so it should not be called directly.
  • timed_set.go: A map from channel names to sequence numbers.

auth

Manages users and roles and their persistent storage.

  • auth.go: The Authenticator class, a factory for User and Role objects.
  • password_hash.go: Password hashing for authentication.
  • principal.go: Defines the abstract Principal interface and its two sub-interfaces Role and User.
  • role.go: Concrete implementation of Role interface.
  • user.go: Concrete implementation of User interface.

db

rest

Dependencies

There are a number of dependent Go packages. Rather than use the regular Go package manager (go get), which always updates packages to the latest revision available, we add them to the Git repo as submodules, so we can control exactly which revisions we use.

To do this, we change the $GOPATH. The details are in the go.sh script, but basically the GOPATH consists of the root of the repo (for the Gateway sources) and the vendor subdirectory (for the dependent packages). The Git submodules are checked out under vendor/src:

  • github.com/couchbaselabs/go-couchbase: Couchbase Server API
  • github.com/couchbaselabs/go.assert: Assertion utilities for unit tests
  • github.com/couchbaselabs/sync_gateway_admin_ui: HTML source for the Gateway admin UI
  • github.com/couchbaselabs/walrus: Lightweight storage engine for tests and experimentation
  • github.com/dustin/gomemcached: Memcached API (used by go-couchbase)
  • github.com/gorilla/context: Web-server utility for storing request variables (used by gorilla/mux)
  • github.com/gorilla/mux: Rails-like HTTP request routing package
  • github.com/robertkrimen/otto: A pure Go JavaScript interpreter
  • github.com/samuel/go-metrics: Utilities for computing performance metrics; used for logging
  • github.com/tleyden/fakehttp: Mock HTTP server; used by unit tests

Some of the dependent packages don't have Git repositories because they're hosted by Google Code. These are checked in directly. Most of these have several sub-packages of which we use only one or two:

  • code.google.com/p/go.crypto: We use the bcrypt package for password hashing.
  • code.google.com/p/go.net: We use the websocket package for handling WebSocket requests.
  • code.google.com/p/go.text: Walrus uses the collate and language packages for Unicode sorting.

Clone this wiki locally