Skip to content

Commit 7c0d469

Browse files
committed
Better lifecycle handling of netty
NettyHomekitHttpService previously held an unnecessary static reference that prevented the destruction and re-creation of a HomekitServer.
1 parent 642c044 commit 7c0d469

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

src/main/java/com/beowulfe/hap/impl/http/impl/HomekitHttpServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public HomekitHttpServer(int port, int nThreads) {
2626
@Override
2727
public CompletableFuture<Integer> start(HomekitClientConnectionFactory clientConnectionFactory) {
2828
if (service == null) {
29-
this.service = NettyHomekitHttpService.get(port, nThreads);
29+
this.service = NettyHomekitHttpService.create(port, nThreads);
3030
return this.service.create(clientConnectionFactory);
3131
} else {
3232
throw new RuntimeException("HomekitHttpServer can only be started once");

src/main/java/com/beowulfe/hap/impl/http/impl/NettyHomekitHttpService.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.beowulfe.hap.impl.http.impl;
22

33
import io.netty.bootstrap.ServerBootstrap;
4-
import io.netty.channel.ChannelFuture;
5-
import io.netty.channel.ChannelOption;
6-
import io.netty.channel.EventLoopGroup;
4+
import io.netty.channel.*;
75
import io.netty.channel.group.ChannelGroup;
86
import io.netty.channel.group.DefaultChannelGroup;
97
import io.netty.channel.nio.NioEventLoopGroup;
108
import io.netty.channel.socket.nio.NioServerSocketChannel;
119
import io.netty.handler.logging.LogLevel;
1210
import io.netty.handler.logging.LoggingHandler;
13-
import io.netty.util.concurrent.Future;
14-
import io.netty.util.concurrent.GenericFutureListener;
15-
import io.netty.util.concurrent.GlobalEventExecutor;
11+
import io.netty.util.concurrent.*;
1612

1713
import java.net.InetSocketAddress;
1814
import java.net.SocketAddress;
@@ -29,20 +25,12 @@ class NettyHomekitHttpService {
2925
private final EventLoopGroup workerGroup;
3026

3127
private final static Logger logger = LoggerFactory.getLogger(NettyHomekitHttpService.class);
32-
private volatile static NettyHomekitHttpService INSTANCE;
3328
private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
3429
private final int port;
3530
private final int nThreads;
3631

37-
public static NettyHomekitHttpService get(int port, int nThreads) {
38-
if (INSTANCE == null) {
39-
synchronized(NettyHomekitHttpService.class) {
40-
if (INSTANCE == null) {
41-
INSTANCE = new NettyHomekitHttpService(port, nThreads);
42-
}
43-
}
44-
}
45-
return INSTANCE;
32+
public static NettyHomekitHttpService create(int port, int nThreads) {
33+
return new NettyHomekitHttpService(port, nThreads);
4634
}
4735

4836
private NettyHomekitHttpService(int port, int nThreads) {

0 commit comments

Comments
 (0)