Skip to content

Commit

Permalink
chore: fix words and modify some syntax declarations (#3113)
Browse files Browse the repository at this point in the history
* chore:fix words

* fix:filter nodes len preallocate;replace func as cancelFunc; fix eureka mutex
  • Loading branch information
yonwoo9 authored Dec 14, 2023
1 parent 4f614be commit 1bb98b6
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions contrib/registry/eureka/eureka.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion contrib/registry/eureka/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/polaris/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]...)
Expand Down
2 changes: 1 addition & 1 deletion selector/filter/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion transport/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion transport/grpc/resolver/direct/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"google.golang.org/grpc/resolver"
)

const name = "direct"

func init() {
resolver.Register(NewBuilder())
}
Expand Down Expand Up @@ -35,5 +37,5 @@ func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _
}

func (d *directBuilder) Scheme() string {
return "direct"
return name
}
8 changes: 8 additions & 0 deletions transport/grpc/resolver/discovery/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion transport/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion transport/http/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1bb98b6

Please sign in to comment.