Skip to content

Commit

Permalink
server: avoid fragile pattern in NewServer
Browse files Browse the repository at this point in the history
This pattern is bad:

```go
func NewServer() *Server {
  s.a = newA()
  s.b = newB(s.a, s.c)
  s.c = newC()
  return s
}
```

Note how `newB` gets called with `s.c == nil`. Creating a partially
populated `Server` struc early is not a good idea; there is no
protection against using fields before they are populated.

Instead, use this pattern:

```go
func NewServer() *Server {
  a := newA()
  c := newC()
  b := newB(a, c)
  return &Server{a: a, b: b, c: c}
}
```

Note how the bug from the first example would be caught at compile time
because we can't use something before it's defined.

In fact, carrying out this refactor revealed a bug: we were always
passing a `nil` engines slice to `NewNodeLiveness`, in effect disabling
the disk stall protection in cockroachdb#32978.

Release note: None
  • Loading branch information
tbg committed Apr 8, 2020
1 parent 3d0ac52 commit 5853bf4
Showing 1 changed file with 210 additions and 173 deletions.
Loading

0 comments on commit 5853bf4

Please sign in to comment.