feat: initial commit
This commit is contained in:
54
reach/client/protocol/registry.go
Normal file
54
reach/client/protocol/registry.go
Normal file
@ -0,0 +1,54 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
protocols map[Identifier]Protocol
|
||||
}
|
||||
|
||||
func (r *Registry) Register(protocol Protocol) {
|
||||
r.protocols[protocol.Identifier()] = protocol
|
||||
}
|
||||
|
||||
func (r *Registry) Availables(ctx context.Context, addr string) ([]Protocol, error) {
|
||||
availables := make([]Protocol, 0)
|
||||
|
||||
for _, proto := range r.protocols {
|
||||
available, err := proto.Available(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if !available {
|
||||
continue
|
||||
}
|
||||
|
||||
availables = append(availables, proto)
|
||||
}
|
||||
|
||||
return availables, nil
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
protocols: make(map[Identifier]Protocol),
|
||||
}
|
||||
}
|
||||
|
||||
var defaultRegistry = NewRegistry()
|
||||
|
||||
func DefaultRegistry() *Registry {
|
||||
return defaultRegistry
|
||||
}
|
||||
|
||||
func Register(protocol Protocol) {
|
||||
defaultRegistry.Register(protocol)
|
||||
}
|
||||
|
||||
func Availables(ctx context.Context, addr string) ([]Protocol, error) {
|
||||
return defaultRegistry.Availables(ctx, addr)
|
||||
}
|
Reference in New Issue
Block a user