Skip to content

Commit 095564f

Browse files
committed
feat: websocket keep-alive 연결 시간 수정
1 parent 295337c commit 095564f

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ GOOGLE_OAUTH_REDIRECT_URI=http://localhost:7900/auth/oauth2/callback
4141
GOOGLE_OAUTH_AUTO_CREATE_USER=true
4242
GOOGLE_OAUTH_DEFAULT_ROLE=User
4343

44+
# WebSocket Configuration
45+
WEBSOCKET_KEEPALIVE_MINUTES=0
46+
WEBSOCKET_RECEIVE_BUFFER_SIZE=4096
47+
WEBSOCKET_SEND_BUFFER_SIZE=4096
48+
4449
# Application Configuration
4550
ASPNETCORE_ENVIRONMENT=Production
4651

ProjectVG.Api/ApiMiddlewareExtensions.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class ApiMiddlewareExtensions
1111
/// </summary>
1212
public static IApplicationBuilder UseApiMiddleware(this IApplicationBuilder app, IWebHostEnvironment environment)
1313
{
14+
var configuration = app.ApplicationServices.GetRequiredService<IConfiguration>();
1415
// 개발 환경 설정
1516
if (environment.IsDevelopment()) {
1617
app.UseSwagger();
@@ -23,8 +24,9 @@ public static IApplicationBuilder UseApiMiddleware(this IApplicationBuilder app,
2324
// 전역 예외 처리
2425
app.UseGlobalExceptionHandler();
2526

26-
// WebSocket 지원
27-
app.UseWebSockets();
27+
// WebSocket 지원 - 구성 가능한 옵션 사용
28+
var webSocketOptions = GetWebSocketOptions(configuration);
29+
app.UseWebSockets(webSocketOptions);
2830

2931
// WebSocket 미들웨어 등록
3032
app.UseMiddleware<WebSocketMiddleware>();
@@ -63,5 +65,56 @@ public static IApplicationBuilder UseDevelopmentFeatures(this IApplicationBuilde
6365

6466
return app;
6567
}
68+
69+
/// <summary>
70+
/// WebSocket 옵션을 구성 파일과 환경 변수에서 가져옵니다
71+
/// </summary>
72+
private static WebSocketOptions GetWebSocketOptions(IConfiguration configuration)
73+
{
74+
var options = new WebSocketOptions();
75+
76+
// KeepAliveInterval 설정 (환경 변수 > appsettings.json 순서)
77+
var keepAliveMinutes = Environment.GetEnvironmentVariable("WEBSOCKET_KEEPALIVE_MINUTES");
78+
if (string.IsNullOrEmpty(keepAliveMinutes))
79+
{
80+
keepAliveMinutes = configuration.GetValue<string>("WebSocket:KeepAliveIntervalMinutes");
81+
}
82+
83+
if (double.TryParse(keepAliveMinutes, out var minutes))
84+
{
85+
if (minutes <= 0)
86+
{
87+
options.KeepAliveInterval = TimeSpan.Zero; // KeepAlive 비활성화
88+
}
89+
else
90+
{
91+
options.KeepAliveInterval = TimeSpan.FromMinutes(minutes);
92+
}
93+
}
94+
else
95+
{
96+
// 기본값: KeepAlive 비활성화 (연결 안정성을 위해)
97+
options.KeepAliveInterval = TimeSpan.Zero;
98+
}
99+
100+
// 수신 버퍼 크기 설정
101+
var receiveBufferSize = Environment.GetEnvironmentVariable("WEBSOCKET_RECEIVE_BUFFER_SIZE") ??
102+
configuration.GetValue<string>("WebSocket:ReceiveBufferSize");
103+
if (int.TryParse(receiveBufferSize, out var recvSize) && recvSize > 0)
104+
{
105+
options.ReceiveBufferSize = recvSize;
106+
}
107+
108+
// 송신 버퍼 크기 설정 (WebSocketOptions에는 없으므로 로깅만)
109+
var sendBufferSize = Environment.GetEnvironmentVariable("WEBSOCKET_SEND_BUFFER_SIZE") ??
110+
configuration.GetValue<string>("WebSocket:SendBufferSize");
111+
112+
// 콘솔 로깅으로 설정 확인
113+
Console.WriteLine($"[WebSocket 설정] KeepAlive: {(options.KeepAliveInterval == TimeSpan.Zero ? "비활성화" : $"{options.KeepAliveInterval.TotalMinutes}분")}, " +
114+
$"ReceiveBuffer: {options.ReceiveBufferSize} bytes" +
115+
$"{(int.TryParse(sendBufferSize, out var sendSize) && sendSize > 0 ? $", SendBuffer: {sendSize} bytes (참고용)" : "")}");
116+
117+
return options;
118+
}
66119
}
67120
}

ProjectVG.Api/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
"TTS": {
2828
"BaseUrl": "https://supertoneapi.com"
2929
},
30+
"WebSocket": {
31+
"KeepAliveIntervalMinutes": 10,
32+
"ReceiveBufferSize": 4096,
33+
"SendBufferSize": 4096
34+
},
3035
"ConnectionStrings": {
3136
"DefaultConnection": "Server=localhost,1433;Database=ProjectVG;User Id=sa;Password=ProjectVG123!;TrustServerCertificate=true;MultipleActiveResultSets=true",
3237
"Redis": "projectvg-redis:6379"

0 commit comments

Comments
 (0)