ronantakizawa/codereview-bench huggingface dataset:
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan struct{}
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan struct{}),
	}
}

func (s *Service) StartMaintenance() {
	s.start <- struct{}{}
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance

	var timer *time.Timer
	var timerChannel <-chan time.Time
	if s.maintenanceInterval != 0 {
		// Figure out when we last ran maintenance and schedule accordingly. If
		// it was never, do it now.
		lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
		nextMaint := lastMaint.Add(s.maintenanceInterval)
		wait := time.Until(nextMaint)
		if wait < 0 {
			wait = time.Minute
		}
		slog.DebugContext(ctx, "Next periodic run due", "after", wait)
		timer = time.NewTimer(wait)
		timerChannel = timer.C
	}

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()

Review comment (suggestion):
You can decide on the desired semantics here, but typically I'd make this a non-blocking send

```
select {
  case s.start <- struct{}{}:
  default:
}
```

so that if we're already in the middle of running a maintenance when the start command comes in, it becomes a no-op instead of blocking until the maintenance is complete and then immediately starting another one.

Code after:
func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan struct{}
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan struct{}),
	}
}

func (s *Service) StartMaintenance() {
	select {
	case s.start <- struct{}{}:
	default:
	}
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {
		timer.Stop()
	}

	for {
		select {
		case <-ctx.Done():
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
	internalMeta        *db.Typed
	start               chan struct{}
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan struct{}),
	}
}

func (s *Service) StartMaintenance() {
	s.start <- struct{}{}
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance

	var timer *time.Timer
	var timerChannel <-chan time.Time
	if s.maintenanceInterval != 0 {
		// Figure out when we last ran maintenance and schedule accordingly. If
		// it was never, do it now.
		lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
		nextMaint := lastMaint.Add(s.maintenanceInterval)
		wait := time.Until(nextMaint)
		if wait < 0 {
			wait = time.Minute
		}
		slog.DebugContext(ctx, "Next periodic run due", "after", wait)
		timer = time.NewTimer(wait)
		timerChannel = timer.C
	}

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-timerChannel:
		case <-s.start:
		}

		if err := s.periodic(ctx); err != nil {
			return wrap(err)
		}

Review comment (suggestion):
I'm not insisting this be changed, it's fine as-is, but I think it _might_ have been cleaner to initialise the timer with a one minute interval, and then `if s.maintenanceInterval == 0 { timer.Stop() } else { /* the nextMaint calculation */ } `. Then we can still select on `timer.C` directly, and after the loop we check on `s.maintenanceInterval != 0` again for the reset.

Maybe see if it is in fact cleaner, otherwise no biggie.

Code after:
	internalMeta        *db.Typed
	start               chan struct{}
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan struct{}),
	}
}

func (s *Service) StartMaintenance() {
	select {
	case s.start <- struct{}{}:
	default:
	}
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {
		timer.Stop()
	}

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-timer.C:
		case <-s.start:
		}

		if err := s.periodic(ctx); err != nil {
			return wrap(err)
Repository: syncthing/syncthing
File: lib/beacon/multicast.go (Go)

Code before:
}

func readMulticasts(ctx context.Context, outbox chan<- recv, addr string) error {
	gaddr, err := net.ResolveUDPAddr("udp6", addr)
	if err != nil {
		l.Debugln(err)
		return err
	}

	conn, err := net.ListenPacket("udp6", addr)
	if err != nil {
		l.Debugln(err)
		return err
	}
	doneCtx, cancel := context.WithCancel(ctx)
	defer cancel()
	go func() {
		<-doneCtx.Done()
		conn.Close()
	}()

	intfs, err := netutil.Interfaces()
	if err != nil {
		l.Debugln(err)
		return err
	}

	pconn := ipv6.NewPacketConn(conn)
	joined := 0
	for _, intf := range intfs {
		if intf.Flags&net.FlagRunning == 0 || intf.Flags&net.FlagMulticast == 0 {
			continue
		}

		if runtime.GOOS == "android" && intf.Flags&net.FlagPointToPoint != 0 {
			// skip  cellular interfaces
			continue
		}

		err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
		if err != nil {
			l.Debugln("IPv6 join", intf.Name, "failed:", err)
		} else {
			l.Debugln("IPv6 join", intf.Name, "success")
		}
		joined++
	}

	if joined == 0 {
		slog.DebugContext(ctx, "No multicast interfaces available")
		return errors.New("no multicast interfaces available")

Review comment (suggestion):
@marbens-arch I dropped the logging here, as we don't do it for other interface related checks and I would have to single out the multicast check otherwise :sweat_smile: 

Code after:
}

func readMulticasts(ctx context.Context, outbox chan<- recv, addr string) error {
	gaddr, err := net.ResolveUDPAddr("udp6", addr)
	if err != nil {
		l.Debugln(err)
		return err
	}

	conn, err := net.ListenPacket("udp6", addr)
	if err != nil {
		l.Debugln(err)
		return err
	}
	doneCtx, cancel := context.WithCancel(ctx)
	defer cancel()
	go func() {
		<-doneCtx.Done()
		conn.Close()
	}()

	intfs, err := netutil.Interfaces()
	if err != nil {
		l.Debugln(err)
		return err
	}

	pconn := ipv6.NewPacketConn(conn)
	joined := 0
	for _, intf := range intfs {
		if intf.Flags&net.FlagRunning == 0 || intf.Flags&net.FlagMulticast == 0 {
			continue
		}

		if build.IsAndroid && intf.Flags&net.FlagPointToPoint != 0 {
			// skip  cellular interfaces
			continue
		}

		err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
		if err != nil {
			l.Debugln("IPv6 join", intf.Name, "failed:", err)
		} else {
			l.Debugln("IPv6 join", intf.Name, "success")
		}
		joined++
	}

	if joined == 0 {
		slog.DebugContext(ctx, "No multicast interfaces available")
		return errors.New("no multicast interfaces available")
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan struct{}
	finished            *sync.Cond
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan struct{}),
		finished:            sync.NewCond(&sync.Mutex{}),
	}
}

func (s *Service) StartMaintenance(waitForCompletion bool) {
	select {
	case s.start <- struct{}{}:
	default:
	}
	if waitForCompletion {
		s.finished.L.Lock()
		s.finished.Wait()
		s.finished.L.Unlock()
	}
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {

Review comment (suggestion):
The idiomatic way to do this in go would be to return a `<- chan struct{}` which the caller can then choose to wait for or discard. (We'd then have to send that over the `start` channel so the actual maintenance routine can close it when done.)

Code after:
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan chan error
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan chan error),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error, 1)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {
		timer.Stop()
	}

	for {
		var finishChan chan error
		select {
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan (chan error)
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan (chan error)),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {
		timer.Stop()
	}

	for {

Review comment (suggestion):
```suggestion
	finishChan := make(chan error, 1)
```

or we'll block the maintenance routine if the error isn't received

Code after:
func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan chan error
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan chan error),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error, 1)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)

	if s.maintenanceInterval == 0 {
		timer.Stop()
	}

	for {
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
			return ctx.Err()
		case <-timer.C:
		case finishChan = <-s.start:
		}

		err := s.periodic(ctx)
		if finishChan != nil {
			finishChan <- err
		}

		if err != nil {
			return wrap(err)
		}

		if s.maintenanceInterval != 0 {
			timer.Reset(s.maintenanceInterval)
			slog.DebugContext(ctx, "Next periodic run due", "after", s.maintenanceInterval)
		}

		_ = s.internalMeta.PutTime(lastMaintKey, time.Now())
	}
}

func (s *Service) LastMaintenanceTime() (lastMaint time.Time) {
	lastMaint, _, _ = s.internalMeta.Time(lastMaintKey)
	return
}

func (s *Service) periodic(ctx context.Context) error {
	t0 := time.Now()
	slog.DebugContext(ctx, "Periodic start")

	t1 := time.Now()
	defer func() { slog.DebugContext(ctx, "Periodic done in", "t1", time.Since(t1), "t0t1", t1.Sub(t0)) }()

	s.sdb.updateLock.Lock()
	err := tidy(ctx, s.sdb.sql)
	s.sdb.updateLock.Unlock()
	if err != nil {
		return err
	}

	return wrap(s.sdb.forEachFolder(func(fdb *folderDB) error {
		// Get the current device sequence, for comparison in the next step.
		seq, err := fdb.GetDeviceSequence(protocol.LocalDeviceID)
		if err != nil {
			return wrap(err)
		}
		// Get the last successful GC sequence. If it's the same as the
		// current sequence, nothing has changed and we can skip the GC
		// entirely.

Review comment (suggestion):
```suggestion
func (s *Service) LastMaintenanceTime() time.Time {
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	return lastMaint
```

no real need for a named return here

Code after:
			return ctx.Err()
		case <-timer.C:
		case finishChan = <-s.start:
		}

		err := s.periodic(ctx)
		if finishChan != nil {
			finishChan <- err
		}

		if err != nil {
			return wrap(err)
		}

		if s.maintenanceInterval != 0 {
			timer.Reset(s.maintenanceInterval)
			slog.DebugContext(ctx, "Next periodic run due", "after", s.maintenanceInterval)
		}

		_ = s.internalMeta.PutTime(lastMaintKey, time.Now())
	}
}

func (s *Service) LastMaintenanceTime() time.Time {
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	return lastMaint
}

func (s *Service) periodic(ctx context.Context) error {
	t0 := time.Now()
	slog.DebugContext(ctx, "Periodic start")

	t1 := time.Now()
	defer func() { slog.DebugContext(ctx, "Periodic done in", "t1", time.Since(t1), "t0t1", t1.Sub(t0)) }()

	s.sdb.updateLock.Lock()
	err := tidy(ctx, s.sdb.sql)
	s.sdb.updateLock.Unlock()
	if err != nil {
		return err
	}

	return wrap(s.sdb.forEachFolder(func(fdb *folderDB) error {
		// Get the current device sequence, for comparison in the next step.
		seq, err := fdb.GetDeviceSequence(protocol.LocalDeviceID)
		if err != nil {
			return wrap(err)
		}
		// Get the last successful GC sequence. If it's the same as the
		// current sequence, nothing has changed and we can skip the GC
		// entirely.
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:

	"github.com/jmoiron/sqlx"
	"github.com/syncthing/syncthing/internal/db"
	"github.com/syncthing/syncthing/internal/slogutil"
	"github.com/syncthing/syncthing/lib/protocol"
)

const (
	internalMetaPrefix     = "dbsvc"
	lastMaintKey           = "lastMaint"
	lastSuccessfulGCSeqKey = "lastSuccessfulGCSeq"

	gcMinChunks  = 5
	gcChunkSize  = 100_000         // approximate number of rows to process in a single gc query
	gcMaxRuntime = 5 * time.Minute // max time to spend on gc, per table, per run
)

func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan (chan error)
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan (chan error)),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {

Review comment (suggestion):
```suggestion
	start               chan chan error
```

Code after:

	"github.com/jmoiron/sqlx"
	"github.com/syncthing/syncthing/internal/db"
	"github.com/syncthing/syncthing/internal/slogutil"
	"github.com/syncthing/syncthing/lib/protocol"
)

const (
	internalMetaPrefix     = "dbsvc"
	lastMaintKey           = "lastMaint"
	lastSuccessfulGCSeqKey = "lastSuccessfulGCSeq"

	gcMinChunks  = 5
	gcChunkSize  = 100_000         // approximate number of rows to process in a single gc query
	gcMaxRuntime = 5 * time.Minute // max time to spend on gc, per table, per run
)

func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan chan error
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan chan error),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error, 1)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
Repository: syncthing/syncthing
File: internal/db/sqlite/db_service.go (Go)

Code before:
	gcMinChunks  = 5
	gcChunkSize  = 100_000         // approximate number of rows to process in a single gc query
	gcMaxRuntime = 5 * time.Minute // max time to spend on gc, per table, per run
)

func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan (chan error)
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan (chan error)),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)


Review comment (suggestion):
```suggestion
		start:               make(chan chan error),
```

Code after:
	gcMinChunks  = 5
	gcChunkSize  = 100_000         // approximate number of rows to process in a single gc query
	gcMaxRuntime = 5 * time.Minute // max time to spend on gc, per table, per run
)

func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
	return newService(s, maintenanceInterval)
}

type Service struct {
	sdb                 *DB
	maintenanceInterval time.Duration
	internalMeta        *db.Typed
	start               chan chan error
}

func (s *Service) String() string {
	return fmt.Sprintf("sqlite.service@%p", s)
}

func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
	return &Service{
		sdb:                 sdb,
		maintenanceInterval: maintenanceInterval,
		internalMeta:        db.NewTyped(sdb, internalMetaPrefix),
		start:               make(chan chan error),
	}
}

func (s *Service) StartMaintenance() <-chan error {
	finishChan := make(chan error, 1)
	select {
	case s.start <- finishChan:
	default:
	}
	return finishChan
}

func (s *Service) Serve(ctx context.Context) error {
	// Run periodic maintenance
	// Figure out when we last ran maintenance and schedule accordingly. If
	// it was never, do it now.
	lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
	nextMaint := lastMaint.Add(s.maintenanceInterval)
	wait := time.Until(nextMaint)
	if wait < 0 {
		wait = time.Minute
	}
	slog.DebugContext(ctx, "Next periodic run due", "after", wait)
	timer := time.NewTimer(wait)