DEV Community

Anton Brilliantov
Anton Brilliantov

Posted on

Every Door Must Open

Every Door Must Open - the cover: a wall of enclosure doors that are only painted on; Biscuit has bumped his nose into one; Penny the practical hamster in glasses counts them with an abacus and a clipboard of tally marks; one real door with hinges stands open; Nora holds a checklist stamped hinges

Every Door Must Open

The zoo map had doors drawn on it that did not open, and visitors found out by walking into them. Now every door on the map gets hinges from the plan, and a painted door fails the morning check. Built and tested in the shared library.


👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.

This part is about built and tested generator behaviour and two checks in a service's build. The "nine in ten" is a count one session made at its start and did not re-check; I give it rounded and say so on the figure.

This is part 16 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.


In real life

Biscuit runs at a door on the zoo map and bounces off it. "Ouch! It's painted on." The door is on the map, it has a number and a sign, and it is a picture on a wall. Nobody lied on purpose. The map was drawn first, and the doors were going to be cut later.

A four-panel comic: Biscuit, angry, next to a house says ouch, it's painted on; Penny, bored, says painted, painted, painted, nine in ten, and Nora asks nine in ten; Pip next to a house with a green door says every door gets hinges, you only write the inside; Penny, proud, says zero painted, I counted twice, and Biscuit asks if he can run through now

This part brings in a new resident: Penny, the most practical animal in the zoo. Penny counts every carrot, and on this morning Penny counts doors: "Painted, painted, painted... nine in ten." Nora does not believe it. Then Pip brings the fix from the plan: every door on the map gets hinges printed for it, and the keeper only writes what is inside the room. The last panel is Penny again, counting twice, because Penny always counts twice: zero painted. Biscuit asks the only question he has: can he run through now?

Doors that compile

A gRPC contract lists the methods of a service. In Go, the generated server type comes with a base that answers every method with the code Unimplemented, so a service can register itself and compile long before it writes a single method. That is convenient on the first day and dangerous on every day after it. A method that nobody wrote looks exactly like a method that exists: it is in the contract, it is in the generated client, the build is green, and the first one to find out is a caller.

In our platform the contract is generated too, and services are printed from a manifest. So the painted door had two places to hide: the contract that declares the rpc, and the service that never wrote it.

Three tiles with faces: grpc: in the manifest, proud; printed method, happy; build check, proud; labels read adapter, ports, registration, and no Unimplemented; pills read built and tested

Hinges from the manifest

The fix is the same move the series keeps making: declare it, and let the generator print it. A service declares its gRPC surface in a grpc: section of the manifest. For every rpc the generator prints the adapter, the ports, the registration and a method. And the body of a small method can be written right in the manifest, so the printed method is complete. In the shape of the manifest - rpc and field names illustrative:

# service.yaml - every rpc gets an adapter, ports, registration and a method; an empty one fails the build
grpc:
  service: <package>.<Service>
  rpcs:
    - name: GetOrder
      body: |              # the printed method is complete
        return s.orders.Get(ctx, req.GetId())
    - name: ListOrders     # no body: the printed method waits for the hand-written inside
Enter fullscreen mode Exit fullscreen mode

A door-wall scene, no code: a machine signed grpc: prints real doors with hinges; one door highlighted with body: and a one-line body, a second door with no inside under a stop sign - left Unimplemented, the build fails; pills read built and tested

Penny holds a manifest board with the grpc section; beside it two doors, each with its own door keeper: the GetOrder keeper already says here is your order, the ready line from body:, and the ListOrders keeper waits with an empty notepad

An rpc without a method does not build. That is the hinge. The base that answers Unimplemented is still there in the generated code, but a service printed from the manifest can no longer quietly lean on it, because there is no rpc in the manifest that does not get its own method.

What the keeper still writes

Printing hinges does not mean printing rooms. The library's own rule for what stays hand-written is short, and this part sits right on its edge:

  • the body of your own method and the business rules in it - a concrete contract in, a concrete contract out;
  • the checks of meaning in tests, on top of the printed test skeleton;
  • the decisions people make: which daemons a service runs, and which migrations are already applied and never edited.

A body in the manifest is for the method that is one line of delegation. Everything with a rule in it is still the keeper's room, and the printed method is the door to it.

Two checks that keep it at zero

The manifest closes the door from one side. The service's build closes it from the other. The generator prints a CI for every service with two checks that matter here. The first one is called, plainly, "gRPC methods implemented": any codes.Unimplemented in the service's gRPC handlers fails the build. The second one is "Generation drift": a printed file that someone edited by hand, or that no longer matches its manifest, fails the build too.

CI of a printed service - a form
  gRPC methods implemented   fail on codes.Unimplemented in the gRPC handlers
  Generation drift           fail when a printed file differs from a fresh print
Enter fullscreen mode Exit fullscreen mode
Check Zoo Catches
a method printed per rpc every door on the map gets hinges an rpc nobody wrote
gRPC methods implemented a painted door fails the morning check a handler that still answers Unimplemented
generation drift nobody repaints a door by hand a printed file edited by hand
a walk over the running server Penny pulls every handle a door that is fine in code and painted in the build

The last row is a guard that one service wrote for itself, and it is the strongest of the four. It starts the built server and calls every rpc of its contract, one by one, and fails on any Unimplemented. It does not trust the code; it pulls every handle.

An animation: a letter travels under four tiles - the contract, the built server, every rpc, the verdict - and the pill changes from list every rpc to start the built server, call each one: Unimplemented?, and none painted: green

What went wrong

The service that wrote that walk had a good reason. At the start of one working session, its own count was that about nine rpcs in ten answered Unimplemented. The contract was large and complete, the generated client was complete, the build was green - and almost every door was painted. That count was made by the session and not re-checked from the repository later, so I give it rounded and as a ratio, not as a measurement I stand behind to the unit.

A waffle of a hundred cells: about 93 red - answered Unimplemented - and about 7 green - opened; Penny, bored, says nine in ten, I counted; a pill reads rounded; a count at the start of one session, not re-checked

The fix was not a heroic week of writing methods. It was the walk first, then a list of rpcs still waiting, then the list going down until it was empty. The walk made the waiting list visible; the list made the work countable; and the build check made sure it stays at zero.

Bars: painted doors at the start, per hundred, about 93; painted doors at the end, 0; Penny, proud, says zero, I counted twice; pills read rounded, and two checks keep it at zero: the build, the running server

What I take from it is not about gRPC. A system that lets "declared" and "done" look the same will drift toward declared. The generator's job is to make them look different - a printed method that will not build empty, a check that fails on the placeholder - so that the gap is a red build and not a surprised caller.

Before and after: an rpc without a method - compiles, and answers Unimplemented; found, by a caller, later - and an rpc with a printed method - fails the build if left empty; found by the build, first; pills read built and tested

Service tiles with faces: the contract happy, the printed adapter happy, the printed method proud, the method body happy; a painted door angry, the drift check proud, the implemented check proud, callers happy; pills read built and tested

What I do not claim

  • Not the exact count of painted doors. "Nine in ten" is rounded from one session's own count, not re-checked.
  • Not that every service already declares its gRPC surface in the manifest. The section is built and tested.
  • Not that the walk over the running server is in the library. One service wrote it.
  • Not the exact manifest syntax. The YAML above is a form.
  • Not that a body in the manifest fits every method. It fits small ones; the rest are written by hand.

What of this is method

  • Declared is not done. Make the placeholder fail the build.
  • Print a method per rpc. Every door on the map gets hinges.
  • Pull every handle. A walk over the running server trusts nothing it did not call.

Zoo keeping, built in - Part 16. Next: Your Own Enclosure Layout.

If your gRPC services embed the unimplemented base, how would you know which methods are still painted? If you generate servers from a contract, does an empty method fail your build? If you counted your painted doors once, what was the ratio? Which door on your map is still a picture?

Top comments (0)