Character Set
Avoid Non-ASCII Characters in Developer-Facing Code
Code that is developer-facing only (variables, functions, test descriptions) should use the ASCII character set only. This ensures code is accessible to different developers with varying setups.Errors
Use %w When Wrapping Errors
Use%w when wrapping errors with context:
errors.As and errors.Is. More info in the blog post.
Keep Errors Short
It is customary in Go to pass errors up the call stack and decorate them. To be a good neighbor to the rest of the call stack, keep errors short:Use Lower Case in Errors
Use lower case in errors; it’s OK to preserve upper case in names.Errors Should Stick to the Facts
It is tempting to write errors that explain the problem that occurred. This can be appropriate in some end-user facing situations, but it is never appropriate for internal error messages. When your interpretation is wrong, it puts the reader on the wrong track. Stick to the facts. Often it’s enough to just describe in a few words what we were trying to do.Use %q When Interpolating Strings
Unless it would lead to incorrect results, always use%q when interpolating strings. The %q operator quotes strings and escapes spaces and non-printable characters, which can save a lot of debugging time.
Logging
Use Context-Based Logging
Thectxlogrus package allows you to extract a logger from the current context.Context structure. This should be the default logging facility, as it may carry additional context-sensitive information like the correlation_id that makes it easy to correlate a log entry with other entries of the same event.
Logging Errors
When logging an error, use theWithError(err) method.
Use the logrus.FieldLogger Interface
When passing around the logger, use thelogrus.FieldLogger interface instead of either *logrus.Entry or *logrus.Logger.
Use Snake Case for Fields
When writing log entries, uselogger.WithFields() to add relevant metadata. The keys should use snake case:
Use RPC Name as Log Message
If you don’t want to write a specific log message but only want to notify about a certain function or RPC being called, use the function’s name as the log message:Embed Package into Log Entries
To associate log entries with a given code package, add acomponent field to the log entry. If the log entry is generated in a method, the component should be $PACKAGE_NAME.$STRUCT_NAME:
Literals and Constructors
Use “Address of Struct” Instead of new
The following are equivalent in Go:Use Hexadecimal Byte Literals in Strings
When using a byte literal in a Go string, use a hexadecimal literal:4 as the first digit. 0377 equals 255 (valid byte value), but 0400 equals 256 (not valid). With hexadecimal you cannot make this mistake because the largest two-digit hex number is 0xff which equals 255.
Functions
Method Receivers
Without any good reason, methods should always use value receivers. Good reasons include (but are not limited to) performance/memory concerns or modification of state in the receiver. If any of the type’s methods requires a pointer receiver, all methods should be pointer receivers.Don’t Use “Naked Return”
In a function with named return variables, it is valid to have a plain (“naked”)return statement, which will return the named return variables.
In Gitaly we don’t use this feature. If the function returns one or more values, always pass them to return.
Ordering
Declare Types Before Their First Use
A type should be declared before its first use.Tests
Naming
Prefer to name tests in the same style as examples. To declare a test for the package, a function F, a type T, and method M on type T:Table-Driven Tests
We like table-driven tests (Table-driven tests using subtests, Cheney blog post, Golang wiki). Use subtests with your table-driven tests, usingt.Run:
Fatal Exit
Aborting test execution with any function which directly or indirectly callsos.Exit() should be avoided as this will cause any deferred function calls to not be executed. This includes any calls to log.Fatal() and related functions.
Common Setup
TheTestMain() function shouldn’t do any package-specific setup. Instead, all tests are supposed to set up required state as part of the tests themselves. All TestMain() functions must call testhelper.Run() though, which performs the setup of global state required for tests.
TestMain Location
If tests require aTestMain() function for common setup, this function should be implemented in a file called testhelper_test.go.
Black Box and White Box Testing
The dominant style of testing in Gitaly is “white box” testing, meaning test functions for packagefoo declare their own package also to be package foo. This gives the test code access to package internals.
Go also provides “black box” testing where test functions use package foo_test instead, which does not provide access to package internals.
As a team we are currently divided on which style to prefer, so we allow both. In areas of the code where there is a clear pattern, please stick with the pattern. For example, almost all our service tests are white box.
Prometheus Metrics
When adding new Prometheus metrics, follow the best practices and be aware of the gotchas.Git Commands
Gitaly relies heavily on spawning git subprocesses to perform work. Any git commands spawned from Go code should use the constructs found insafecmd.go.
These constructs, all beginning with Safe, help prevent certain kinds of flag injection exploits. Proper usage is important to mitigate these injection risks:
Flag Usage Guidelines
Prefer long flags over short flags:Go Imports Style
When adding new package dependencies to a source code file, keep all standard library packages in one contiguous import block, and all third party packages (which includes Gitaly packages) in another contiguous block. This way, the goimports tool will deterministically sort the packages which reduces noise in reviews. Valid usage:Goroutine Guidelines
Gitaly is a long-lived process. This means that every goroutine spawned carries liability until either the goroutine ends or the program exits. Proper cleanup of goroutines is crucial to prevent leaks.Is A Goroutine Necessary?
Avoid using goroutines if the job at hand can be done just as easily and just as well without them.Background Task Goroutines
These are goroutines we expect to run the entire life of the process. If they crash, we expect them to be restarted. Seedontpanic.GoForever for a useful function to handle goroutine restarts with Sentry observability.
RPC Goroutines
These are goroutines created to help handle an RPC. A goroutine started during an RPC must also end when the RPC completes.Defer-Based Cleanup
One of the safest ways to clean up goroutines is via deferred statements:Goroutine Panic Risks
Every new goroutine has the potential to crash the process. Any unrecovered panic can cause the entire process to crash and take out any in-flight requests. When writing code that creates a goroutine, consider usingdontpanic.Go to handle panic recovery.
Limiting Goroutines
When spawning goroutines, you should always be aware of how many goroutines you will be creating. While cheap, goroutines are not free. Consider:-
How many goroutines will it take the task/RPC to complete?
- Fixed number: Good
- Variable number: See next question
-
Does the goroutine count scale with a configuration value?
- Yes: Good
- No: Red flag! An RPC where goroutines do not scale predictably will open the service to denial of service attacks