package postgres import ( "context" "errors" "time" ) var ErrLibraryExecutionBusy = errors.New("target already has an active executor") func (s *Store) WithLibraryExecution(ctx context.Context, instance string, run func(context.Context, func(context.Context) error) error) error { if instance == "" { return errors.New("execution instance identity required") } // A lease expiry is not proof that the old writer has stopped. Keep a // shared PG advisory lock until the synchronous callback (including cleanup) // exits. No deferred unlock may run merely because ctx was cancelled. tx, err := s.db.Begin(ctx) if err != nil { return err } defer func() { cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) defer cancel() _ = tx.Rollback(cleanup) }() var held bool if err = tx.QueryRow(ctx, `SELECT pg_try_advisory_xact_lock(hashtextextended($1,0))`, "library-execution:"+instance).Scan(&held); err != nil { return err } if !held { return ErrLibraryExecutionBusy } ping := func(probe context.Context) error { var one int; return tx.QueryRow(probe, `SELECT 1`).Scan(&one) } return run(ctx, ping) }