boltfx

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Contributors Forks Stargazers Issues Apache 2.0 License


boltfx

Uber Fx module for bbolt (BoltDB) — opens an embedded key/value database from a bolt:// URL and ties its lifetime to the Fx application lifecycle.

Report Bug · Request Feature

Table of Contents


About The Project

boltfx is the go-core-fx module for bbolt (BoltDB), the embedded transactional key/value store. It turns a bolt:// URL into an open *bbolt.DB, hands the handle to the rest of the Fx graph, and closes it when the application stops — so consumers never have to manage the database file lifecycle themselves.

  • One-file module — boltfx.Module() returns an fx.Option that provides the *bbolt.DB and registers the close hook
  • URL-based configuration — the database location is a bolt:// URL: relative paths (bolt://data/app.db) and absolute paths (bolt:///var/lib/app.db) both work
  • Fail-fast startup — an insecure database file, a foreign URL scheme, a missing path or a file locked by another process is rejected before the application starts serving
  • Named logger — the module decorates the injected *zap.Logger with boltfx, so its log lines are distinguishable from other modules

This is a library, not an application: it reads no environment variables of its own. DEBUG is read by the go-core-fx/logger dependency, and TZ/DEBUG are set for the development server by make air — see Configuration.

Features

  • Fx lifecycle management — boltfx.Module() provides *bbolt.DB to the graph and closes it on the Fx stop hook, with start/stop log lines carrying the database path
  • bolt:// URL parsing — a URL with a host yields a path relative to the working directory; a URL with an empty host yields an absolute path
  • Database file permission guard — an existing database file that users other than the owner can read or write is rejected; files the module creates are mode 0600
  • Bounded lock wait — a database file locked by another process fails after 1 second instead of blocking startup
  • Sentinel errors — every failure wraps one of four exported errors, so callers can classify with errors.Is
  • Strict golangci-lint — the same golden config used across the go-core-fx modules

Built With

  • Go
  • Uber Fx
  • bbolt
  • zap

(back to top)


Getting Started

Prerequisites

  • Go 1.26 or newer (go.mod requires go 1.26.0)
  • golangci-lint v2 (the config targets v2.14.0) for make lint and make fmt
  • GNU Make

Installation

go get github.com/go-core-fx/boltfx

The module has no tagged release yet, so go get resolves a pseudo-version from the default branch until the first tag is cut.

(back to top)


Usage

Module Setup

Add boltfx.Module() to your Fx options and provide a boltfx.Config anywhere in the graph — the module opens the database from it and closes it on stop:

package main

import (
	"github.com/go-core-fx/boltfx"
	"github.com/go-core-fx/logger"
	bolt "go.etcd.io/bbolt"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		logger.Module(),
		fx.Provide(func() boltfx.Config {
			return boltfx.Config{URL: "bolt://data/app.db"}
		}),
		boltfx.Module(),
		fx.Invoke(func(db *bolt.DB) {
			// use the database; it is closed when the application stops
		}),
	).Run()
}

Inside the module, logger.WithNamedLogger("boltfx") decorates the *zap.Logger, so the open, start and shutdown log lines are tagged with the module name:

func Module() fx.Option {
	return fx.Module(
		"boltfx",
		logger.WithNamedLogger("boltfx"),
		fx.Provide(New),
		fx.Invoke(registerLifecycle),
	)
}

The parent directory of the database file is not created — it must exist before the application starts.

Standalone Use

Outside an Fx application, boltfx.New opens the database without tying it to a lifecycle. The caller owns the returned handle and must close it:

db, err := boltfx.New(boltfx.Config{URL: "bolt:///var/lib/app.db"}, logger)
if err != nil {
	return err
}
defer db.Close()

Errors

Every failure wraps one of these exported sentinel errors — classify with errors.Is:

Error Returned when
ErrEmptyURL Config.URL is empty or carries no path (bolt://, bolt:///)
ErrInvalidScheme Config.URL cannot be parsed or its scheme is not bolt
ErrOpenFailed The database file cannot be opened — locked by another process, or the parent is missing
ErrInsecureFile The database file exists with permissions that let users other than the owner access it

(back to top)


Configuration

Database location

The module's only setting is boltfx.Config.URL:

URL form Resolves to
bolt://data/app.db data/app.db, relative to the working directory
bolt:///var/lib/app.db /var/lib/app.db, absolute
empty, bolt:// or bolt:/// rejected with ErrEmptyURL
anything else rejected with ErrInvalidScheme

Behavior worth knowing before production use:

  • The database file is created with mode 0600; an existing file with any other-user access is rejected with ErrInsecureFile
  • A file locked by another process fails after a 1-second timeout with ErrOpenFailed
  • The parent directory is not created

Environment variables

This module itself reads no environment variables. The table below lists everything the repository and its direct dependency consume:

Variable Read by Purpose Format Default Example
DEBUG github.com/go-core-fx/logger (logger.New) Selects the zap development logger (console encoding, debug level) instead of the production logger Boolean accepted by strconv.ParseBool (1, t, T, TRUE, true, …) unset → treated as false → production logger 1
TZ make air (Makefile) Timezone of the development server process IANA timezone name system local timezone UTC
CODECOV_TOKEN Codecov GitHub Action (CI only) Uploads coverage reports from the test workflow job Opaque token, stored as a GitHub Actions secret not set your-codecov-token

(back to top)


Development

Make Targets

Target Description
make all Run all checks: fmt, lint, coverage
make version Display the current version (from git describe, defaults to 0.0.0)
make fmt Run go generate ./..., then golangci-lint fmt
make lint Run golangci-lint run --timeout=5m
make test Run tests with race detector, shuffle and atomic coverage
make coverage Produce coverage.out and an HTML report (coverage.html)
make benchmark Run benchmarks into benchmark.txt
make air Run the development server with air (TZ=UTC DEBUG=1)
make deps Download module dependencies
make gen Run go generate ./...
make clean Remove coverage output, benchmarks, dist/ and bin/
make help List all available targets

Continuous Integration

The Go workflow runs on pushes to master and on pull requests (path-filtered to Go sources, go.mod/go.sum, the lint config and the workflows themselves):

  • Lint — golangci-lint with the repository config
  • Test — race-enabled, shuffled tests with coverage uploaded to Codecov
  • Benchmark — runs go test -bench, keeps history in the Actions cache, uploads results when benchmarks exist and fails the job on a performance alert

A second workflow marks issues and pull requests stale after 7 days of inactivity and closes them 7 days later.

(back to top)


Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please run make all before opening the pull request — it formats, lints and tests the change.

(back to top)


License

Distributed under the Apache License 2.0. See LICENSE for more information.

(back to top)


Acknowledgments

(back to top)

Documentation

Overview

Package boltfx provides an fx-managed bbolt (BoltDB) handle.

It opens a single embedded key/value database file from a URL and ties its lifetime to the fx application lifecycle: the handle is provided as a *bbolt.DB and closed when the application stops.

The package deliberately depends on nothing but the standard library, fx, zap, go-core-fx/logger and bbolt, at the module path github.com/go-core-fx/boltfx.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyURL is returned when Config.URL carries no database location,
	// either because it is empty or because it holds no path at all (for
	// example "bolt://" or "bolt:///").
	ErrEmptyURL = errors.New("url is empty")

	// ErrInvalidScheme is returned when Config.URL cannot be parsed or uses a
	// scheme other than "bolt".
	ErrInvalidScheme = errors.New("invalid url scheme")

	// ErrOpenFailed is returned when the database file cannot be opened, for
	// example because it is locked by another process or because its parent
	// directory does not exist.
	ErrOpenFailed = errors.New("open database")

	// ErrInsecureFile is returned when the database file already exists with
	// permissions that let users other than the owner read or write it. The
	// file holds service state, so startup fails instead of opening it.
	ErrInsecureFile = errors.New("database file is accessible by other users")
)

Functions

func Module

func Module() fx.Option

Module provides an open *bbolt.DB to the rest of the graph.

The database is opened from a Config supplied elsewhere in the graph and closed by a lifecycle hook when the application stops, so consumers must not hold the handle past shutdown.

func New

func New(config Config, logger *zap.Logger) (*bolt.DB, error)

New opens the database described by config and returns the handle.

The URL is parsed with net/url.Parse and must use the "bolt" scheme. A URL with a host (for example "bolt://data/app.db") yields a path relative to the working directory; a URL with an empty host ("bolt:///var/lib/app.db") yields an absolute path. An empty URL, a foreign scheme or a URL without a path is rejected before anything is opened.

The caller owns the returned handle. Inside an fx application use Module instead, which closes the database on stop.

Types

type Config

type Config struct {
	// URL is the database location, for example "bolt://data/app.db" for a
	// relative path or "bolt:///var/lib/app.db" for an absolute one. The
	// scheme must be "bolt". The parent directory is not created: it has to
	// exist before New is called.
	URL string
}

Config describes the database this module opens. It is always a struct, even though it currently holds a single setting, so that the module takes a named value through fx instead of a bare string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址