refactor(embedding): drop init(), build default registry eagerly

Replace the import-time init() that mutated the DefaultRegistry global with a constructor that returns a ready registry, removing mutate-after-init global state. Exported API unchanged.
This commit is contained in:
2026-06-19 14:01:43 +01:00
parent 406b539c84
commit b38ecee4da
2 changed files with 19 additions and 12 deletions
+19 -1
View File
@@ -138,7 +138,25 @@ func (r *ModelRegistry) List() []ModelMetadata {
}
// DefaultRegistry is the global model registry with all available models.
var DefaultRegistry = NewModelRegistry()
// It is constructed eagerly at package load via newDefaultRegistry, which
// registers the built-in BGE model — replacing the previous init()-based
// mutate-after-init pattern while preserving identical behaviour.
var DefaultRegistry = newDefaultRegistry()
// newDefaultRegistry builds a model registry pre-populated with the built-in
// models (currently BGE). Registering at construction time keeps DefaultRegistry
// ready-to-use without a separate init() mutating it after package load.
func newDefaultRegistry() *ModelRegistry {
r := NewModelRegistry()
r.Register(ModelMetadata{
Name: BGEModelName,
Version: BGEModelVersion,
Dimensions: EmbeddingDim,
Description: "High-quality semantic search model",
Default: true,
}, newBGEModel)
return r
}
// RegisterModel adds a model to the default registry.
func RegisterModel(meta ModelMetadata, factory ModelFactory) {
-11
View File
@@ -562,17 +562,6 @@ func (m *bgeModel) Close() error {
return nil
}
// Register the BGE model with the default registry at init time
func init() {
RegisterModel(ModelMetadata{
Name: BGEModelName,
Version: BGEModelVersion,
Dimensions: EmbeddingDim,
Description: "High-quality semantic search model",
Default: true,
}, newBGEModel)
}
// Service provides thread-safe text embedding generation with model abstraction.
type Service struct {
model EmbeddingModel