Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Go

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
test:
name: Vet and Test
runs-on: ubuntu-24.04

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

- name: Vet
run: go vet ./...

- name: Test Race Conditions
run: go test -race -v ./...

- name: Build
run: go build ./...
87 changes: 87 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
version: "2"

linters:
default: all
disable:
- inamedparam
- gochecknoinits
- wsl
- wsl_v5
- nlreturn
- whitespace
- wrapcheck
- exhaustruct
- exhaustruct_v5
- err113
- testpackage
- depguard
- paralleltest
- varnamelen
- forbidigo
- funlen
- gochecknoglobals
- godox
- lll
- mnd
- noinlineerr
# funcorder demands constructor-then-exported-then-unexported ordering,
# which conflicts with the convention here (shared with dioad/net) of
# placing an unexported helper immediately after the exported method that
# calls it: readability comes from proximity to the call site. Pure
# code-organization preference, consistent with the other style linters
# disabled above.
- funcorder
settings:
ireturn:
allow:
- anon
- error
- empty
- stdlib
# Generic functions must return their type parameter; that isn't
# the "unnecessary interface" smell ireturn looks for.
- generic
exhaustive:
# A switch that deliberately dispatches over a small, bounded subset
# of an enum's members and handles everything else in "default" is
# exhaustive by design; the default case is the intended catch-all,
# not a sign the switch forgot members.
default-signifies-exhaustive: true
tagliatelle:
case:
rules:
json: snake
exclusions:
rules:
# Table-driven tests naturally accumulate cognitive/nesting
# "complexity" from many small, independent per-case closures
# (checkFunc, subtests); that's the idiomatic Go testing pattern, not a
# sign the test itself is hard to follow. funlen is disabled repo-wide
# for the same reason.
- path: '_test\.go'
linters:
- cyclop
- gocognit
- gocyclo
- maintidx
- nestif
# Repeated literals in tests are almost always fixture/expectation
# data (a hostname, a header name, an error string) restated across
# otherwise-independent table-driven cases, not a shared business
# constant that drifted. Forcing every one into a package-level
# const would couple unrelated test cases together for no benefit;
# a magic number/string inside one test's own table is still
# self-documenting in context. 96 of this repo's 97 goconst
# findings are in _test.go files.
- path: '_test\.go'
linters:
- goconst

issues:
max-issues-per-linter: 0
max-same-issues: 0

formatters:
enable:
- gofmt
- goimports
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# This module is verified against the versions its go.mod declares, not against
# whatever a go.work above it resolves.
export GOWORK=off

default: verify

build:
go build .

lint:
go fmt ./...
go fix ./...
golangci-lint run ./...
gosec ./...
go vet ./...

test:
go test -race ./...

verify: lint test build
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# lifecycle

Process lifecycle for Dioad daemons: ordered start with rollback, staged stop with budgets, failure fan-in, readiness and drain.

```go
app := lifecycle.New(
lifecycle.WithDrainDelay(5*time.Second),
lifecycle.WithStopStages(
lifecycle.Stage{Names: []string{"api"}, Budget: 10 * time.Second},
lifecycle.Stage{Names: []string{"worker"}},
),
)
if err := app.Add("store", store); err != nil {
return err
}
if err := app.Add("worker", worker); err != nil {
return err
}
if err := app.Add("api", apiServer); err != nil {
return err
}

// Report readiness wherever the platform looks for it, for example
// dioad/net's http.Server.AddHealthSource.
app.Readiness().AddCheck("store", store.Ping, lifecycle.WithCacheTTL(5*time.Second))

return app.Run(ctx) // returns when ctx is done or a service fails
```

| | |
|---|---|
| **Start** | In registration order. A failure stops the services that had started, in the same way Stop would, and is returned joined with any stop errors. |
| **Stop** | In stages: the services of a stage stop concurrently, each stage optionally within a budget, all within one stop timeout. Without stages, in reverse registration order. Errors are joined. |
| **Failure** | A service that implements `Failed() <-chan error` has its failure reported on `App.Failed` as a `*ServiceError`. `Run` stops at once on a failure, without draining. |
| **Readiness** | Not ready until started, not ready once draining, and in between ready unless a named check fails. A check can be cached for a TTL, and can be `Degraded`: reported, but still ready. It is a health source for `dioad/net`'s HTTP server, without importing it. |
| **Drain** | When `Run`'s context is done, readiness reports not ready and `Run` waits for the drain delay before it stops the services, so the load balancer stops first. |

A `Service` is `Start(ctx) error` and `Shutdown(ctx) error`: the same method set as `dioad/net`'s `http.Server`, so
one is a `Service` with no adapter. `Failed`, `Live` and `Ready` are optional, found by interface assertion.

See the package documentation for the details, and the examples for runnable versions.
Loading
Loading