Skip to content
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

新增对ServiceConfig配置的支持 #574

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -48,6 +49,7 @@
import net.devh.boot.grpc.client.config.GrpcChannelProperties;
import net.devh.boot.grpc.client.config.GrpcChannelProperties.Security;
import net.devh.boot.grpc.client.config.GrpcChannelsProperties;
import net.devh.boot.grpc.client.config.MethodConfig;
import net.devh.boot.grpc.client.config.NegotiationType;
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry;

Expand All @@ -56,7 +58,6 @@
* connection pooling and thus needs to be {@link #close() closed} after usage.
*
* @param <T> The type of builder used by this channel factory.
*
* @author Michael ([email protected])
* @author Daniel Theuke ([email protected])
* @since 5/17/16
Expand Down Expand Up @@ -168,11 +169,43 @@ protected void configure(final T builder, final String name) {
configureSecurity(builder, name);
configureLimits(builder, name);
configureCompression(builder, name);
configureRetryEnabled(builder, name);
for (final GrpcChannelConfigurer channelConfigurer : this.channelConfigurers) {
channelConfigurer.accept(builder, name);
}
}

/**
* Configures the retry options that should be used by the channel.
*
* @param builder The channel builder to configure.
* @param name The name of the client to configure.
*/
protected void configureRetryEnabled(final T builder, final String name) {
final GrpcChannelProperties properties = getPropertiesFor(name);
if (properties.isRetryEnabled()) {
builder.enableRetry();
// build retry policy by default service config
// TODO: Wrap field in defaultServiceConfig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid conflicts with other values in the serviceConfig it might be neccessary to wrap the methodConfig property in a defaultServiceConfig property for clarity.

builder.defaultServiceConfig(buildDefaultServiceConfig(properties));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default service config might be used outside of the retry logic.
So it might be neccessary to move it "elsewhere".

}
}

/**
* Builds the service config object.
*
* @param properties The properties of
* @return The json alike service config.
*/
protected Map<String, Object> buildDefaultServiceConfig(final GrpcChannelProperties properties) {
final Map<String, Object> serviceConfig = new LinkedHashMap<>();
final List<MethodConfig> methodConfigList = properties.getMethodConfig();
if (methodConfigList != null && !methodConfigList.isEmpty()) {
serviceConfig.put("methodConfig", MethodConfig.buildMaps(methodConfigList));
}
return serviceConfig;
}

/**
* Configures the keep alive options that should be used by the channel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,43 @@ public void setImmediateConnectTimeout(final Duration immediateConnectTimeout) {

// --------------------------------------------------

private Boolean retryEnabled;
private static final boolean DEFAULT_RETRY_ENABLED = false;

/**
* Gets whether retry should be enabled.
*
* @return True, if retry should be enabled. False otherwise.
* @see #setRetryEnabled(Boolean)
*/
public boolean isRetryEnabled() {
return this.retryEnabled == null ? DEFAULT_RETRY_ENABLED : this.retryEnabled;
}

/**
* Set Retry enable
*
* @param retryEnabled Whether retry enabled or null to use the fallback.
* @see ManagedChannelBuilder#enableRetry()
*/
public void setRetryEnabled(final Boolean retryEnabled) {
this.retryEnabled = retryEnabled;
}

// --------------------------------------------------

private List<MethodConfig> methodConfig;

public List<MethodConfig> getMethodConfig() {
return this.methodConfig;
}

public void setMethodConfig(final List<MethodConfig> methodConfig) {
this.methodConfig = methodConfig;
}

// --------------------------------------------------

private final Security security = new Security();

/**
Expand Down Expand Up @@ -475,6 +512,13 @@ public void copyDefaultsFrom(final GrpcChannelProperties config) {
if (this.immediateConnectTimeout == null) {
this.immediateConnectTimeout = config.immediateConnectTimeout;
}
if (this.retryEnabled == null) {
this.retryEnabled = config.retryEnabled;
}
if (this.methodConfig == null || this.methodConfig.isEmpty()) {
// TBD: Should we smartly merge the method configs?
this.methodConfig = config.methodConfig == null ? null : MethodConfig.copy(config.methodConfig);
}
this.security.copyDefaultsFrom(config.security);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.devh.boot.grpc.client.config;

import static java.util.Objects.requireNonNull;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Data;


/**
* The method config for retry policy config.
*
* <p>
* For the exact specification refer to:
* <a href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md">A6-client-retries</a>
* </p>
*
* @author wushengju
*/
@Data
public class MethodConfig {

/**
* retry policy config
*/
private RetryPolicyConfig retryPolicy;
/**
* name for list
*/
private List<NameConfig> name;


/**
* Creates a copy of this instance.
*
* @return The newly created copy.
*/
public MethodConfig copy() {
final MethodConfig copy = new MethodConfig();
copy.retryPolicy = requireNonNull(this.retryPolicy, "retryPolicy").copy();
copy.name = NameConfig.copy(this.name);
return copy;
}

/**
* Creates a copy of the given instances.
*
* @param configs The configs to copy.
* @return The copied instances.
*/
public static List<MethodConfig> copy(final List<MethodConfig> configs) {
return requireNonNull(configs, "configs").stream()
.map(MethodConfig::copy)
.collect(Collectors.toList());
}

/**
* Builds a json like map from this instance.
*
* @return The json like map representation of this instance.
*/
public Map<String, Object> buildMap() {
final Map<String, Object> map = new LinkedHashMap<>();
if (this.name != null && !this.name.isEmpty()) {
map.put("name", NameConfig.buildMaps(this.name));
}
if (this.retryPolicy != null) {
map.put("retryPolicy", this.retryPolicy.buildMap());
}
return map;
}

/**
* Builds a json like map from the given instances.
*
* @param configs The configs to convert.
* @return The json like array of maps representation of the instances.
*/
public static List<Map<String, Object>> buildMaps(final List<MethodConfig> configs) {
return requireNonNull(configs, "configs").stream()
.map(MethodConfig::buildMap)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.devh.boot.grpc.client.config;

import static java.util.Objects.requireNonNull;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Data;

/**
* The name config for service and method.
*
* <p>
* If both the service and method name are empty, then this applies to all requests.
* </p>
*
* <p>
* If only the method name is empty, then this applies to all methods in the given service.
* </p>
*
* <p>
* For the exact specification refer to:
* <a href="https://github.com/grpc/proposal/blob/master/A6-client-retries.md">A6-client-retries</a>
* </p>
*
* @author wushengju
*/
@Data
public class NameConfig {

/**
* The service name as defined in your proto file. May be empty to match all services, but may never be null.
*/
private String service = "";
/**
* The method name which you will call. May be empty to match all method within the service, but may never be null.
*/
private String method = "";

/**
* Creates a copy of this instance.
*
* @return The newly created copy.
*/
public NameConfig copy() {
final NameConfig copy = new NameConfig();
copy.service = this.service;
copy.method = this.method;
return copy;
}

/**
* Creates a copy of the given instances.
*
* @param configs The configs to copy.
* @return The copied instances.
*/
public static List<NameConfig> copy(final List<NameConfig> configs) {
return requireNonNull(configs, "configs").stream()
.map(NameConfig::copy)
.collect(Collectors.toList());
}

/**
* Builds a json like map from this instance.
*
* @return The json like map representation of this instance.
*/
public Map<String, Object> buildMap() {
final Map<String, Object> map = new LinkedHashMap<>();
map.put("service", this.service);
map.put("method", this.method);
return map;
}

/**
* Builds a json like map from the given instances.
*
* @param configs The configs to convert.
* @return The json like array of maps representation of the instances.
*/
public static List<Map<String, Object>> buildMaps(final List<NameConfig> configs) {
return requireNonNull(configs, "configs").stream()
.map(NameConfig::buildMap)
.collect(Collectors.toList());
}

}
Loading