Skip to content
Merged
4 changes: 2 additions & 2 deletions study/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ handlebars:

server:
tomcat:
accept-count: 1
max-connections: 1
accept-count: 2

Choose a reason for hiding this comment

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

accept-count는 무엇이고 어떤 이유로 값을 2로 설정했나요?

Copy link
Author

Choose a reason for hiding this comment

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

accept-countmax-connections이상의 요청이 들어왔을 때 max-connections를 초과한 수만큼 큐에 담아두는데 이때 담는 큐의 사이즈를 의미합니다!

지금 다시 생각해보니 굳이 바꿀 이유가 없었네요...

max-connections: 2

Choose a reason for hiding this comment

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

max-connections는 무엇을 의미하고 이것 또한 어떤 이유로 값을 2로 설정했나요?

Copy link
Author

@unifolio0 unifolio0 Sep 14, 2024

Choose a reason for hiding this comment

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

max-connections는 서버가 동시에 처리할 수 있는 최대 연결 수를 의미합니다!

사실 위의 답글에 accept-count를 바꿀 필요가 없다고 했었지만 max-connections의 값과 accept-count의 합을 테스트에서 기대되는 값만큼으로 설정하고 TestHttpUtils에서 timeout의 값을 충분히 높게 설정해주면 테스트가 통과합니다!

하지만 가능한 적은 변화로 테스트가 통과되길 원해서 max-connections를 테스트에서 기대되는 값만큼으로 설정해서 2개를 제외한 나머지 스레드는 timeout이 발생해서 incrementIfOk에서 값을 증가시키지 못하도록 설정했습니다!

threads:
min-spare: 2
max: 2
Comment on lines 9 to 10

Choose a reason for hiding this comment

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

min-spare, max 또한 설명 부탁드려요!

Copy link
Author

Choose a reason for hiding this comment

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

  • min-spare는 스레드 풀에 대기 상태로 있을 수 있는 스레드 개수 입니다! 요청이 없어도 해당 개수만큼의 스레드를 유지하도록 하여 서버의 반응성을 높일 수 있습니다.
  • max는 동시에 실행할 수 있는 최대 스레드의 개수입니다!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public abstract class AbstractController implements Controller {
public void service(HttpRequest httpRequest, HttpResponse httpResponse) {
if (httpRequest.isMethod(HttpMethod.GET)) {
doGet(httpRequest, httpResponse);
return;
}
if (httpRequest.isMethod(HttpMethod.POST)) {
doPost(httpRequest, httpResponse);
return;
}

throw new IllegalArgumentException("유효하지 않은 메소드입니다.");
Expand Down
17 changes: 11 additions & 6 deletions tomcat/src/main/java/org/apache/catalina/connector/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.coyote.http11.Http11Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -14,16 +16,19 @@ public class Connector implements Runnable {

private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_ACCEPT_COUNT = 100;
private static final int DEFAULT_MAX_THREADS = 250;

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

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

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.executor = Executors.newFixedThreadPool(maxThreads);
this.stopped = false;
}

Expand All @@ -38,10 +43,8 @@ private ServerSocket createServerSocket(final int port, final int acceptCount) {
}

public void start() {
var thread = new Thread(this);
thread.setDaemon(true);
thread.start();
stopped = false;
executor.execute(this);
log.info("Web Application Server started {} port.", serverSocket.getLocalPort());
}

Expand All @@ -66,7 +69,7 @@ private void process(final Socket connection) {
return;
}
var processor = new Http11Processor(connection);
new Thread(processor).start();
executor.execute(processor);

Choose a reason for hiding this comment

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

지난 변경 사항이 아니라서 여기다 코멘트 남겨요!
Connector의 start 메서드도 ExecutorService를 사용하도록 변경해주세요~~

Copy link
Author

Choose a reason for hiding this comment

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

start 메소드도 스레드 풀을 사용하도록 수정했습니다!

}

public void stop() {
Expand All @@ -75,6 +78,8 @@ public void stop() {
serverSocket.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
executor.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private HttpResponse createResponse(HttpRequest httpRequest) {
HttpResponse httpResponse = new HttpResponse();
try {
handle(httpRequest, httpResponse);
return httpResponse;
} catch (NotFoundException e) {
httpResponse.location(httpRequest, NOT_FOUND_PATH);
} catch (UnauthorizedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public byte[] toResponse() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(httpStatusLine.createStatusLineResponse())
.append(RESPONSE_LINE_DELIMITER)
.append(httpResponseHeader.createHeadersResponse());
.append(httpResponseHeader.createHeadersResponse())
.append(RESPONSE_LINE_DELIMITER);
if (httpResponseBody != null) {
stringBuilder.append(RESPONSE_LINE_DELIMITER)
.append(httpResponseBody.getBody());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.apache.coyote.http11.session;

import com.techcourse.model.User;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Session {

Expand All @@ -13,7 +13,7 @@ public class Session {

public Session(String id) {
this.id = id;
this.attributes = new HashMap<>();
this.attributes = new ConcurrentHashMap<>();
}

public boolean hasAttribute(String name) {
Expand Down