This repository was archived by the owner on Aug 20, 2025. It is now read-only.
키움 OpenAPI 최대 조회건수 #22
cyberprophet
started this conversation in
General
Replies: 1 comment
-
using System;
using System.Threading;
class Program
{
// 조회 제약조건
const int MaxRequestsPerSecond = 5;
const int MaxRequestsPerMinute = 100;
const int MaxRequestsPerHour = 1000;
// 조회한 건수와 초기화 시간을 관리하는 변수
static int requestsThisSecond = 0;
static int requestsThisMinute = 0;
static int requestsThisHour = 0;
static DateTime lastResetTime = DateTime.Now;
// 동기화 객체
static readonly object lockObject = new object();
static void Main()
{
// 예제에서는 20번의 조회를 시도합니다.
for (int i = 0; i < 20; i++)
{
// 조회 제약을 체크하고 필요한 경우 초기화
CheckAndResetLimits();
if (CheckRateLimit())
{
// 조회 가능한 경우
Console.WriteLine($"조회 성공: {DateTime.Now}");
// 여기에서 실제 조회 작업을 수행하면 됩니다.
// 조회한 건수 증가
UpdateRequestCounts();
// 간단하게 대기를 위해 Sleep을 사용
Thread.Sleep(1000); // 1초 대기
}
else
{
// 조회 제한 초과로 대기해야 하는 경우
Console.WriteLine($"조회 제한 초과: {DateTime.Now}");
// 대기 시간 동안 다른 작업을 수행하거나, 필요에 따라 예외 처리 등을 수행할 수 있습니다.
// 대기
Thread.Sleep(GetDelayMilliseconds());
}
}
}
// 조회 제약을 체크하고 필요한 경우 초기화하는 메서드
static void CheckAndResetLimits()
{
lock (lockObject)
{
// 시간이 경과하면 조회 횟수 초기화
TimeSpan elapsed = DateTime.Now - lastResetTime;
if (elapsed.TotalSeconds >= 1)
{
requestsThisSecond = 0;
lastResetTime = DateTime.Now;
}
if (elapsed.TotalMinutes >= 1)
{
requestsThisMinute = 0;
}
if (elapsed.TotalHours >= 1)
{
requestsThisHour = 0;
}
}
}
// 조회 제약을 체크하는 메서드
static bool CheckRateLimit()
{
lock (lockObject)
{
return requestsThisSecond < MaxRequestsPerSecond &&
requestsThisMinute < MaxRequestsPerMinute &&
requestsThisHour < MaxRequestsPerHour;
}
}
// 조회한 건수를 업데이트하는 메서드
static void UpdateRequestCounts()
{
lock (lockObject)
{
requestsThisSecond++;
requestsThisMinute++;
requestsThisHour++;
}
}
// 대기해야 하는 시간을 계산하는 메서드
static int GetDelayMilliseconds()
{
// 초당 최대 조회 허용 횟수를 초과한 경우
if (requestsThisSecond >= MaxRequestsPerSecond)
{
return 1000; // 1초 대기
}
// 분당 최대 조회 허용 횟수를 초과한 경우
if (requestsThisMinute >= MaxRequestsPerMinute)
{
return 60000; // 1분 대기
}
// 시간당 최대 조회 허용 횟수를 초과한 경우
if (requestsThisHour >= MaxRequestsPerHour)
{
return 3600000; // 1시간 대기
}
return 0; // 대기할 필요가 없는 경우
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
using System;
using System.Threading;
Beta Was this translation helpful? Give feedback.
All reactions