Skip to content
Merged
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 @@ -3,29 +3,33 @@
import org.apache.coyote.http11.Http11Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Connector implements Runnable {

private static final Logger log = LoggerFactory.getLogger(Connector.class);

private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_ACCEPT_COUNT = 100;
private static final int DEFAULT_MAX_THREAD = 10;
Comment on lines 18 to +19
Copy link

Choose a reason for hiding this comment

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

acceptCount와 maxThreads는 각각 어떤 설정인가요?

Copy link
Member Author

@tkdgur0906 tkdgur0906 Sep 19, 2024

Choose a reason for hiding this comment

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

acceptCount는 큐에 대기할 수 있는 스레드의 개수를 의미합니다.
maxThreads는 스레드 풀에 생성할 스레드의 개수와 동시에 실행할 수 있는 스레드의 최대 개수를 의미합니다.


private final ServerSocket serverSocket;
private boolean stopped;
private final ExecutorService executorService;

public Connector() {
this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT);
this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT, DEFAULT_MAX_THREAD);
}

public Connector(final int port, final int acceptCount) {
public Connector(final int port, final int acceptCount, final int maxThreads) {
this.serverSocket = createServerSocket(port, acceptCount);
this.stopped = false;
executorService = Executors.newFixedThreadPool(maxThreads);
Copy link

Choose a reason for hiding this comment

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

Executors.newFixedThreadPool 은 내부적으로 LinkedBlockingQueue를 대기큐로 사용하여 대기 큐의 크기 제한이 없다고 합니다.
스레드풀 대기 큐의 크기를 제한하고 싶다면 어떤 방법을 사용할 수 있을까요? (코드에 반영하지 않으셔도 됩니다)

Copy link
Member Author

@tkdgur0906 tkdgur0906 Sep 19, 2024

Choose a reason for hiding this comment

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

static 메서드를 통해 바로 생성하는 것 대신 new ThreadPoolExecutor()를 직접 생성해서 파라미터로 대기큐의 크기를 설정해서 넘길 수 있다고 합니다!
아직 대기큐 설정에 대한 기준이 없어 기존 방식 그대로 유지했습니다!

}

private ServerSocket createServerSocket(final int port, final int acceptCount) {
Expand Down Expand Up @@ -67,7 +71,7 @@ private void process(final Socket connection) {
return;
}
var processor = new Http11Processor(connection);
new Thread(processor).start();
executorService.submit(processor);
Copy link

Choose a reason for hiding this comment

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

submit() 과 execute() 는 어떤 차이가 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

가장 큰 차이점은 submit()은 실행결과를 나타내는 Future를 반환하는 반면,
execute()는 결과를 반환하지 않는다는 점 입니다.

그래서 결과 값이 필요하거나 예외처리해야하는 경우에 submit()을 사용하고, 그게 아니라면 execute()를 사용해도 무방할 것 같습니다!

}

public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SessionManager implements Manager {

private static final Map<String, Session> SESSIONS = new HashMap<>();
private static final Map<String, Session> SESSIONS = new ConcurrentHashMap<>();

public SessionManager() {
}
Expand Down