Skip to content

Commit 49cbbbb

Browse files
committed
fix
1 parent 2b71f21 commit 49cbbbb

File tree

8 files changed

+47
-81
lines changed

8 files changed

+47
-81
lines changed

clients/grpcc/client.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ import (
1313
"github.com/pubgo/lava/logging/logutil"
1414
"github.com/pubgo/lava/pkg/merge"
1515
"github.com/pubgo/lava/runtime"
16-
17-
// 加载mdns注册中心
18-
_ "github.com/pubgo/lava/core/registry/registry_driver/mdns"
19-
20-
// 加载grpcLog
21-
_ "github.com/pubgo/lava/logging/log_ext/grpclog"
2216
)
2317

2418
var _ grpc.ClientConnInterface = (*Client)(nil)

clients/grpcc/grpcc_config/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ var defaultOpts = []grpc.DialOption{grpc.WithDefaultServiceConfig(`{}`)}
2121

2222
// Cfg ...
2323
type Cfg struct {
24-
Client *ClientCfg `json:"client"`
25-
Addr string `json:"addr"`
26-
Scheme string `json:"scheme"`
27-
Registry string `json:"registry"`
28-
Middlewares []string `json:"plugins"`
24+
Client *ClientCfg `yaml:"client"`
25+
Addr string `yaml:"addr"`
26+
Scheme string `yaml:"scheme"`
27+
Registry string `yaml:"registry"`
28+
Middlewares []string `yaml:"middlewares"`
2929
}
3030

3131
func (t Cfg) Check() error { return nil }

clients/grpcc/grpcc_resolver/discovbuilder.go

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package grpcc_resolver
22

33
import (
44
"context"
5-
"fmt"
65
"sync"
76

87
"github.com/kr/pretty"
@@ -42,11 +41,6 @@ func (d *discovBuilder) updateService(services ...*registry.Service) {
4241
// 更新服务信息
4342
for j := 0; j < Replica; j++ {
4443
addr := n.Address
45-
// 如果port不存在, 那么addr中包含port
46-
//if !strings.Contains(n.Address, ":") {
47-
addr = fmt.Sprintf("%s:%d", "localhost", n.Port)
48-
//}
49-
5044
res := newAddr(addr, services[i].Name)
5145
val, ok := d.services.LoadOrStore(getServiceUniqueId(n.Id, j), &res)
5246
if ok {
@@ -104,40 +98,41 @@ func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, op
10498
w, err := r.Watch(srv)
10599
xerror.PanicF(err, "target.Endpoint: %s", srv)
106100

107-
cancel := syncx.GoCtx(func(ctx context.Context) {
108-
defer func() { xerror.Panic(w.Stop()) }()
101+
return &baseResolver{
102+
cancel: syncx.GoCtx(func(ctx context.Context) {
103+
defer func() { xerror.Panic(w.Stop()) }()
109104

110-
for {
111-
select {
112-
case <-ctx.Done():
113-
return
114-
default:
115-
res, err := w.Next()
116-
if err == registry.ErrWatcherStopped {
105+
for {
106+
select {
107+
case <-ctx.Done():
117108
return
109+
default:
110+
res, err := w.Next()
111+
if err == registry.ErrWatcherStopped {
112+
return
113+
}
114+
115+
if err != nil {
116+
logs.WithErr(err).Error("error")
117+
continue
118+
}
119+
120+
// 注册中心删除服务
121+
if res.Action == event.EventType_DELETE {
122+
d.delService(res.Service)
123+
} else {
124+
d.updateService(res.Service)
125+
}
126+
127+
xerror.TryCatch(func() (interface{}, error) {
128+
var addrList = d.getAddrList(srv)
129+
return nil, cc.UpdateState(newState(addrList))
130+
}, func(err error) {
131+
logs.WithErr(err).Error("update resolver address error")
132+
})
118133
}
119-
120-
if err != nil {
121-
logs.WithErr(err).Error("error")
122-
continue
123-
}
124-
125-
// 注册中心删除服务
126-
if res.Action == event.EventType_DELETE {
127-
d.delService(res.Service)
128-
} else {
129-
d.updateService(res.Service)
130-
}
131-
132-
xerror.TryCatch(func() (interface{}, error) {
133-
var addrList = d.getAddrList(srv)
134-
return nil, cc.UpdateState(newState(addrList))
135-
}, func(err error) {
136-
logs.WithErr(err).Error("update resolver address error")
137-
})
138134
}
139-
}
140-
})
141-
142-
return &baseResolver{cancel: cancel, builder: DiscovScheme}, nil
135+
}),
136+
builder: DiscovScheme,
137+
}, nil
143138
}

clients/grpcc/grpcc_resolver/resolver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package grpcc_resolver
33
import (
44
"context"
55
"fmt"
6-
"github.com/pubgo/lava/logging"
76
"math/rand"
87
"strings"
98

109
"google.golang.org/grpc/attributes"
1110
"google.golang.org/grpc/resolver"
11+
12+
"github.com/pubgo/lava/logging"
1213
)
1314

1415
var logs = logging.Component("balancer.resolver")

clients/grpcc/options.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,10 @@ import (
44
"google.golang.org/grpc"
55

66
"github.com/pubgo/lava/clients/grpcc/grpcc_config"
7-
"github.com/pubgo/lava/clients/grpcc/grpcc_resolver"
87
)
98

109
type Option func(cli *Client)
1110

12-
func WithDirect() func(cfg *grpcc_config.Cfg) {
13-
return func(cfg *grpcc_config.Cfg) { cfg.Scheme = grpcc_resolver.DirectScheme }
14-
}
15-
16-
func WithDns() func(cfg *grpcc_config.Cfg) {
17-
return func(cfg *grpcc_config.Cfg) { cfg.Scheme = grpcc_resolver.DnsScheme }
18-
}
19-
20-
func WithK8s() func(cfg *grpcc_config.Cfg) {
21-
return func(cfg *grpcc_config.Cfg) { cfg.Scheme = grpcc_resolver.K8sScheme }
22-
}
23-
24-
func WithDiscov() func(cfg *grpcc_config.Cfg) {
25-
return func(cfg *grpcc_config.Cfg) { cfg.Scheme = grpcc_resolver.DiscovScheme }
26-
}
27-
2811
func WithDial(fn func(srv string, cfg grpcc_config.Cfg) (grpc.ClientConnInterface, error)) func(cli *Client) {
2912
return func(cli *Client) { cli.dial = fn }
3013
}

cmd/protoc-gen-lava/internal/lava.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
bindingCall = protoutil.Import("github.com/pubgo/lava/pkg/binding")
2828
byteutilCall = protoutil.Import("github.com/pubgo/x/byteutil")
2929
runtimeCall = protoutil.Import("github.com/grpc-ecosystem/grpc-gateway/v2/runtime")
30-
moduleCall = protoutil.Import("github.com/pubgo/lava/module")
30+
injectCall = protoutil.Import("github.com/pubgo/lava/inject")
3131
fxCall = protoutil.Import("go.uber.org/fx")
3232
grpccBuilderCall = protoutil.Import("github.com/pubgo/lava/clients/grpcc/grpcc_builder")
3333
)
@@ -83,7 +83,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
8383
}
8484

8585
func genClient(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
86-
g.QualifiedGoIdent(moduleCall(""))
86+
g.QualifiedGoIdent(injectCall(""))
8787
g.QualifiedGoIdent(fxCall(""))
8888
g.QualifiedGoIdent(grpccBuilderCall(""))
8989
g.P("func Init", service.GoName, "Client(addr string, alias ...string) {")
@@ -95,7 +95,7 @@ func genClient(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedF
9595
conn := grpcc_builder.NewClient(addr)
9696
`)
9797

98-
g.P(` module.Register(fx.Provide(fx.Annotated{`)
98+
g.P(` inject.Register(fx.Provide(fx.Annotated{`)
9999
g.P(`Target: func() `, service.GoName, `Client { return New`, service.GoName, `Client(conn) },`)
100100
g.P(` Name: name,`)
101101
g.P("}))")

runtime/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func init() {
2626
xerror.Panic(err)
2727

2828
Mode = RunMode(i)
29-
xerror.Assert(Mode == RunModeLocal, "unknown mode, mode=%s", val)
29+
xerror.Assert(Mode.String() == "", "unknown mode, mode=%s", val)
3030
return nil
3131
},
3232
},

runtime/runmode.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ func (x RunMode) String() string {
1414
return "prod"
1515
case 4:
1616
return "release"
17+
case 5:
18+
return "release"
1719
default:
18-
return "local"
20+
return ""
1921
}
2022
}
2123

@@ -28,15 +30,6 @@ const (
2830
RunModeRelease RunMode = 5
2931
)
3032

31-
var RunModeValue = map[string]int32{
32-
"local": 0,
33-
"dev": 1,
34-
"test": 2,
35-
"stag": 3,
36-
"prod": 4,
37-
"release": 5,
38-
}
39-
4033
func IsK8s() bool {
4134
return Namespace != ""
4235
}

0 commit comments

Comments
 (0)