-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support discover grpc pluggable component (#991)
* 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 <[email protected]>
- Loading branch information
Showing
46 changed files
with
2,636 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.