Skip to content

Commit

Permalink
Merge pull request #1448 from cloudwego/release-v0.10.2
Browse files Browse the repository at this point in the history
chore: release v0.10.2
  • Loading branch information
jayantxie authored Jul 25, 2024
2 parents 906aeee + 1404b9e commit eb99c3f
Show file tree
Hide file tree
Showing 64 changed files with 832 additions and 816 deletions.
7 changes: 4 additions & 3 deletions client/rpctimeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,20 @@ func makeTimeoutErr(ctx context.Context, start time.Time, timeout time.Duration)
return kerrors.ErrRPCTimeout.WithCause(fmt.Errorf("%s: %w by business", errMsg, ctx.Err()))
}
}

if ddl, ok := ctx.Deadline(); !ok {
errMsg = fmt.Sprintf("%s, %s", errMsg, "unknown error: context deadline not set?")
} else {
// Go's timer implementation is not so accurate,
// so if we need to check ctx deadline earlier than our timeout, we should consider the accuracy
if timeout <= 0 {
isBizTimeout := isBusinessTimeout(start, timeout, ddl, rpctimeout.LoadBusinessTimeoutThreshold())
if isBizTimeout {
// if timeout set in context is shorter than RPCTimeout in rpcinfo, it will trigger earlier.
errMsg = fmt.Sprintf("%s, timeout by business, actual=%s", errMsg, ddl.Sub(start))
} else if roundTimeout := timeout - time.Millisecond; roundTimeout >= 0 && ddl.Before(start.Add(roundTimeout)) {
errMsg = fmt.Sprintf("%s, context deadline earlier than timeout, actual=%v", errMsg, ddl.Sub(start))
}

if needFineGrainedErrCode && isBusinessTimeout(start, timeout, ddl, rpctimeout.LoadBusinessTimeoutThreshold()) {
if needFineGrainedErrCode && isBizTimeout {
return kerrors.ErrTimeoutByBusiness.WithCause(errors.New(errMsg))
}
}
Expand Down
11 changes: 11 additions & 0 deletions client/rpctimeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ func TestRpcTimeoutMWTimeoutByBusiness(t *testing.T) {
}
rpctimeout.DisableGlobalNeedFineGrainedErrCode()
})

t.Run("Timeout by business with shorter timeout than RPCTimeout in rpcinfo", func(t *testing.T) {
rpctimeout.DisableGlobalNeedFineGrainedErrCode()
err := runTimeoutMW(time.Second)
if !kerrors.IsTimeoutError(err) {
t.Errorf("rpcTimeoutMW(1s) = %v, want %v", err, kerrors.ErrRPCTimeout)
}
if errMsg := err.Error(); !strings.Contains(errMsg, "timeout by business, actual") {
t.Errorf("rpcTimeoutMW(1s) = %v, want error msg with %v", errMsg, "timeout by business, actual")
}
})
}

func TestRpcTimeoutMWCancelByBusiness(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ go 1.13
require (
github.com/apache/thrift v0.13.0
github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1
github.com/bytedance/mockey v1.2.7
github.com/bytedance/sonic v1.11.8
github.com/cloudwego/configmanager v0.2.2
github.com/cloudwego/dynamicgo v0.2.8
github.com/cloudwego/dynamicgo v0.2.9
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/frugal v0.1.15
github.com/cloudwego/localsession v0.0.2
github.com/cloudwego/netpoll v0.6.2
github.com/cloudwego/netpoll v0.6.3
github.com/cloudwego/runtimex v0.1.0
github.com/cloudwego/thriftgo v0.3.6
github.com/cloudwego/thriftgo v0.3.15
github.com/golang/mock v1.6.0
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3
github.com/jhump/protoreflect v1.8.2
Expand Down
30 changes: 8 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQ
github.com/bytedance/gopkg v0.0.0-20240507064146-197ded923ae3/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1 h1:rT7Mm6uUpHeZQzfs2v0Mlj0SL02CzyVi+EB7VYPM/z4=
github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
github.com/bytedance/mockey v1.2.7 h1:8j4yCqS5OmMe2dQCxPit4FVkwTK9nrykIgbOZN3s28o=
github.com/bytedance/mockey v1.2.7/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA=
github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
Expand All @@ -32,10 +30,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/configmanager v0.2.2 h1:sVrJB8gWYTlPV2OS3wcgJSO9F2/9Zbkmcm1Z7jempOU=
github.com/cloudwego/configmanager v0.2.2/go.mod h1:ppiyU+5TPLonE8qMVi/pFQk2eL3Q4P7d4hbiNJn6jwI=
github.com/cloudwego/dynamicgo v0.2.7 h1:D6vAUqGC00kj5fOme/ipftyH38/Af+8kmczLFs9z2SM=
github.com/cloudwego/dynamicgo v0.2.7/go.mod h1:F3jlbPmlNzhcuDMXwZoBJ7rJKpg2iE+TnIy9pSJiGzs=
github.com/cloudwego/dynamicgo v0.2.8 h1:rCkVEZzXpdnM1dlunu6mMz5pDtZfBQFv9/FAk1hOoOE=
github.com/cloudwego/dynamicgo v0.2.8/go.mod h1:F3jlbPmlNzhcuDMXwZoBJ7rJKpg2iE+TnIy9pSJiGzs=
github.com/cloudwego/dynamicgo v0.2.9 h1:MHGyGmdFT8iMOsM5S9iutjZB0csu2LupsTTHyi6a8pY=
github.com/cloudwego/dynamicgo v0.2.9/go.mod h1:F3jlbPmlNzhcuDMXwZoBJ7rJKpg2iE+TnIy9pSJiGzs=
github.com/cloudwego/fastpb v0.0.4 h1:/ROVVfoFtpfc+1pkQLzGs+azjxUbSOsAqSY4tAAx4mg=
github.com/cloudwego/fastpb v0.0.4/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/frugal v0.1.15 h1:LC55UJKhQPMFVjDPbE+LJcF7etZjSx6uokG1tk0wPK0=
Expand All @@ -45,21 +41,21 @@ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cloudwego/localsession v0.0.2 h1:N9/IDtCPj1fCL9bCTP+DbXx3f40YjVYWcwkJG0YhQkY=
github.com/cloudwego/localsession v0.0.2/go.mod h1:kiJxmvAcy4PLgKtEnPS5AXed3xCiXcs7Z+KBHP72Wv8=
github.com/cloudwego/netpoll v0.6.1 h1:Cjftvi6bmumsOijmuUFy6HqAUXMxAT3fKK96wsrm3XA=
github.com/cloudwego/netpoll v0.6.1/go.mod h1:kaqvfZ70qd4T2WtIIpCOi5Cxyob8viEpzLhCrTrz3HM=
github.com/cloudwego/netpoll v0.6.2 h1:+KdILv5ATJU+222wNNXpHapYaBeRvvL8qhJyhcxRxrQ=
github.com/cloudwego/netpoll v0.6.2/go.mod h1:kaqvfZ70qd4T2WtIIpCOi5Cxyob8viEpzLhCrTrz3HM=
github.com/cloudwego/netpoll v0.6.3 h1:t+ndlwBFjQZimUj3ul31DwI45t18eOr2pcK3juZZm+E=
github.com/cloudwego/netpoll v0.6.3/go.mod h1:kaqvfZ70qd4T2WtIIpCOi5Cxyob8viEpzLhCrTrz3HM=
github.com/cloudwego/runtimex v0.1.0 h1:HG+WxWoj5/CDChDZ7D99ROwvSMkuNXAqt6hnhTTZDiI=
github.com/cloudwego/runtimex v0.1.0/go.mod h1:23vL/HGV0W8nSCHbe084AgEBdDV4rvXenEUMnUNvUd8=
github.com/cloudwego/thriftgo v0.3.6 h1:gHHW8Ag3cAEQ/awP4emTJiRPr5yQjbANhcsmV8/Epbw=
github.com/cloudwego/thriftgo v0.3.6/go.mod h1:29ukiySoAMd0vXMYIduAY9dph/7dmChvOS11YLotFb8=
github.com/cloudwego/thriftgo v0.3.15 h1:yB/DDGjeSjliyidMVBjKhGl9RgE4M8iVIz5dKpAIyUs=
github.com/cloudwego/thriftgo v0.3.15/go.mod h1:R4a+4aVDI0V9YCTfpNgmvbkq/9ThKgF7Om8Z0I36698=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
Expand Down Expand Up @@ -109,8 +105,6 @@ github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 h1:mpL/HvfIgIejhVwAfx
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3/go.mod h1:gSuNB+gJaOiQKLEZ+q+PK9Mq3SOzhRcw2GsGS/FhYDk=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gordonklaus/ineffassign v0.0.0-20200309095847-7953dde2c7bf/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
Expand All @@ -119,8 +113,6 @@ github.com/jhump/protoreflect v1.8.2 h1:k2xE7wcUomeqwY0LDCYA16y4WWfyTcMx5mKhk0d4
github.com/jhump/protoreflect v1.8.2/go.mod h1:7GcYQDdMU/O/BBrl/cX6PNHpXh6cenjd8pneu5yW7Tg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down Expand Up @@ -154,10 +146,6 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -184,7 +172,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY=
golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down Expand Up @@ -304,7 +291,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
64 changes: 64 additions & 0 deletions internal/mocks/remote/servicesearcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 CloudWeGo 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 remote

import (
"github.com/cloudwego/kitex/internal/mocks"
"github.com/cloudwego/kitex/pkg/serviceinfo"
)

type MockSvcSearcher struct {
svcMap map[string]*serviceinfo.ServiceInfo
methodSvcMap map[string]*serviceinfo.ServiceInfo
}

func NewMockSvcSearcher(svcMap, methodSvcMap map[string]*serviceinfo.ServiceInfo) *MockSvcSearcher {
return &MockSvcSearcher{svcMap: svcMap, methodSvcMap: methodSvcMap}
}

func NewDefaultSvcSearcher() *MockSvcSearcher {
svcInfo := mocks.ServiceInfo()
s := map[string]*serviceinfo.ServiceInfo{
mocks.MockServiceName: svcInfo,
}
m := map[string]*serviceinfo.ServiceInfo{
mocks.MockMethod: svcInfo,
mocks.MockExceptionMethod: svcInfo,
mocks.MockErrorMethod: svcInfo,
mocks.MockOnewayMethod: svcInfo,
}
return &MockSvcSearcher{svcMap: s, methodSvcMap: m}
}

func (s *MockSvcSearcher) SearchService(svcName, methodName string, strict bool) *serviceinfo.ServiceInfo {
if strict {
if svc := s.svcMap[svcName]; svc != nil {
return svc
}
return nil
}
var svc *serviceinfo.ServiceInfo
if svcName == "" {
svc = s.methodSvcMap[methodName]
} else {
svc = s.svcMap[svcName]
}
if svc != nil {
return svc
}
return nil
}
11 changes: 4 additions & 7 deletions internal/test/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ func GetLocalAddress() string {

// tells if a net address is already in use.
func IsAddressInUse(address string) bool {
// Attempt to establish a TCP connection to the address
conn, err := net.DialTimeout("tcp", address, time.Duration(1+rand.Intn(10))*100*time.Millisecond)
ln, err := net.Listen("tcp", address)
if err != nil {
// If there's an error, the address is likely not in use or not reachable
return false
return true
}
_ = conn.Close()
// If the connection is successful, the address is in use
return true
ln.Close()
return false
}

// WaitServerStart waits for server to start for at most 1 second
Expand Down
19 changes: 13 additions & 6 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,20 @@ func DefaultDiff(cacheKey string, prev, next Result) (Change, bool) {
},
}

prevMap := make(map[string]struct{}, len(prev.Instances))
prevMap := make(map[string]Instance, len(prev.Instances))
for _, ins := range prev.Instances {
prevMap[ins.Address().String()] = struct{}{}
prevMap[ins.Address().String()] = ins
}

nextMap := make(map[string]struct{}, len(next.Instances))
nextMap := make(map[string]Instance, len(next.Instances))
for _, ins := range next.Instances {
addr := ins.Address().String()
nextMap[addr] = struct{}{}
if _, found := prevMap[addr]; !found {
nextMap[addr] = ins
// FIXME(jizhuozhi): tags should also be used to determine whether the instance has updated
if prevIns, found := prevMap[addr]; !found {
ch.Added = append(ch.Added, ins)
} else if prevIns.Weight() != ins.Weight() {
ch.Updated = append(ch.Updated, ins)
}
}

Expand All @@ -98,7 +101,7 @@ func DefaultDiff(cacheKey string, prev, next Result) (Change, bool) {
ch.Removed = append(ch.Removed, ins)
}
}
return ch, len(ch.Added)+len(ch.Removed) != 0
return ch, len(ch.Added)+len(ch.Updated)+len(ch.Removed) != 0
}

type instance struct {
Expand All @@ -120,6 +123,10 @@ func (i *instance) Tag(key string) (value string, exist bool) {
return
}

func (i *instance) Tags() map[string]string {
return i.tags
}

// NewInstance creates a Instance using the given network, address and tags
func NewInstance(network, address string, weight int, tags map[string]string) Instance {
return &instance{
Expand Down
5 changes: 3 additions & 2 deletions pkg/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ func TestDefaultDiff(t *testing.T) {
Instances: []Instance{
NewInstance("tcp", "1", 10, nil),
NewInstance("tcp", "2", 10, nil),
NewInstance("tcp", "3", 10, nil),
NewInstance("tcp", "3", 20, nil),
NewInstance("tcp", "5", 10, nil),
},
}}, Change{
Result: Result{Instances: []Instance{
NewInstance("tcp", "1", 10, nil),
NewInstance("tcp", "2", 10, nil),
NewInstance("tcp", "3", 10, nil),
NewInstance("tcp", "3", 20, nil),
NewInstance("tcp", "5", 10, nil),
}, CacheKey: "1", Cacheable: true},
Added: []Instance{NewInstance("tcp", "5", 10, nil)},
Updated: []Instance{NewInstance("tcp", "3", 20, nil)},
Removed: []Instance{NewInstance("tcp", "4", 10, nil)},
}, true},
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/generic/binary_test/generic_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import (
kt "github.com/cloudwego/kitex/internal/mocks/thrift"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/serviceinfo"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/pkg/utils"
"github.com/cloudwego/kitex/server"
"github.com/cloudwego/kitex/server/genericserver"
"github.com/cloudwego/kitex/transport"
)

var (
Expand All @@ -45,14 +48,14 @@ var (
)

func newGenericClient(destService string, g generic.Generic, targetIPPort string, opts ...client.Option) genericclient.Client {
opts = append(opts, client.WithHostPorts(targetIPPort))
opts = append(opts, client.WithHostPorts(targetIPPort), client.WithMetaHandler(transmeta.ClientTTHeaderHandler), client.WithTransportProtocol(transport.TTHeaderFramed))
genericCli, _ := genericclient.NewClient(destService, g, opts...)
return genericCli
}

func newGenericServer(g generic.Generic, addr net.Addr, handler generic.Service) server.Server {
var opts []server.Option
opts = append(opts, server.WithServiceAddr(addr), server.WithExitWaitTime(time.Microsecond*10))
opts = append(opts, server.WithServiceAddr(addr), server.WithExitWaitTime(time.Microsecond*10), server.WithMetaHandler(transmeta.ServerTTHeaderHandler))
svr := genericserver.NewServer(handler, g, opts...)
go func() {
err := svr.Run()
Expand Down Expand Up @@ -84,6 +87,14 @@ func (g *GenericServiceErrorImpl) GenericCall(ctx context.Context, method string
return response, errors.New(errResp)
}

// GenericServiceBizErrorImpl ...
type GenericServiceBizErrorImpl struct{}

// GenericCall ...
func (g *GenericServiceBizErrorImpl) GenericCall(ctx context.Context, method string, request interface{}) (response interface{}, err error) {
return response, kerrors.NewBizStatusError(404, "not found")
}

// GenericServiceMockImpl ...
type GenericServiceMockImpl struct{}

Expand Down
19 changes: 19 additions & 0 deletions pkg/generic/binary_test/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
kt "github.com/cloudwego/kitex/internal/mocks/thrift"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/utils"
"github.com/cloudwego/kitex/server"
)
Expand All @@ -43,6 +44,7 @@ var addr = test.GetLocalAddress()
func TestRun(t *testing.T) {
t.Run("RawThriftBinary", rawThriftBinary)
t.Run("RawThriftBinaryError", rawThriftBinaryError)
t.Run("RawThriftBinaryBizError", rawThriftBinaryBizError)
t.Run("RawThriftBinaryMockReq", rawThriftBinaryMockReq)
t.Run("RawThriftBinary2NormalServer", rawThriftBinary2NormalServer)
}
Expand Down Expand Up @@ -77,6 +79,23 @@ func rawThriftBinaryError(t *testing.T) {
test.Assert(t, strings.Contains(err.Error(), errResp), err.Error())
}

func rawThriftBinaryBizError(t *testing.T) {
svr := initRawThriftBinaryServer(new(GenericServiceBizErrorImpl))
defer svr.Stop()

cli := initRawThriftBinaryClient()

method := "myMethod"
buf := genBinaryReqBuf(method)

_, err := cli.GenericCall(context.Background(), method, buf)
test.Assert(t, err != nil)
bizStatusErr, ok := kerrors.FromBizStatusError(err)
test.Assert(t, ok)
test.Assert(t, bizStatusErr.BizStatusCode() == 404)
test.Assert(t, bizStatusErr.BizMessage() == "not found")
}

func rawThriftBinaryMockReq(t *testing.T) {
svr := initRawThriftBinaryServer(new(GenericServiceMockImpl))
defer svr.Stop()
Expand Down
Loading

0 comments on commit eb99c3f

Please sign in to comment.