mirror of
				https://github.com/go-kratos/kratos.git
				synced 2025-10-30 23:47:59 +02:00 
			
		
		
		
	chore: fix words and modify some syntax declarations (#3113)
* chore:fix words * fix:filter nodes len preallocate;replace func as cancelFunc; fix eureka mutex
This commit is contained in:
		
							
								
								
									
										2
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								app.go
									
									
									
									
									
								
							| @@ -30,7 +30,7 @@ type AppInfo interface { | ||||
| type App struct { | ||||
| 	opts     options | ||||
| 	ctx      context.Context | ||||
| 	cancel   func() | ||||
| 	cancel   context.CancelFunc | ||||
| 	mu       sync.Mutex | ||||
| 	instance *registry.ServiceInstance | ||||
| } | ||||
|   | ||||
| @@ -11,7 +11,7 @@ import ( | ||||
| 	"github.com/go-kratos/kratos/v2/config" | ||||
| ) | ||||
|  | ||||
| // Option is etcd config option. | ||||
| // Option is consul config option. | ||||
| type Option func(o *options) | ||||
|  | ||||
| type options struct { | ||||
|   | ||||
| @@ -58,9 +58,9 @@ func (e *API) broadcast() { | ||||
| 	for _, subscriber := range e.subscribers { | ||||
| 		go subscriber.callBack() | ||||
| 	} | ||||
| 	defer e.lock.Unlock() | ||||
| 	e.lock.Lock() | ||||
| 	e.allInstances = instances | ||||
| 	e.lock.Unlock() | ||||
| } | ||||
|  | ||||
| func (e *API) cacheAllInstances() map[string][]Instance { | ||||
| @@ -106,12 +106,12 @@ func (e *API) Deregister(ctx context.Context, endpoints []Endpoint) error { | ||||
|  | ||||
| func (e *API) Subscribe(serverName string, fn func()) error { | ||||
| 	e.lock.Lock() | ||||
| 	defer e.lock.Unlock() | ||||
| 	appID := e.ToAppID(serverName) | ||||
| 	e.subscribers[appID] = &subscriber{ | ||||
| 		appID:    appID, | ||||
| 		callBack: fn, | ||||
| 	} | ||||
| 	e.lock.Unlock() | ||||
| 	go e.broadcast() | ||||
| 	return nil | ||||
| } | ||||
| @@ -128,8 +128,8 @@ func (e *API) GetService(ctx context.Context, serverName string) []Instance { | ||||
|  | ||||
| func (e *API) Unsubscribe(serverName string) { | ||||
| 	e.lock.Lock() | ||||
| 	defer e.lock.Unlock() | ||||
| 	delete(e.subscribers, e.ToAppID(serverName)) | ||||
| 	e.lock.Unlock() | ||||
| } | ||||
|  | ||||
| func (e *API) ToAppID(serverName string) string { | ||||
|   | ||||
| @@ -54,7 +54,11 @@ func New(eurekaUrls []string, opts ...Option) (*Registry, error) { | ||||
| 		o(r) | ||||
| 	} | ||||
|  | ||||
| 	client := NewClient(eurekaUrls, WithHeartbeatInterval(r.heartbeatInterval), WithClientContext(r.ctx), WithNamespace(r.eurekaPath)) | ||||
| 	client := NewClient(eurekaUrls, | ||||
| 		WithHeartbeatInterval(r.heartbeatInterval), | ||||
| 		WithClientContext(r.ctx), | ||||
| 		WithNamespace(r.eurekaPath), | ||||
| 	) | ||||
| 	r.api = NewAPI(r.ctx, client, r.refreshInterval) | ||||
| 	return r, nil | ||||
| } | ||||
|   | ||||
| @@ -376,7 +376,7 @@ func (w *Watcher) Next() ([]*registry.ServiceInstance, error) { | ||||
| 							if serviceInstance.ID == instance.GetId() { | ||||
| 								// remove equal | ||||
| 								if len(w.ServiceInstances) <= 1 { | ||||
| 									w.ServiceInstances = w.ServiceInstances[0:0] | ||||
| 									w.ServiceInstances = w.ServiceInstances[:0] | ||||
| 									continue | ||||
| 								} | ||||
| 								w.ServiceInstances = append(w.ServiceInstances[:i], w.ServiceInstances[i+1:]...) | ||||
|   | ||||
| @@ -9,7 +9,7 @@ import ( | ||||
| // Version is version filter. | ||||
| func Version(version string) selector.NodeFilter { | ||||
| 	return func(_ context.Context, nodes []selector.Node) []selector.Node { | ||||
| 		newNodes := make([]selector.Node, 0) | ||||
| 		newNodes := make([]selector.Node, 0, len(nodes)) | ||||
| 		for _, n := range nodes { | ||||
| 			if n.Version() == version { | ||||
| 				newNodes = append(newNodes, n) | ||||
|   | ||||
| @@ -39,7 +39,7 @@ func WithEndpoint(endpoint string) ClientOption { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithSubset with client disocvery subset size. | ||||
| // WithSubset with client discovery subset size. | ||||
| // zero value means subset filter disabled | ||||
| func WithSubset(size int) ClientOption { | ||||
| 	return func(o *clientOptions) { | ||||
|   | ||||
| @@ -6,6 +6,8 @@ import ( | ||||
| 	"google.golang.org/grpc/resolver" | ||||
| ) | ||||
|  | ||||
| const name = "direct" | ||||
|  | ||||
| func init() { | ||||
| 	resolver.Register(NewBuilder()) | ||||
| } | ||||
| @@ -35,5 +37,5 @@ func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ | ||||
| } | ||||
|  | ||||
| func (d *directBuilder) Scheme() string { | ||||
| 	return "direct" | ||||
| 	return name | ||||
| } | ||||
|   | ||||
| @@ -38,6 +38,14 @@ func TestDisableDebugLog(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestPrintDebugLog(t *testing.T) { | ||||
| 	o := &builder{} | ||||
| 	PrintDebugLog(true)(o) | ||||
| 	if !o.debugLog { | ||||
| 		t.Errorf("expected PrintdebugLog true, got %v", o.debugLog) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type mockDiscovery struct{} | ||||
|  | ||||
| func (m *mockDiscovery) GetService(_ context.Context, _ string) ([]*registry.ServiceInstance, error) { | ||||
|   | ||||
| @@ -56,7 +56,7 @@ type clientOptions struct { | ||||
| 	subsetSize   int | ||||
| } | ||||
|  | ||||
| // WithSubset with client disocvery subset size. | ||||
| // WithSubset with client discovery subset size. | ||||
| // zero value means subset filter disabled | ||||
| func WithSubset(size int) ClientOption { | ||||
| 	return func(o *clientOptions) { | ||||
|   | ||||
| @@ -56,7 +56,7 @@ type resolver struct { | ||||
| func newResolver(ctx context.Context, discovery registry.Discovery, target *Target, | ||||
| 	rebalancer selector.Rebalancer, block, insecure bool, subsetSize int, | ||||
| ) (*resolver, error) { | ||||
| 	// this is new resovler | ||||
| 	// this is new resolver | ||||
| 	watcher, err := discovery.Watch(ctx, target.Endpoint) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|   | ||||
| @@ -134,7 +134,7 @@ func Listener(lis net.Listener) ServerOption { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // PathPrefix with mux's PathPrefix, router will replaced by a subrouter that start with prefix. | ||||
| // PathPrefix with mux's PathPrefix, router will be replaced by a subrouter that start with prefix. | ||||
| func PathPrefix(prefix string) ServerOption { | ||||
| 	return func(s *Server) { | ||||
| 		s.router = s.router.PathPrefix(prefix).Subrouter() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user