1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-11-29 21:47:44 +02:00

feat: add test framework & refactor RPC server (#2579)

Co-authored-by: Rene Jochum <rene@jochum.dev>
This commit is contained in:
David Brouwer
2022-10-20 13:00:50 +02:00
committed by GitHub
parent c25dee7c8a
commit a3980c2308
54 changed files with 3703 additions and 2497 deletions

View File

@@ -1,8 +1,11 @@
package selector
import (
"sync"
"time"
"github.com/pkg/errors"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/registry/cache"
)
@@ -10,19 +13,25 @@ import (
type registrySelector struct {
so Options
rc cache.Cache
mu sync.RWMutex
}
func (c *registrySelector) newCache() cache.Cache {
opts := make([]cache.Option, 0, 1)
if c.so.Context != nil {
if t, ok := c.so.Context.Value("selector_ttl").(time.Duration); ok {
opts = append(opts, cache.WithTTL(t))
}
}
return cache.New(c.so.Registry, opts...)
}
func (c *registrySelector) Init(opts ...Option) error {
c.mu.Lock()
defer c.mu.Unlock()
for _, o := range opts {
o(&c.so)
}
@@ -38,6 +47,9 @@ func (c *registrySelector) Options() Options {
}
func (c *registrySelector) Select(service string, opts ...SelectOption) (Next, error) {
c.mu.RLock()
defer c.mu.RUnlock()
sopts := SelectOptions{
Strategy: c.so.Strategy,
}
@@ -51,9 +63,10 @@ func (c *registrySelector) Select(service string, opts ...SelectOption) (Next, e
// if that fails go directly to the registry
services, err := c.rc.GetService(service)
if err != nil {
if err == registry.ErrNotFound {
if errors.Is(err, registry.ErrNotFound) {
return nil, ErrNotFound
}
return nil, err
}
@@ -87,6 +100,7 @@ func (c *registrySelector) String() string {
return "registry"
}
// NewSelector creates a new default selector.
func NewSelector(opts ...Option) Selector {
sopts := Options{
Strategy: Random,