Skip to content

Commit

Permalink
feat: support discover grpc pluggable component (#991)
Browse files Browse the repository at this point in the history
* feat: support discover grpc pluggable component

* chore: add file license and fix test case

* style: fix code lint

* refactor: discover pluggable component then register it to mosn runtime

* refactor: ignore discover error

* feat: support register hello pluggable component

* feat: delete unused pluggable grpc metadata instance key

* chore: add proto comment

* docs: add pluggable component usage document

* docs: delete unused document content

* refactor: move pluggable component implement from layotto to components

* refactor: format code

* docs: add pluggable component design chinese doc

* docs: fix markdown lint

* refactor: improve code readability and add English documents

* fix: set pluggable component grpc dial timeout

---------

Co-authored-by: Marco <lingfenglangshao@gmail.com>
  • Loading branch information
cyb0225 and zhenjunMa authored Oct 27, 2023
1 parent 48ec2bc commit 8f73577
Show file tree
Hide file tree
Showing 46 changed files with 2,636 additions and 46 deletions.
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
}
80 changes: 80 additions & 0 deletions components/hello/pluggable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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"
"time"

"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) pluggable.Component {
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, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
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

0 comments on commit 8f73577

Please sign in to comment.