Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support discover grpc pluggable component #991

Merged
merged 19 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/cryption/aliyun/kms.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion components/cryption/aws/kms.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions components/custom/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
)

type Registry interface {
Register(kind string, factorys ...*ComponentFactory)
Register(kind string, factories ...*Factory)
Create(kind, compType string) (Component, error)
}

type ComponentFactory struct {
type Factory struct {
Type string
FactoryMethod func() Component
}

func NewComponentFactory(compType string, f func() Component) *ComponentFactory {
return &ComponentFactory{
func NewComponentFactory(compType string, f func() Component) *Factory {
return &Factory{
Type: compType,
FactoryMethod: f,
}
Expand All @@ -47,7 +47,7 @@ func NewRegistry(info *info.RuntimeInfo) Registry {
}
}

func (r *componentRegistry) Register(kind string, fs ...*ComponentFactory) {
func (r *componentRegistry) Register(kind string, fs ...*Factory) {
if len(fs) == 0 {
return
}
Expand Down
10 changes: 5 additions & 5 deletions components/file/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ import (
)

type Registry interface {
Register(fs ...*FileFactory)
Register(fs ...*Factory)
Create(compType string) (File, error)
}

type FileFactory struct {
type Factory struct {
CompType string
FactoryMethod func() File
}

func NewFileFactory(CompType string, f func() File) *FileFactory {
return &FileFactory{
func NewFileFactory(CompType string, f func() File) *Factory {
return &Factory{
CompType: CompType,
FactoryMethod: f,
}
Expand All @@ -52,7 +52,7 @@ func NewRegistry(info *info.RuntimeInfo) Registry {
}
}

func (r *FileStoreRegistry) Register(fs ...*FileFactory) {
func (r *FileStoreRegistry) Register(fs ...*Factory) {
for _, f := range fs {
r.files[f.CompType] = f.FactoryMethod
r.info.RegisterComponent(ServiceName, f.CompType)
Expand Down
5 changes: 4 additions & 1 deletion components/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ require (
go.mongodb.org/mongo-driver v1.8.0
go.uber.org/atomic v1.8.0
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.27.1
mosn.io/api v1.3.0
mosn.io/layotto/spec v0.0.0-20231023045845-48ec2bc7eab8
mosn.io/mosn v1.3.0
mosn.io/pkg v1.3.0
)
Expand Down Expand Up @@ -208,7 +210,6 @@ require (
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand All @@ -219,3 +220,5 @@ require (
)

replace github.com/klauspost/compress => github.com/klauspost/compress v1.13.1

replace mosn.io/layotto/spec => ../spec
9 changes: 5 additions & 4 deletions components/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ const ServiceName = "hello"

type HelloService interface {
Init(*HelloConfig) error
Hello(context.Context, *HelloRequest) (*HelloReponse, error)
Hello(context.Context, *HelloRequest) (*HelloResponse, error)
}

type HelloConfig struct {
ref.Config
Type string `json:"type"`
HelloString string `json:"hello"`
Type string `json:"type"`
HelloString string `json:"hello"`
Metadata map[string]string `json:"metadata"`
}

type HelloRequest struct {
Name string `json:"name"`
}

type HelloReponse struct {
type HelloResponse struct {
HelloString string `json:"hello"`
}
4 changes: 2 additions & 2 deletions components/hello/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (hw *HelloWorld) Init(config *hello.HelloConfig) error {
return nil
}

func (hw *HelloWorld) Hello(ctx context.Context, req *hello.HelloRequest) (*hello.HelloReponse, error) {
func (hw *HelloWorld) Hello(ctx context.Context, req *hello.HelloRequest) (*hello.HelloResponse, error) {
greetings, _ := hw.Say.Load().(string)
if req.Name != "" {
greetings = greetings + ", " + req.Name
}
return &hello.HelloReponse{
return &hello.HelloResponse{
HelloString: greetings,
}, nil
}
78 changes: 78 additions & 0 deletions components/hello/pluggable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 Layotto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hello

import (
"context"
"fmt"

"mosn.io/layotto/components/pluggable"
helloproto "mosn.io/layotto/spec/proto/pluggable/v1/hello"
)

func init() {
// spec.proto.pluggable.v1.Hello
pluggable.AddServiceDiscoveryCallback(helloproto.Hello_ServiceDesc.ServiceName, func(compType string, dialer pluggable.GRPCConnectionDialer) interface{} {
return NewHelloFactory(compType, func() HelloService {
return NewGRPCHello(dialer)
})
})
}

type grpcHello struct {
dialer pluggable.GRPCConnectionDialer
client helloproto.HelloClient
}

func NewGRPCHello(dialer pluggable.GRPCConnectionDialer) HelloService {
return &grpcHello{dialer: dialer}
}

// todo 优雅关闭时关闭 conn

func (g *grpcHello) Init(config *HelloConfig) error {
// 1.dial grpc server
ctx := context.TODO()
conn, err := g.dialer(ctx)
if err != nil {
return fmt.Errorf("dial hello pluggable component: %w", err)
}

// 2.init pluggable component
g.client = helloproto.NewHelloClient(conn)
if _, err := g.client.Init(ctx, &helloproto.HelloConfig{
Config: pluggable.ToProtoConfig(config.Config),
Type: config.Type,
HelloString: config.HelloString,
Metadata: config.Metadata,
}); err != nil {
return fmt.Errorf("init hello pluggable component: %w", err)
}

return nil
}

func (g *grpcHello) Hello(ctx context.Context, request *HelloRequest) (*HelloResponse, error) {
resp, err := g.client.SayHello(ctx, &helloproto.HelloRequest{
Name: request.Name,
})
if err != nil {
return nil, err
}

res := &HelloResponse{
HelloString: resp.GetHelloString(),
}
return res, nil
}
Loading
Loading