Skip to content

Commit

Permalink
feat: be compatible with old triple-gen code (#2416)
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima authored Sep 13, 2023
1 parent 2c92580 commit c0beb47
Show file tree
Hide file tree
Showing 56 changed files with 1,575 additions and 412 deletions.
10 changes: 8 additions & 2 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package client

import (
"strconv"
)

import (
"github.com/creasty/defaults"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/proxy"
"github.com/creasty/defaults"
"strconv"
)

type ClientOptions struct {
Expand Down
3 changes: 3 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ const (
CallClientStream = "client-stream"
CallServerStream = "server-stream"
CallBidiStream = "bidi-stream"
CallHTTPTypeKey = "call-http-type"
CallHTTP = "http"
CallHTTP2 = "http2"
)

const (
Expand Down
5 changes: 4 additions & 1 deletion compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package dubbo

import (
"go.uber.org/atomic"
)

import (
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"go.uber.org/atomic"
)

func compatRootConfig(c *InstanceOptions) *config.RootConfig {
Expand Down
5 changes: 4 additions & 1 deletion dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package dubbo

import (
"github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/global"
"github.com/pkg/errors"
)

// Instance is the highest layer conception that user could touch. It is mapped from RootConfig.
Expand Down
4 changes: 3 additions & 1 deletion global/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package global

import "github.com/creasty/defaults"
import (
"github.com/creasty/defaults"
)

type LoggerConfig struct {
// logger driver default zap
Expand Down
4 changes: 3 additions & 1 deletion global/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package global

import "strconv"
import (
"strconv"
)

// ReferenceConfig is the configuration of service consumer
type ReferenceConfig struct {
Expand Down
1 change: 1 addition & 0 deletions global/shutdown_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package global

import (
"github.com/creasty/defaults"

"go.uber.org/atomic"
)

Expand Down
62 changes: 34 additions & 28 deletions protocol/triple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package triple
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"path"
url_package "net/url"
"strings"
)

Expand All @@ -49,7 +49,7 @@ const (

// clientManager wraps triple clients and is responsible for find concrete triple client to invoke
// callUnary, callClientStream, callServerStream, callBidiStream.
// An Interface has a clientManager.
// A Reference has a clientManager.
type clientManager struct {
// triple_protocol clients, key is method name
triClients map[string]*tri.Client
Expand All @@ -58,7 +58,7 @@ type clientManager struct {
func (cm *clientManager) getClient(method string) (*tri.Client, error) {
triClient, ok := cm.triClients[method]
if !ok {
return nil, errors.New("missing triple client")
return nil, fmt.Errorf("missing triple client for method: %s", method)
}
return triClient, nil
}
Expand All @@ -68,9 +68,8 @@ func (cm *clientManager) callUnary(ctx context.Context, method string, req, resp
if err != nil {
return err
}
// todo: compiler generate tri.Request and tri.Response
triReq := req.(*tri.Request)
triResp := resp.(*tri.Response)
triReq := tri.NewRequest(req)
triResp := tri.NewResponse(resp)
if err := triClient.CallUnary(ctx, triReq, triResp); err != nil {
return err
}
Expand All @@ -94,7 +93,7 @@ func (cm *clientManager) callServerStream(ctx context.Context, method string, re
if err != nil {
return nil, err
}
triReq := req.(*tri.Request)
triReq := tri.NewRequest(req)
stream, err := triClient.CallServerStream(ctx, triReq)
if err != nil {
return nil, err
Expand All @@ -114,8 +113,10 @@ func (cm *clientManager) callBidiStream(ctx context.Context, method string) (int
return stream, nil
}

func (cm *clientManager) close() {
// todo: close connection and release resources
func (cm *clientManager) close() error {
// There is no need to release resources right now.
// But we leave this function here for future use.
return nil
}

func newClientManager(url *common.URL) (*clientManager, error) {
Expand Down Expand Up @@ -173,23 +174,23 @@ func newClientManager(url *common.URL) (*clientManager, error) {
tlsFlag = true
}

// todo: using option to represent whether using http1 or http2, and whether to use grpc

key := url.GetParam(constant.InterfaceKey, "")
conRefs := config.GetConsumerConfig().References
ref, ok := conRefs[key]
if !ok {
panic("no reference")
}
// todo(DMwangnima): this code fragment would be used to be compatible with old triple client
//key := url.GetParam(constant.InterfaceKey, "")
//conRefs := config.GetConsumerConfig().References
//ref, ok := conRefs[key]
//if !ok {
// panic("no reference")
//}
// todo: set timeout
var transport http.RoundTripper
switch ref.Protocol {
case "http":
callType := url.GetParam(constant.CallHTTPTypeKey, constant.CallHTTP2)
switch callType {
case constant.CallHTTP:
transport = &http.Transport{
TLSClientConfig: cfg,
}
triClientOpts = append(triClientOpts, tri.WithTriple())
case TRIPLE:
case constant.CallHTTP2:
if tlsFlag {
transport = &http2.Transport{
TLSClientConfig: cfg,
Expand All @@ -203,23 +204,28 @@ func newClientManager(url *common.URL) (*clientManager, error) {
}
}
triClientOpts = append(triClientOpts)
default:
panic(fmt.Sprintf("Unsupported type: %s", callType))
}
httpClient := &http.Client{
Transport: transport,
}

var baseTriUrl string
baseTriUrl = strings.TrimPrefix(url.Location, httpPrefix)
baseTriUrl = strings.TrimPrefix(url.Location, httpsPrefix)
var baseTriURL string
baseTriURL = strings.TrimPrefix(url.Location, httpPrefix)
baseTriURL = strings.TrimPrefix(url.Location, httpsPrefix)
if tlsFlag {
baseTriUrl = httpsPrefix + baseTriUrl
baseTriURL = httpsPrefix + baseTriURL
} else {
baseTriUrl = httpPrefix + baseTriUrl
baseTriURL = httpPrefix + baseTriURL
}
triClients := make(map[string]*tri.Client)
for _, method := range url.Methods {
triUrl := path.Join(baseTriUrl, url.Interface(), method)
triClient := tri.NewClient(httpClient, triUrl, triClientOpts...)
triURL, err := url_package.JoinPath(baseTriURL, url.Interface(), method)
if err != nil {
return nil, fmt.Errorf("JoinPath failed for base %s, interface %s, method %s", baseTriURL, url.Interface(), method)
}
triClient := tri.NewClient(httpClient, triURL, triClientOpts...)
triClients[method] = triClient
}

Expand Down
108 changes: 4 additions & 104 deletions protocol/triple/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,110 +18,10 @@
package triple

import (
"reflect"
"net/url"
)

import (
hessian "github.com/apache/dubbo-go-hessian2"

perrors "github.com/pkg/errors"
)

// ReflectResponse reflect return value
func ReflectResponse(in interface{}, out interface{}) error {
if in == nil {
return perrors.Errorf("@in is nil")
}

if out == nil {
return perrors.Errorf("@out is nil")
}
if reflect.TypeOf(out).Kind() != reflect.Ptr {
return perrors.Errorf("@out should be a pointer")
}

inValue := hessian.EnsurePackValue(in)
outValue := hessian.EnsurePackValue(out)

outType := outValue.Type().String()
if outType == "interface {}" || outType == "*interface {}" {
hessian.SetValue(outValue, inValue)
return nil
}

switch inValue.Type().Kind() {
case reflect.Slice, reflect.Array:
return CopySlice(inValue, outValue)
case reflect.Map:
return CopyMap(inValue, outValue)
default:
hessian.SetValue(outValue, inValue)
}

return nil
}

// CopySlice copy from inSlice to outSlice
func CopySlice(inSlice, outSlice reflect.Value) error {
if inSlice.IsNil() {
return perrors.New("@in is nil")
}
if inSlice.Kind() != reflect.Slice {
return perrors.Errorf("@in is not slice, but %v", inSlice.Kind())
}

for outSlice.Kind() == reflect.Ptr {
outSlice = outSlice.Elem()
}

size := inSlice.Len()
outSlice.Set(reflect.MakeSlice(outSlice.Type(), size, size))

for i := 0; i < size; i++ {
inSliceValue := inSlice.Index(i)
if !inSliceValue.Type().AssignableTo(outSlice.Index(i).Type()) {
return perrors.Errorf("in element type [%s] can not assign to out element type [%s]",
inSliceValue.Type().String(), outSlice.Type().String())
}
outSlice.Index(i).Set(inSliceValue)
}

return nil
}

// CopyMap copy from in map to out map
func CopyMap(inMapValue, outMapValue reflect.Value) error {
if inMapValue.IsNil() {
return perrors.New("@in is nil")
}
if !inMapValue.CanInterface() {
return perrors.New("@in's Interface can not be used.")
}
if inMapValue.Kind() != reflect.Map {
return perrors.Errorf("@in is not map, but %v", inMapValue.Kind())
}

outMapType := hessian.UnpackPtrType(outMapValue.Type())
hessian.SetValue(outMapValue, reflect.MakeMap(outMapType))

outKeyType := outMapType.Key()

outMapValue = hessian.UnpackPtrValue(outMapValue)
outValueType := outMapValue.Type().Elem()

for _, inKey := range inMapValue.MapKeys() {
inValue := inMapValue.MapIndex(inKey)

if !inKey.Type().AssignableTo(outKeyType) {
return perrors.Errorf("in Key:{type:%s, value:%#v} can not assign to out Key:{type:%s} ",
inKey.Type().String(), inKey, outKeyType.String())
}
if !inValue.Type().AssignableTo(outValueType) {
return perrors.Errorf("in Value:{type:%s, value:%#v} can not assign to out value:{type:%s}",
inValue.Type().String(), inValue, outValueType.String())
}
outMapValue.SetMapIndex(inKey, inValue)
}

return nil
func joinProcedure(interfaceName, methodName string) string {
procedure, _ := url.JoinPath("", interfaceName, methodName)
return "/" + procedure
}
4 changes: 2 additions & 2 deletions protocol/triple/internal/client/cmd_classic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"context"
"dubbo.apache.org/dubbo-go/v3/config"
"fmt"
)

import (
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
greet "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/greettriple"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple"
)

func main() {
Expand Down
7 changes: 5 additions & 2 deletions protocol/triple/internal/client/cmd_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package main

import (
"context"
"fmt"
)

import (
"dubbo.apache.org/dubbo-go/v3/client"
greet "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/greettriple"
"fmt"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple"
)

func main() {
Expand Down
7 changes: 5 additions & 2 deletions protocol/triple/internal/client/cmd_instance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package main

import (
"context"
"fmt"
)

import (
dubbo "dubbo.apache.org/dubbo-go/v3"
"dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/global"
greet "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/greettriple"
"fmt"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple"
)

func main() {
Expand Down
Loading

0 comments on commit c0beb47

Please sign in to comment.