Skip to content

Commit

Permalink
feat(halo): Support listener of handler invoke async.
Browse files Browse the repository at this point in the history
[Comet] Fixed socket read error after close connection by client.
[Moon] Change percent parse api simplify.
  • Loading branch information
yizzuide committed Mar 23, 2020
1 parent 256c5a2 commit e625d1e
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>2.7.4-SNAPSHOT</project.release.version>
<project.release.version>2.7.5-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -65,7 +65,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>2.7.4</project.release.version>
<project.release.version>2.7.5</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.yizzuide.milkomeda.comet;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ReadListener;
Expand All @@ -9,6 +10,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;

/**
Expand All @@ -17,7 +19,7 @@
*
* @author yizzuide
* @since 2.0.0
* @version 2.2.2
* @version 2.7.5
* Create at 2019/12/12 17:37
*/
@Slf4j
Expand Down Expand Up @@ -48,7 +50,7 @@ public String getBodyString(final ServletRequest request) {
try {
return inputStream2String(request.getInputStream());
} catch (IOException e) {
log.error("read error", e);
log.error("Comet get input stream error:{}", e.getMessage(), e);
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -82,15 +84,17 @@ private String inputStream2String(InputStream inputStream) {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (ClientAbortException | SocketTimeoutException ignore) {
return null;
} catch (IOException e) {
log.error("read error", e);
log.error("Comet read input stream error:{}", e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.error("read error", e);
log.error("Comet close input stream error:{}", e.getMessage(), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ protected void signParam(Map<String, Object> inParams, Map<String, Object> outPa
* @param headers HttpHeaders
*/
protected void appendHeaders(HttpHeaders headers) {
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yizzuide.milkomeda.halo;

import com.github.yizzuide.milkomeda.pulsar.EnablePulsar;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
Expand All @@ -9,12 +10,14 @@
*
* @author yizzuide
* @since 2.5.0
* @version 2.7.5
* Create at 2020/01/30 18:42
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnablePulsar
@Import(HaloConfig.class)
public @interface EnableHalo {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class HaloContext implements ApplicationListener<ContextRefreshedEvent> {
* 监听类型的方法属性名
*/
public static final String ATTR_TYPE = "type";
/**
* 调用处理异步方式的方法属性名
*/
public static final String ATTR_ASYNC = "async";

private static Map<String, List<HandlerMetaData>> tableNameMap = new HashMap<>();

Expand All @@ -32,6 +36,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
// 设置其它属性方法的值
Map<String, Object> attrs = new HashMap<>(2);
attrs.put(ATTR_TYPE, haloListener.type());
attrs.put(ATTR_ASYNC, haloListener.async());
metaData.setAttributes(attrs);
return haloListener.value();
}, false, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yizzuide.milkomeda.halo;

import com.github.yizzuide.milkomeda.pulsar.PulsarHolder;
import com.github.yizzuide.milkomeda.universe.metadata.HandlerMetaData;
import com.github.yizzuide.milkomeda.util.MybatisUtil;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,7 +23,7 @@
*
* @author yizzuide
* @since 2.5.0
* @version 2.7.4
* @version 2.7.5
* Create at 2020/01/30 20:38
*/
@Slf4j
Expand Down Expand Up @@ -81,7 +82,13 @@ private void invokeWithTable(String tableName, String matchTableName, MappedStat
}
HaloContext.getTableNameMap().get(matchTableName).stream()
.filter(metaData -> metaData.getAttributes().get(HaloContext.ATTR_TYPE) == type)
.forEach(handlerMetaData -> invokeHandler(tableName, handlerMetaData, mappedStatement, param, result));
.forEach(handlerMetaData -> {
if ((boolean) handlerMetaData.getAttributes().get(HaloContext.ATTR_ASYNC)) {
PulsarHolder.getPulsar().post(() -> invokeHandler(tableName, handlerMetaData, mappedStatement, param, result));
} else {
invokeHandler(tableName, handlerMetaData, mappedStatement, param, result);
}
});
}

private void invokeHandler(String tableName, HandlerMetaData handlerMetaData, MappedStatement mappedStatement, Object param, Object result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author yizzuide
* @since 2.5.0
* @version 2.5.1
* @version 2.7.5
* Create at 2020/01/30 22:36
*/
@Documented
Expand All @@ -45,4 +45,10 @@
* @return 默认为后置监听
*/
HaloType type() default HaloType.POST;

/**
* 支持异步处理
* @return 默认使用异步处理
*/
boolean async() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
@Inherited
public @interface LightCacheEvict {
/**
* 缓存实例名(不同的缓存类型应该设置不能的名字)
* 缓存实例名(不同的缓存类型应该设置不能的名字),支持EL表达式
* @return String
*/
String value();

/**
* 缓存key,支持EL表达式获取参数的值
* 缓存key,支持EL表达式
* @return String
*/
String key();
Expand All @@ -34,7 +34,7 @@
String keyPrefix() default "";

/**
* 缓存条件,使用EL表达式
* 缓存条件,需要使用EL表达式
* @return String
*/
String condition() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
@Inherited
public @interface LightCachePut {
/**
* 缓存实例名(不同的缓存类型应该设置不能的名字)
* 缓存实例名(不同的缓存类型应该设置不能的名字),支持EL表达式
* @return String
*/
String value();

/**
* 缓存key,支持EL表达式获取参数的值
* 缓存key,支持EL表达式
* @return String
*/
String key();
Expand All @@ -34,7 +34,7 @@
String keyPrefix() default "";

/**
* 缓存条件,使用EL表达式
* 缓存条件,需要使用EL表达式
* @return String
*/
String condition() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
@Inherited
public @interface LightCacheable {
/**
* 缓存实例名(不同的缓存类型应该设置不能的名字)
* 缓存实例名(不同的缓存类型应该设置不能的名字),支持EL表达式
* @return String
*/
String value();

/**
* 缓存key,支持EL表达式获取参数的值
* 缓存key,支持EL表达式
* @return String
*/
String key();
Expand All @@ -34,7 +34,7 @@
String keyPrefix() default "";

/**
* 缓存条件,使用EL表达式
* 缓存条件,需要使用EL表达式
* @return String
*/
String condition() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @author yizzuide
* @since 2.6.0
* @version 2.7.3
* @version 2.7.5
* Create at 2020/03/13 21:42
*/
@Data
Expand All @@ -24,7 +24,7 @@ public class PercentMoonStrategy implements MoonStrategy {
/**
* 百分比
*/
public int percent = DEFAULT_PERCENT;
private int percent = DEFAULT_PERCENT;

@Override
public <T> T getCurrentPhase(Moon<T> moon) {
Expand Down Expand Up @@ -61,44 +61,22 @@ public LeftHandPointer pluck(Moon<?> moon, LeftHandPointer leftHandPointer) {
}

/**
* 百分比表达式解析, 固定百分总值为100。如:5/5、3/7、25/75
* @param percentExpress 百分比表达式
* @return 百分比列表
*/
public static Integer[] parse(String percentExpress) {
return parse(percentExpress, null);
}

/**
* 百分比表达式解析, 并自动缩放百分总值 <br>
* 百分比表达式解析 <br>
* <pre>
* 5/5、3/7:百分总值为10
* 25/75:百分总值为100
* 百分总量为10时:5/5、3/7、1/5/4、0/10、10/0
* 百分总量为100时:25/75、10/20/70、0/100、100/0
* </pre>
* @param percentExpress 百分比表达式
* @param strategy PercentMoonStrategy
* @return 百分比列表
*/
public static Integer[] parse(String percentExpress, MoonStrategy strategy) {
public static Integer[] parse(String percentExpress) {
String[] percentComps = percentExpress.split("/");
if (percentComps.length < 2) {
int len = percentComps.length;
if (len < 2) {
throw new IllegalArgumentException("Percent express format is illegal: " + percentExpress);
}
Integer[] percentArray = new Integer[percentComps.length];
for (int i = 0; i < percentComps.length; i++) {
String percentComp = percentComps[i];
if (percentComp.length() > 2 || percentComp.startsWith("0")) {
throw new IllegalArgumentException("Percent express format is illegal: " + percentExpress);
}
if (percentComp.length() == 1) {
if (strategy == null) {
// 补充到百分的比例
percentComps[i] = percentComp + "0";
} else if (strategy instanceof PercentMoonStrategy){
// 缩小百分分配总值
((PercentMoonStrategy) strategy).setPercent(10);
}
}
Integer[] percentArray = new Integer[len];
for (int i = 0; i < len; i++) {
percentArray[i] = Integer.valueOf(percentComps[i]);
}
return percentArray;
Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<milkomeda.version>2.7.4-SNAPSHOT</milkomeda.version>
<milkomeda.version>2.7.5-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public Moon<String> smsMoon() {
@Bean("abTestMoon")
public Moon<Integer> abTestMoon() {
Moon<Integer> moon = new Moon<>();
// 设置L2缓存不过期
// 设置缓存缓存实例名(需要在milkomeda.light.instances下配置不同的缓存实例)
moon.setCacheName("ab-test");
moon.setMoonStrategy(new PercentMoonStrategy());
PercentMoonStrategy moonStrategy = new PercentMoonStrategy();
// 设置百分比总量(默认为100)
moonStrategy.setPercent(10);
moon.setMoonStrategy(moonStrategy);
// AB测试阶段值:15%为0,85%为1
// moon.add(15, 85);
// moon.add(PercentMoonStrategy.parse("15/85"));
moon.add(PercentMoonStrategy.parse("3/7", moon.getMoonStrategy()));
moon.add(PercentMoonStrategy.parse("3/7"));
return moon;
}
}

0 comments on commit e625d1e

Please sign in to comment.