acce86c0e4
Log close errors in Unregister instead of silently discarding them. Add doc comments to all exported symbols across 20 registry files. Add comprehensive tests covering base registry, all wrapper types, hot-reload delegation, nil-fallback semantics, and global accessors.
44 lines
1004 B
Go
44 lines
1004 B
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-gost/core/recorder"
|
|
)
|
|
|
|
// recorderRegistry implements a hot-reload-safe registry for recorder.Recorder.
|
|
type recorderRegistry struct {
|
|
registry[recorder.Recorder]
|
|
}
|
|
|
|
// Register stores a Recorder under the given name.
|
|
func (r *recorderRegistry) Register(name string, v recorder.Recorder) error {
|
|
return r.registry.Register(name, v)
|
|
}
|
|
|
|
// Get returns a wrapper that delegates to the currently registered Recorder.
|
|
// Returns nil if name is empty.
|
|
func (r *recorderRegistry) Get(name string) recorder.Recorder {
|
|
if name != "" {
|
|
return &recorderWrapper{name: name, r: r}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *recorderRegistry) get(name string) recorder.Recorder {
|
|
return r.registry.Get(name)
|
|
}
|
|
|
|
type recorderWrapper struct {
|
|
name string
|
|
r *recorderRegistry
|
|
}
|
|
|
|
func (w *recorderWrapper) Record(ctx context.Context, b []byte, opts ...recorder.RecordOption) error {
|
|
v := w.r.get(w.name)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.Record(ctx, b, opts...)
|
|
}
|