Skip to content

Commit 802d151

Browse files
committed
update
1 parent fd9656e commit 802d151

29 files changed

+551
-607
lines changed

grpc/balancer.go cgrpc/balancer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"context"
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type balancerBuilder struct {
17-
c *GrpcClient
17+
c *CGrpcClient
1818
}
1919

2020
func (b *balancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
@@ -32,7 +32,7 @@ func (b *balancerBuilder) Name() string {
3232
}
3333

3434
type corelibBalancer struct {
35-
c *GrpcClient
35+
c *CGrpcClient
3636
cc balancer.ClientConn
3737
servers map[balancer.SubConn]*ServerForPick
3838
pservers []*ServerForPick
@@ -167,12 +167,12 @@ func (b *corelibBalancer) UpdateSubConnState(sc balancer.SubConn, s balancer.Sub
167167
return
168168
}
169169
if s.ConnectivityState == connectivity.Idle && atomic.LoadInt32(&exist.status) == int32(connectivity.Ready) {
170-
log.Info(nil, "[grpc.client] server:", b.c.appname+":"+exist.addr, "offline")
170+
log.Info(nil, "[cgrpc.client] server:", b.c.appname+":"+exist.addr, "offline")
171171
} else if s.ConnectivityState == connectivity.Ready {
172172
b.c.resolver.wakemanual()
173-
log.Info(nil, "[grpc.client] server:", b.c.appname+":"+exist.addr, "online")
173+
log.Info(nil, "[cgrpc.client] server:", b.c.appname+":"+exist.addr, "online")
174174
} else if s.ConnectivityState == connectivity.TransientFailure {
175-
log.Error(nil, "[grpc.client] connect to server:", b.c.appname+":"+exist.addr, "error:", s.ConnectionError)
175+
log.Error(nil, "[cgrpc.client] connect to server:", b.c.appname+":"+exist.addr, "error:", s.ConnectionError)
176176
}
177177
olds := atomic.LoadInt32(&exist.status)
178178
atomic.StoreInt32(&exist.status, int32(s.ConnectivityState))

grpc/client.go cgrpc/client.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"context"
@@ -30,7 +30,7 @@ import (
3030

3131
type PickHandler func(servers []*ServerForPick) *ServerForPick
3232

33-
type DiscoveryHandler func(group, name string, manually <-chan *struct{}, client *GrpcClient)
33+
type DiscoveryHandler func(group, name string, manually <-chan *struct{}, client *CGrpcClient)
3434

3535
type ClientConfig struct {
3636
ConnTimeout time.Duration
@@ -76,7 +76,7 @@ func (c *ClientConfig) validate() {
7676
}
7777
}
7878

79-
type GrpcClient struct {
79+
type CGrpcClient struct {
8080
c *ClientConfig
8181
selfappname string
8282
appname string
@@ -85,7 +85,7 @@ type GrpcClient struct {
8585
balancer *corelibBalancer
8686
}
8787

88-
func NewGrpcClient(c *ClientConfig, selfgroup, selfname, group, name string) (*GrpcClient, error) {
88+
func NewCGrpcClient(c *ClientConfig, selfgroup, selfname, group, name string) (*CGrpcClient, error) {
8989
if e := common.NameCheck(selfname, false, true, false, true); e != nil {
9090
return nil, e
9191
}
@@ -107,17 +107,17 @@ func NewGrpcClient(c *ClientConfig, selfgroup, selfname, group, name string) (*G
107107
return nil, e
108108
}
109109
if c == nil {
110-
return nil, errors.New("[grpc.client] missing config")
110+
return nil, errors.New("[cgrpc.client] missing config")
111111
}
112112
if c.Discover == nil {
113-
return nil, errors.New("[grpc.client] missing discover in config")
113+
return nil, errors.New("[cgrpc.client] missing discover in config")
114114
}
115115
if c.Picker == nil {
116-
log.Warning(nil, "[grpc.client] missing picker in config,default picker will be used")
116+
log.Warning(nil, "[cgrpc.client] missing picker in config,default picker will be used")
117117
c.Picker = defaultPicker
118118
}
119119
c.validate()
120-
clientinstance := &GrpcClient{
120+
clientinstance := &CGrpcClient{
121121
c: c,
122122
selfappname: selfappname,
123123
appname: appname,
@@ -133,10 +133,10 @@ func NewGrpcClient(c *ClientConfig, selfgroup, selfname, group, name string) (*G
133133
for _, cert := range c.CAs {
134134
certPEM, e := os.ReadFile(cert)
135135
if e != nil {
136-
return nil, errors.New("[grpc.client] read cert file:" + cert + " error:" + e.Error())
136+
return nil, errors.New("[cgrpc.client] read cert file:" + cert + " error:" + e.Error())
137137
}
138138
if !certpool.AppendCertsFromPEM(certPEM) {
139-
return nil, errors.New("[grpc.client] load cert file:" + cert + " error:" + e.Error())
139+
return nil, errors.New("[cgrpc.client] load cert file:" + cert + " error:" + e.Error())
140140
}
141141
}
142142
}
@@ -176,7 +176,7 @@ type RegisterData struct {
176176
}
177177

178178
//all: key server's addr "ip:port"
179-
func (c *GrpcClient) UpdateDiscovery(all map[string]*RegisterData) {
179+
func (c *CGrpcClient) UpdateDiscovery(all map[string]*RegisterData) {
180180
s := resolver.State{
181181
Addresses: make([]resolver.Address, 0, len(all)),
182182
}
@@ -194,7 +194,7 @@ func (c *GrpcClient) UpdateDiscovery(all map[string]*RegisterData) {
194194
}
195195
c.resolver.cc.UpdateState(s)
196196
}
197-
func (c *GrpcClient) Call(ctx context.Context, path string, req interface{}, resp interface{}, metadata map[string]string) error {
197+
func (c *CGrpcClient) Call(ctx context.Context, path string, req interface{}, resp interface{}, metadata map[string]string) error {
198198
start := time.Now()
199199
if c.c.GlobalTimeout != 0 {
200200
var cancel context.CancelFunc

grpc/context.go cgrpc/context.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"context"
@@ -7,7 +7,7 @@ import (
77
"google.golang.org/protobuf/reflect/protoreflect"
88
)
99

10-
func (s *GrpcServer) getcontext(c context.Context, path string, peername string, peeraddr string, metadata map[string]string, handlers []OutsideHandler, d func(interface{}) error) *Context {
10+
func (s *CGrpcServer) getcontext(c context.Context, path string, peername string, peeraddr string, metadata map[string]string, handlers []OutsideHandler, d func(interface{}) error) *Context {
1111
ctx, ok := s.ctxpool.Get().(*Context)
1212
if !ok {
1313
return &Context{
@@ -35,7 +35,7 @@ func (s *GrpcServer) getcontext(c context.Context, path string, peername string,
3535
ctx.status = 0
3636
return ctx
3737
}
38-
func (s *GrpcServer) putcontext(ctx *Context) {
38+
func (s *CGrpcServer) putcontext(ctx *Context) {
3939
s.ctxpool.Put(ctx)
4040
}
4141

@@ -66,7 +66,7 @@ func (c *Context) Abort(e error) {
6666
c.status = -1
6767
c.e = cerror.ConvertStdError(e)
6868
if c.e != nil && (c.e.Httpcode < 400 || c.e.Httpcode > 999) {
69-
panic("[grpc.Context.Abort] httpcode must in [400,999]")
69+
panic("[cgrpc.Context.Abort] httpcode must in [400,999]")
7070
}
7171
}
7272

grpc/error.go cgrpc/error.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"net/http"
@@ -7,9 +7,9 @@ import (
77
)
88

99
var (
10-
ErrNoapi = &error.Error{Code: 3001, Httpcode: http.StatusNotImplemented, Msg: "[grpc] api not implement"}
11-
errClosing = &error.Error{Code: 3002, Httpcode: http.StatusInternalServerError, Msg: "[grpc] server is closing"}
12-
ErrPanic = &error.Error{Code: 3003, Httpcode: http.StatusServiceUnavailable, Msg: "[grpc] server panic"}
13-
ErrNoserver = &error.Error{Code: 3004, Httpcode: http.StatusServiceUnavailable, Msg: "[grpc] no servers"}
14-
ErrClosed = &error.Error{Code: 3005, Httpcode: http.StatusInternalServerError, Msg: "[grpc] connection closed"}
10+
ErrNoapi = &error.Error{Code: 3001, Httpcode: http.StatusNotImplemented, Msg: "[cgrpc] api not implement"}
11+
errClosing = &error.Error{Code: 3002, Httpcode: http.StatusInternalServerError, Msg: "[cgrpc] server is closing"}
12+
ErrPanic = &error.Error{Code: 3003, Httpcode: http.StatusServiceUnavailable, Msg: "[cgrpc] server panic"}
13+
ErrNoserver = &error.Error{Code: 3004, Httpcode: http.StatusServiceUnavailable, Msg: "[cgrpc] no servers"}
14+
ErrClosed = &error.Error{Code: 3005, Httpcode: http.StatusInternalServerError, Msg: "[cgrpc] connection closed"}
1515
)

grpc/mids/mids.go cgrpc/mids/mids.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package mids
22

33
import (
4+
"github.com/chenjie199234/Corelib/cgrpc"
45
cerror "github.com/chenjie199234/Corelib/error"
5-
"github.com/chenjie199234/Corelib/grpc"
66
publicmids "github.com/chenjie199234/Corelib/mids"
77
)
88

99
//dosn't include global mids in here
10-
var all map[string]grpc.OutsideHandler
10+
var all map[string]cgrpc.OutsideHandler
1111

1212
func init() {
13-
all = make(map[string]grpc.OutsideHandler)
13+
all = make(map[string]cgrpc.OutsideHandler)
1414
//register here
1515
all["rate"] = rate
1616
}
1717

18-
func AllMids() map[string]grpc.OutsideHandler {
18+
func AllMids() map[string]cgrpc.OutsideHandler {
1919
return all
2020
}
2121

22-
func rate(ctx *grpc.Context) {
22+
func rate(ctx *cgrpc.Context) {
2323
if !publicmids.GrpcRate(ctx.GetPath()) {
2424
ctx.Abort(cerror.ErrLimit)
2525
}

grpc/picker.go cgrpc/picker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"math"
File renamed without changes.

grpc/protoc-gen-go-grpc/grpc.go cgrpc/protoc-gen-go-cgrpc/cgrpc.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ const (
1616
regexpPackage = protogen.GoImportPath("regexp")
1717
contextPackage = protogen.GoImportPath("context")
1818
protoPackage = protogen.GoImportPath("google.golang.org/protobuf/proto")
19-
grpcPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/grpc")
19+
cgrpcPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/cgrpc")
2020
logPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/log")
2121
commonPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/util/common")
2222
metadataPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/metadata")
2323
errorPackage = protogen.GoImportPath("github.com/chenjie199234/Corelib/error")
2424
)
2525

2626
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
27-
filename := file.GeneratedFilenamePrefix + "_grpc.pb.go"
27+
filename := file.GeneratedFilenamePrefix + "_cgrpc.pb.go"
2828
g := gen.NewGeneratedFile(filename, file.GoImportPath)
2929

3030
genFileComment(gen, file, g)
@@ -42,7 +42,7 @@ func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
4242
}
4343
func genFileComment(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
4444
//add version comments
45-
g.P("// Code generated by protoc-gen-go-grpc. DO NOT EDIT.")
45+
g.P("// Code generated by protoc-gen-go-cgrpc. DO NOT EDIT.")
4646
g.P("// version:")
4747
protocVersion := "(unknown)"
4848
if v := gen.Request.GetCompilerVersion(); v != nil {
@@ -51,7 +51,7 @@ func genFileComment(gen *protogen.Plugin, file *protogen.File, g *protogen.Gener
5151
protocVersion += "-" + s
5252
}
5353
}
54-
g.P("// \tprotoc-gen-go-grpc ", version)
54+
g.P("// \tprotoc-gen-go-cgrpc ", version)
5555
g.P("// \tprotoc ", protocVersion)
5656
g.P("// source: ", file.Desc.Path())
5757
g.P()
@@ -68,15 +68,15 @@ func genPath(file *protogen.File, service *protogen.Service, g *protogen.Generat
6868
if method.Desc.Options().(*descriptorpb.MethodOptions).GetDeprecated() {
6969
continue
7070
}
71-
pathname := "_GrpcPath" + service.GoName + method.GoName
71+
pathname := "_CGrpcPath" + service.GoName + method.GoName
7272
pathurl := "/" + *file.Proto.Package + "." + string(service.Desc.Name()) + "/" + string(method.Desc.Name())
7373
g.P("var ", pathname, "=", strconv.Quote(pathurl))
7474
}
7575
g.P()
7676
}
7777
func genServer(file *protogen.File, service *protogen.Service, g *protogen.GeneratedFile) {
7878
// Server interface.
79-
serverName := service.GoName + "GrpcServer"
79+
serverName := service.GoName + "CGrpcServer"
8080

8181
g.P("type ", serverName, " interface {")
8282
for _, method := range service.Methods {
@@ -94,11 +94,11 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
9494
if method.Desc.Options().(*descriptorpb.MethodOptions).GetDeprecated() {
9595
continue
9696
}
97-
fname := "func _" + service.GoName + "_" + method.GoName + "_" + "GrpcHandler"
97+
fname := "func _" + service.GoName + "_" + method.GoName + "_" + "CGrpcHandler"
9898
p1 := "handler func (" + g.QualifiedGoIdent(contextPackage.Ident("Context")) + ",*" + g.QualifiedGoIdent(method.Input.GoIdent) + ")(*" + g.QualifiedGoIdent(method.Output.GoIdent) + ",error)"
99-
freturn := g.QualifiedGoIdent(grpcPackage.Ident("OutsideHandler"))
99+
freturn := g.QualifiedGoIdent(cgrpcPackage.Ident("OutsideHandler"))
100100
g.P(fname, "(", p1, ")", freturn, "{")
101-
g.P("return func(ctx *", g.QualifiedGoIdent(grpcPackage.Ident("Context")), "){")
101+
g.P("return func(ctx *", g.QualifiedGoIdent(cgrpcPackage.Ident("Context")), "){")
102102
g.P("req:=new(", g.QualifiedGoIdent(method.Input.GoIdent), ")")
103103
g.P("if ctx.DecodeReq(req)!=nil{")
104104
g.P("ctx.Abort(", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
@@ -128,7 +128,7 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
128128
}
129129

130130
//Server Register
131-
g.P("func Register", serverName, "(engine *", g.QualifiedGoIdent(grpcPackage.Ident("GrpcServer")), ",svc ", serverName, ",allmids map[string]", g.QualifiedGoIdent(grpcPackage.Ident("OutsideHandler")), "){")
131+
g.P("func Register", serverName, "(engine *", g.QualifiedGoIdent(cgrpcPackage.Ident("CGrpcServer")), ",svc ", serverName, ",allmids map[string]", g.QualifiedGoIdent(cgrpcPackage.Ident("OutsideHandler")), "){")
132132
g.P("//avoid lint")
133133
g.P("_=allmids")
134134
for _, method := range service.Methods {
@@ -137,10 +137,10 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
137137
continue
138138
}
139139
var mids []string
140-
if proto.HasExtension(mop, pbex.E_GrpcMidwares) {
141-
mids = proto.GetExtension(mop, pbex.E_GrpcMidwares).([]string)
140+
if proto.HasExtension(mop, pbex.E_CgrpcMidwares) {
141+
mids = proto.GetExtension(mop, pbex.E_CgrpcMidwares).([]string)
142142
}
143-
fname := "_" + service.GoName + "_" + method.GoName + "_" + "GrpcHandler(svc." + method.GoName + ")"
143+
fname := "_" + service.GoName + "_" + method.GoName + "_" + "CGrpcHandler(svc." + method.GoName + ")"
144144
if len(mids) > 0 {
145145
g.P("{")
146146
str := ""
@@ -150,7 +150,7 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
150150
}
151151
str = str[1:]
152152
g.P("requiredMids:=[]string{", str, "}")
153-
g.P("mids:=make([]", g.QualifiedGoIdent(grpcPackage.Ident("OutsideHandler")), ",0,", len(mids)+1, ")")
153+
g.P("mids:=make([]", g.QualifiedGoIdent(cgrpcPackage.Ident("OutsideHandler")), ",0,", len(mids)+1, ")")
154154
g.P("for _,v:=range requiredMids{")
155155
g.P("if mid,ok:=allmids[v];ok{")
156156
g.P("mids = append(mids,mid)")
@@ -169,7 +169,7 @@ func genServer(file *protogen.File, service *protogen.Service, g *protogen.Gener
169169
}
170170
func genClient(file *protogen.File, service *protogen.Service, g *protogen.GeneratedFile) {
171171
// Client interface.
172-
clientName := service.GoName + "GrpcClient"
172+
clientName := service.GoName + "CGrpcClient"
173173
lowclientName := strings.ToLower(clientName[:1]) + clientName[1:]
174174

175175
g.P("type ", clientName, " interface {")
@@ -184,9 +184,9 @@ func genClient(file *protogen.File, service *protogen.Service, g *protogen.Gener
184184
g.P("}")
185185
g.P()
186186
g.P("type ", lowclientName, " struct{")
187-
g.P("cc *", g.QualifiedGoIdent(grpcPackage.Ident("GrpcClient")))
187+
g.P("cc *", g.QualifiedGoIdent(cgrpcPackage.Ident("CGrpcClient")))
188188
g.P("}")
189-
g.P("func New", clientName, "(c *", g.QualifiedGoIdent(grpcPackage.Ident("GrpcClient")), ")(", clientName, "){")
189+
g.P("func New", clientName, "(c *", g.QualifiedGoIdent(cgrpcPackage.Ident("CGrpcClient")), ")(", clientName, "){")
190190
g.P("return &", lowclientName, "{cc:c}")
191191
g.P("}")
192192
g.P()
@@ -196,7 +196,7 @@ func genClient(file *protogen.File, service *protogen.Service, g *protogen.Gener
196196
if mop.GetDeprecated() {
197197
continue
198198
}
199-
pathname := "_GrpcPath" + service.GoName + method.GoName
199+
pathname := "_CGrpcPath" + service.GoName + method.GoName
200200
p1 := "ctx " + g.QualifiedGoIdent(contextPackage.Ident("Context"))
201201
p2 := "req *" + g.QualifiedGoIdent(method.Input.GoIdent)
202202
freturn := "(*" + g.QualifiedGoIdent(method.Output.GoIdent) + ",error)"

grpc/protoc-gen-go-grpc/main.go cgrpc/protoc-gen-go-cgrpc/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141
}
4242
}
4343
//delete old file
44-
oldfile := f.GeneratedFilenamePrefix + "_grpc.pb.go"
44+
oldfile := f.GeneratedFilenamePrefix + "_cgrpc.pb.go"
4545
if e := os.RemoveAll(oldfile); e != nil {
4646
panic("remove old file " + oldfile + " error:" + e.Error())
4747
}

grpc/resolver.go cgrpc/resolver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package grpc
1+
package cgrpc
22

33
import (
44
"context"
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type resolverBuilder struct {
12-
c *GrpcClient
12+
c *CGrpcClient
1313
}
1414

1515
func (b *resolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {

0 commit comments

Comments
 (0)