Skip to content

Commit

Permalink
心跳调整
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Feb 14, 2016
1 parent 49e4db7 commit e2884c1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
@@ -1,16 +1,12 @@
package com.shinemo.mpush.core.server; package com.shinemo.mpush.core.server;


import java.util.concurrent.TimeUnit;


import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.connection.ConnectionManager;
import com.shinemo.mpush.api.protocol.Command; import com.shinemo.mpush.api.protocol.Command;
import com.shinemo.mpush.common.MessageDispatcher; import com.shinemo.mpush.common.MessageDispatcher;
import com.shinemo.mpush.core.handler.*; import com.shinemo.mpush.core.handler.*;
import com.shinemo.mpush.netty.connection.NettyConnectionManager; import com.shinemo.mpush.netty.connection.NettyConnectionManager;
import com.shinemo.mpush.netty.server.NettyServer; import com.shinemo.mpush.netty.server.NettyServer;
import com.shinemo.mpush.netty.server.ScanAllConnectionTimerTask;
import com.shinemo.mpush.netty.util.NettySharedHolder;
import com.shinemo.mpush.tools.config.ConfigCenter;


import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
Expand All @@ -26,7 +22,7 @@ public final class ConnectionServer extends NettyServer {


public ConnectionServer(int port) { public ConnectionServer(int port) {
super(port); super(port);
NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS); // NettySharedHolder.HASHED_WHEEL_TIMER.newTimeout(new ScanAllConnectionTimerTask(connectionManager), ConfigCenter.holder.scanConnTaskCycle() / 1000, TimeUnit.SECONDS);
} }


@Override @Override
Expand Down
Expand Up @@ -2,24 +2,42 @@




import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.eventbus.Subscribe;
import com.shinemo.mpush.api.connection.Connection; import com.shinemo.mpush.api.connection.Connection;
import com.shinemo.mpush.api.connection.ConnectionManager; import com.shinemo.mpush.api.connection.ConnectionManager;
import com.shinemo.mpush.api.event.HandshakeEvent;
import com.shinemo.mpush.common.EventBus;
import com.shinemo.mpush.log.LogType;
import com.shinemo.mpush.log.LoggerManage;
import com.shinemo.mpush.tools.config.ConfigCenter;


import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;
import io.netty.util.internal.chmv8.ConcurrentHashMapV8; import io.netty.util.internal.chmv8.ConcurrentHashMapV8;


import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;


/** /**
* Created by ohun on 2015/12/22. * Created by ohun on 2015/12/22.
*/ */
public final class NettyConnectionManager implements ConnectionManager { public final class NettyConnectionManager implements ConnectionManager {
//可能会有20w的链接数 //可能会有20w的链接数
private final ConcurrentMap<String, Connection> connections = new ConcurrentHashMapV8<>(); private final ConcurrentMap<String, Connection> connections = new ConcurrentHashMapV8<>();


private Timer wheelTimer;

@Override @Override
public void init() { public void init() {
//每秒钟走一步,一个心跳周期内走一圈
long tickDuration = 1000;//1s
int ticksPerWheel = (int) (ConfigCenter.holder.maxHeartbeat() / tickDuration);
this.wheelTimer = new HashedWheelTimer(tickDuration, TimeUnit.MILLISECONDS, ticksPerWheel);
EventBus.INSTANCE.register(this);
} }


@Override @Override
Expand All @@ -44,5 +62,44 @@ public void remove(Channel channel) {
public List<Connection> getConnections() { public List<Connection> getConnections() {
return Lists.newArrayList(connections.values()); return Lists.newArrayList(connections.values());
} }

@Subscribe
void onHandshakeOk(HandshakeEvent event) {
HeartbeatCheckTask task = new HeartbeatCheckTask(event.heartbeat, event.connection);
task.startTimeout();
}

private class HeartbeatCheckTask implements TimerTask {
private int expiredTimes = 0;
private final int heartbeat;
private final Connection connection;

public HeartbeatCheckTask(int heartbeat, Connection connection) {
this.heartbeat = heartbeat;
this.connection = connection;
}

public void startTimeout() {
wheelTimer.newTimeout(this, heartbeat, TimeUnit.MILLISECONDS);
}

@Override
public void run(Timeout timeout) throws Exception {
if (!connection.isConnected()) return;
if (connection.heartbeatTimeout()) {
if (++expiredTimes > ConfigCenter.holder.maxHBTimeoutTimes()) {
connection.close();
LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, connection has bean closed:%s,%s", connection.getChannel(),connection.getSessionContext().deviceId);
return;
} else {
LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat timeout, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId);
}
} else {
expiredTimes = 0;
LoggerManage.info(LogType.HEARTBEAT, "connection heartbeat reset, expiredTimes:%s,%s,%s", expiredTimes,connection.getChannel(),connection.getSessionContext().deviceId);
}
startTimeout();
}
}


} }
Expand Up @@ -28,7 +28,7 @@ public abstract class NettyServer implements Server {


public enum State {Created, Initialized, Starting, Started, Shutdown} public enum State {Created, Initialized, Starting, Started, Shutdown}


protected final AtomicReference<State> serverState = new AtomicReference(State.Created); protected final AtomicReference<State> serverState = new AtomicReference<>(State.Created);


private final int port; private final int port;
private EventLoopGroup bossGroup; private EventLoopGroup bossGroup;
Expand Down Expand Up @@ -162,7 +162,8 @@ private void createNioServer(final Listener listener) {
} }




private void createEpollServer(final Listener listener) { @SuppressWarnings("unused")
private void createEpollServer(final Listener listener) {
EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor); EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(1, ThreadPoolManager.bossExecutor);
EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor); EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(0, ThreadPoolManager.workExecutor);
createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class); createServer(listener, bossGroup, workerGroup, EpollServerSocketChannel.class);
Expand Down
@@ -1,6 +1,7 @@
package com.shinemo.mpush.test.redis; package com.shinemo.mpush.test.redis;


import java.util.List; import java.util.List;
import java.util.concurrent.locks.LockSupport;


import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
Expand All @@ -27,14 +28,32 @@ public void init(){
} }


@Test @Test
public void pubSubTest(){ public void subpubTest(){

RedisManage.subscribe(Subscriber.holder, "/hello/123"); RedisManage.subscribe(Subscriber.holder, "/hello/123");

RedisManage.subscribe(Subscriber.holder, "/hello/124");
RedisManage.subscribe(Subscriber.holder, "/hello/124");

RedisManage.publish("/hello/123", "123"); RedisManage.publish("/hello/123", "123");
RedisManage.publish("/hello/124", "124"); RedisManage.publish("/hello/124", "124");
} }


@Test
public void pubsubTest(){
RedisManage.publish("/hello/123", "123");
RedisManage.publish("/hello/124", "124");
RedisManage.subscribe(Subscriber.holder, "/hello/123");
RedisManage.subscribe(Subscriber.holder, "/hello/124");
}

@Test
public void pubTest(){
RedisManage.publish("/hello/123", "123");
RedisManage.publish("/hello/124", "124");
}

@Test
public void subTest(){
RedisManage.subscribe(Subscriber.holder, "/hello/123");
RedisManage.subscribe(Subscriber.holder, "/hello/124");
LockSupport.park();
}

} }
4 changes: 2 additions & 2 deletions mpush-test/src/test/resources/logback.xml
Expand Up @@ -15,7 +15,7 @@
<target>System.err</target> <target>System.err</target>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level> <level>debug</level>
</filter> </filter>
<layout class="ch.qos.logback.classic.PatternLayout"> <layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n</pattern> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level - %logger{35} - %msg%n</pattern>
Expand All @@ -24,7 +24,7 @@




<root> <root>
<level value="warn" /> <level value="debug" />
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>
</configuration> </configuration>

0 comments on commit e2884c1

Please sign in to comment.