-
Notifications
You must be signed in to change notification settings - Fork 930
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
feature-graceful-shutdwon #1675
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
41d1203
优雅下线
XiaoWeiKIN 5759c77
优雅下线
XiaoWeiKIN 9b7b897
unit test add
XiaoWeiKIN 941351d
add test
XiaoWeiKIN da25c24
import format
XiaoWeiKIN e879126
Fix: import formatter
LaurenceLiZhixin 272d18a
Merge remote-tracking branch 'upstream/3.0' into graceful-shutdown
LaurenceLiZhixin e2ff2ec
Fix: license
LaurenceLiZhixin 3dd5c1f
invoker
5b8939e
invoker
6d6a61a
fix: ut
LaurenceLiZhixin 97fb3d5
ftr: finish graceful shugdown
LaurenceLiZhixin 0a137fd
fix: update triple
LaurenceLiZhixin 6d34c66
fix: add nolint
LaurenceLiZhixin c95f80d
Merge remote-tracking branch 'upstream/3.0' into graceful-shutdown
LaurenceLiZhixin e8677d1
fix: lint
LaurenceLiZhixin 39916bb
fix: ut
LaurenceLiZhixin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -124,6 +124,16 @@ func GetApplicationConfig() *ApplicationConfig { | |
return rootConfig.Application | ||
} | ||
|
||
func GetShutDown() *ShutdownConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这样写也许更整洁点 |
||
if err := check(); err != nil { | ||
return NewShutDownConfigBuilder().Build() | ||
} | ||
if rootConfig.Shutdown != nil { | ||
return rootConfig.Shutdown | ||
} | ||
return NewShutDownConfigBuilder().Build() | ||
} | ||
|
||
// getRegistryIds get registry ids | ||
func (rc *RootConfig) getRegistryIds() []string { | ||
ids := make([]string, 0) | ||
|
@@ -220,6 +230,7 @@ func (rc *RootConfig) Start() { | |
// todo if register consumer instance or has exported services | ||
exportMetadataService() | ||
registerServiceInstance() | ||
gracefulShutdownInit() | ||
}) | ||
} | ||
|
||
|
@@ -237,6 +248,7 @@ func newEmptyRootConfig() *RootConfig { | |
Metric: NewMetricConfigBuilder().Build(), | ||
Logger: NewLoggerConfigBuilder().Build(), | ||
Custom: NewCustomConfigBuilder().Build(), | ||
Shutdown: NewShutDownConfigBuilder().Build(), | ||
} | ||
return newRootConfig | ||
} | ||
|
@@ -329,6 +341,11 @@ func (rb *RootConfigBuilder) SetCustom(customConfig *CustomConfig) *RootConfigBu | |
return rb | ||
} | ||
|
||
func (rb *RootConfigBuilder) SetShutDown(shutDownConfig *ShutdownConfig) *RootConfigBuilder { | ||
rb.rootConfig.Shutdown = shutDownConfig | ||
return rb | ||
} | ||
|
||
func (rb *RootConfigBuilder) Build() *RootConfig { | ||
return rb.rootConfig | ||
} | ||
|
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,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 graceful_shutdown | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
import ( | ||
"dubbo.apache.org/dubbo-go/v3/common/constant" | ||
"dubbo.apache.org/dubbo-go/v3/common/extension" | ||
"dubbo.apache.org/dubbo-go/v3/common/logger" | ||
"dubbo.apache.org/dubbo-go/v3/config" | ||
"dubbo.apache.org/dubbo-go/v3/filter" | ||
"dubbo.apache.org/dubbo-go/v3/protocol" | ||
) | ||
|
||
var ( | ||
csfOnce sync.Once | ||
csf *consumerGracefulShutdownFilter | ||
) | ||
|
||
func init() { | ||
// `init()` is performed before config.Load(), so shutdownConfig will be retrieved after config was loaded. | ||
extension.SetFilter(constant.GracefulShutdownConsumerFilterKey, func() filter.Filter { | ||
return newConsumerGracefulShutdownFilter() | ||
}) | ||
|
||
} | ||
|
||
type consumerGracefulShutdownFilter struct { | ||
activeCount int32 | ||
shutdownConfig *config.ShutdownConfig | ||
} | ||
|
||
func newConsumerGracefulShutdownFilter() filter.Filter { | ||
if csf == nil { | ||
csfOnce.Do(func() { | ||
csf = &consumerGracefulShutdownFilter{} | ||
|
||
}) | ||
} | ||
return csf | ||
} | ||
|
||
// Invoke adds the requests count and block the new requests if application is closing | ||
func (f *consumerGracefulShutdownFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
atomic.AddInt32(&f.activeCount, 1) | ||
return invoker.Invoke(ctx, invocation) | ||
} | ||
|
||
// OnResponse reduces the number of active processes then return the process result | ||
func (f *consumerGracefulShutdownFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
atomic.AddInt32(&f.activeCount, -1) | ||
// although this isn't thread safe, it won't be a problem if the f.rejectNewRequest() is true. | ||
if f.shutdownConfig != nil && f.shutdownConfig.RejectRequest && f.activeCount <= 0 { | ||
f.shutdownConfig.RequestsFinished = true | ||
} | ||
return result | ||
} | ||
|
||
func (f *consumerGracefulShutdownFilter) Set(name string, conf interface{}) { | ||
switch name { | ||
case constant.GracefulShutdownFilterShutdownConfig: | ||
if shutdownConfig, ok := conf.(*config.ShutdownConfig); ok { | ||
f.shutdownConfig = shutdownConfig | ||
return | ||
} | ||
logger.Warnf("the type of config for {%s} should be *config.ShutdownConfig", constant.GracefulShutdownFilterShutdownConfig) | ||
default: | ||
// do nothing | ||
} | ||
} |
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,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 graceful_shutdown | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
"dubbo.apache.org/dubbo-go/v3/common" | ||
"dubbo.apache.org/dubbo-go/v3/common/constant" | ||
"dubbo.apache.org/dubbo-go/v3/common/extension" | ||
"dubbo.apache.org/dubbo-go/v3/config" | ||
"dubbo.apache.org/dubbo-go/v3/protocol" | ||
"dubbo.apache.org/dubbo-go/v3/protocol/invocation" | ||
) | ||
|
||
func TestConusmerFilterInvoke(t *testing.T) { | ||
url := common.NewURLWithOptions(common.WithParams(url.Values{})) | ||
invocation := invocation.NewRPCInvocation("GetUser", []interface{}{"OK"}, make(map[string]interface{})) | ||
|
||
rootConfig := config.NewRootConfigBuilder(). | ||
SetShutDown(config.NewShutDownConfigBuilder(). | ||
SetTimeout("60s"). | ||
SetStepTimeout("3s"). | ||
Build()).Build() | ||
|
||
config.SetRootConfig(*rootConfig) | ||
|
||
filterValue, _ := extension.GetFilter(constant.GracefulShutdownConsumerFilterKey) | ||
filter := filterValue.(*consumerGracefulShutdownFilter) | ||
filter.Set(constant.GracefulShutdownFilterShutdownConfig, config.GetShutDown()) | ||
assert.Equal(t, filter.shutdownConfig, config.GetShutDown()) | ||
|
||
result := filter.Invoke(context.Background(), protocol.NewBaseInvoker(url), invocation) | ||
assert.NotNil(t, result) | ||
assert.Nil(t, result.Error()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来返回一个参数感觉更舒服,panic去掉就可以了