@k-msg/webhook 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -415,4 +415,4 @@ MIT License - [LICENSE](../../LICENSE) 파일을 참조하세요.
415
415
 
416
416
  **K-Message** - 한국형 멀티채널 메시징 플랫폼
417
417
 
418
- 🌟 [GitHub](https://github.com/imjlk/k-message) | 📖 [Documentation](https://k-message.dev) | 💬 [Discord](https://discord.gg/k-message)
418
+ 🌟 [GitHub](https://github.com/imjlk/k-msg) | 📖 [Documentation](https://k-msg.dev) | 💬 [Discord](https://discord.gg/k-msg)
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Batch Webhook Dispatcher
3
+ * 대량 웹훅 요청을 효율적으로 처리하는 배치 디스패처
4
+ */
5
+ import { EventEmitter } from "events";
6
+ import type { WebhookBatch } from "../types/webhook.types";
7
+ import type { BatchConfig, DispatchJob } from "./types";
8
+ export declare class BatchDispatcher extends EventEmitter {
9
+ private config;
10
+ private pendingJobs;
11
+ private activeBatches;
12
+ private batchProcessor;
13
+ private defaultConfig;
14
+ constructor(config?: Partial<BatchConfig>);
15
+ /**
16
+ * 배치 작업 추가
17
+ */
18
+ addJob(job: DispatchJob): Promise<void>;
19
+ /**
20
+ * 특정 엔드포인트의 배치 처리
21
+ */
22
+ processBatchForEndpoint(endpointId: string): Promise<WebhookBatch | null>;
23
+ /**
24
+ * 모든 대기 중인 배치 처리
25
+ */
26
+ processAllBatches(): Promise<WebhookBatch[]>;
27
+ /**
28
+ * 배치 통계 조회
29
+ */
30
+ getBatchStats(): {
31
+ pendingJobsCount: number;
32
+ activeBatchesCount: number;
33
+ endpointsWithPendingJobs: number;
34
+ averageQueueSize: number;
35
+ };
36
+ /**
37
+ * 특정 엔드포인트의 대기 중인 작업 수 조회
38
+ */
39
+ getPendingJobCount(endpointId: string): number;
40
+ /**
41
+ * 배치 처리기 시작
42
+ */
43
+ private startBatchProcessor;
44
+ /**
45
+ * 우선순위 기반 작업 삽입
46
+ */
47
+ private insertJobByPriority;
48
+ /**
49
+ * 배치 생성
50
+ */
51
+ private createBatch;
52
+ /**
53
+ * 배치 실행
54
+ */
55
+ private executeBatch;
56
+ /**
57
+ * 개별 작업 실행 (실제로는 WebhookDispatcher 사용)
58
+ */
59
+ private executeJob;
60
+ /**
61
+ * 실패한 작업들을 다시 큐에 추가
62
+ */
63
+ private requeueFailedJobs;
64
+ /**
65
+ * 배치 처리기 정지
66
+ */
67
+ shutdown(): Promise<void>;
68
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Webhook Dispatcher Components
3
+ * 웹훅 전송 및 배치 처리 관련 컴포넌트들
4
+ */
5
+ export { WebhookDispatcher } from "../services/webhook.dispatcher";
6
+ export { BatchDispatcher } from "./batch.dispatcher";
7
+ export { LoadBalancer } from "./load-balancer";
8
+ export { QueueManager } from "./queue.manager";
9
+ export type { BatchConfig, DispatchConfig, LoadBalancerConfig, QueueConfig, } from "./types";
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Load Balancer
3
+ * 웹훅 엔드포인트 간의 부하 분산 관리
4
+ */
5
+ import { EventEmitter } from "events";
6
+ import type { WebhookEndpoint } from "../types/webhook.types";
7
+ import type { LoadBalancerConfig } from "./types";
8
+ interface EndpointHealth {
9
+ endpointId: string;
10
+ isHealthy: boolean;
11
+ consecutiveFailures: number;
12
+ lastHealthCheckAt: Date;
13
+ averageResponseTime: number;
14
+ activeConnections: number;
15
+ }
16
+ export declare class LoadBalancer extends EventEmitter {
17
+ private config;
18
+ private endpointHealth;
19
+ private circuitBreakers;
20
+ private connectionCounts;
21
+ private roundRobinIndex;
22
+ private healthCheckInterval;
23
+ private defaultConfig;
24
+ constructor(config?: Partial<LoadBalancerConfig>);
25
+ /**
26
+ * 엔드포인트 등록
27
+ */
28
+ registerEndpoint(endpoint: WebhookEndpoint): Promise<void>;
29
+ /**
30
+ * 엔드포인트 등록 해제
31
+ */
32
+ unregisterEndpoint(endpointId: string): Promise<void>;
33
+ /**
34
+ * 로드 밸런싱을 통한 엔드포인트 선택
35
+ */
36
+ selectEndpoint(endpoints: WebhookEndpoint[]): Promise<WebhookEndpoint | null>;
37
+ /**
38
+ * 요청 완료 시 호출 (연결 수 감소 및 통계 업데이트)
39
+ */
40
+ onRequestComplete(endpointId: string, success: boolean, responseTime: number): Promise<void>;
41
+ /**
42
+ * 엔드포인트 건강 상태 조회
43
+ */
44
+ getEndpointHealth(endpointId: string): EndpointHealth | null;
45
+ /**
46
+ * 모든 엔드포인트 건강 상태 조회
47
+ */
48
+ getAllEndpointHealth(): EndpointHealth[];
49
+ /**
50
+ * 로드 밸런서 통계 조회
51
+ */
52
+ getStats(): {
53
+ totalEndpoints: number;
54
+ healthyEndpoints: number;
55
+ activeConnections: number;
56
+ circuitBreakersOpen: number;
57
+ averageResponseTime: number;
58
+ };
59
+ /**
60
+ * Round Robin 전략
61
+ */
62
+ private selectRoundRobin;
63
+ /**
64
+ * Least Connections 전략
65
+ */
66
+ private selectLeastConnections;
67
+ /**
68
+ * Weighted 전략
69
+ */
70
+ private selectWeighted;
71
+ /**
72
+ * Random 전략
73
+ */
74
+ private selectRandom;
75
+ /**
76
+ * Half-open Circuit Breaker 엔드포인트 시도
77
+ */
78
+ private tryHalfOpenEndpoint;
79
+ /**
80
+ * Circuit Breaker 상태 업데이트
81
+ */
82
+ private updateCircuitBreaker;
83
+ /**
84
+ * 연결 수 증가
85
+ */
86
+ private incrementConnections;
87
+ /**
88
+ * 연결 수 감소
89
+ */
90
+ private decrementConnections;
91
+ /**
92
+ * 엔드포인트 건강 상태 확인
93
+ */
94
+ private checkEndpointHealth;
95
+ /**
96
+ * 건강 상태 확인 시작
97
+ */
98
+ private startHealthChecks;
99
+ /**
100
+ * 로드 밸런서 종료
101
+ */
102
+ shutdown(): Promise<void>;
103
+ }
104
+ export {};
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Queue Manager
3
+ * 웹훅 작업 큐 관리 시스템
4
+ */
5
+ import { EventEmitter } from "events";
6
+ import type { DispatchJob, QueueConfig } from "./types";
7
+ export declare class QueueManager extends EventEmitter {
8
+ private config;
9
+ private queues;
10
+ private highPriorityQueue;
11
+ private mediumPriorityQueue;
12
+ private lowPriorityQueue;
13
+ private delayedJobs;
14
+ private totalJobs;
15
+ private defaultConfig;
16
+ constructor(config?: Partial<QueueConfig>);
17
+ /**
18
+ * 작업을 큐에 추가
19
+ */
20
+ enqueue(job: DispatchJob): Promise<boolean>;
21
+ /**
22
+ * 우선순위에 따라 작업 추출
23
+ */
24
+ dequeue(): Promise<DispatchJob | null>;
25
+ /**
26
+ * 특정 우선순위 큐에서 작업 추출
27
+ */
28
+ dequeueFromPriority(priority: number): Promise<DispatchJob | null>;
29
+ /**
30
+ * 작업 상태 확인
31
+ */
32
+ peek(): DispatchJob | null;
33
+ /**
34
+ * 특정 작업 제거
35
+ */
36
+ removeJob(jobId: string): Promise<boolean>;
37
+ /**
38
+ * 큐 통계 조회
39
+ */
40
+ getStats(): {
41
+ totalJobs: number;
42
+ highPriorityJobs: number;
43
+ mediumPriorityJobs: number;
44
+ lowPriorityJobs: number;
45
+ delayedJobs: number;
46
+ queueUtilization: number;
47
+ };
48
+ /**
49
+ * 큐 비우기
50
+ */
51
+ clear(): Promise<void>;
52
+ /**
53
+ * 만료된 작업 정리
54
+ */
55
+ cleanupExpiredJobs(): Promise<number>;
56
+ /**
57
+ * 우선순위 숫자를 큐 이름으로 변환
58
+ */
59
+ private getQueueName;
60
+ /**
61
+ * 지연된 작업 스케줄링
62
+ */
63
+ private scheduleDelayedJob;
64
+ /**
65
+ * TTL 정리 작업 시작
66
+ */
67
+ private startTTLCleanup;
68
+ /**
69
+ * 디스크에 큐 상태 저장
70
+ */
71
+ private saveToDisk;
72
+ /**
73
+ * 디스크에서 큐 상태 로드
74
+ */
75
+ private loadFromDisk;
76
+ /**
77
+ * 큐 관리자 종료
78
+ */
79
+ shutdown(): Promise<void>;
80
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Dispatcher Type Definitions
3
+ */
4
+ import type { WebhookEndpoint, WebhookEvent } from "../types/webhook.types";
5
+ export interface DispatchConfig {
6
+ maxConcurrentRequests: number;
7
+ requestTimeoutMs: number;
8
+ defaultRetries: number;
9
+ circuitBreakerThreshold: number;
10
+ circuitBreakerTimeoutMs: number;
11
+ }
12
+ export interface BatchConfig {
13
+ maxBatchSize: number;
14
+ batchTimeoutMs: number;
15
+ maxConcurrentBatches: number;
16
+ enablePrioritization: boolean;
17
+ priorityLevels: number;
18
+ }
19
+ export interface QueueConfig {
20
+ maxQueueSize: number;
21
+ persistToDisk: boolean;
22
+ diskPath?: string;
23
+ compressionEnabled: boolean;
24
+ ttlMs: number;
25
+ }
26
+ export interface LoadBalancerConfig {
27
+ strategy: "round-robin" | "least-connections" | "weighted" | "random";
28
+ healthCheckInterval: number;
29
+ healthCheckTimeoutMs: number;
30
+ weights?: Record<string, number>;
31
+ }
32
+ export interface DispatchJob {
33
+ id: string;
34
+ event: WebhookEvent;
35
+ endpoint: WebhookEndpoint;
36
+ priority: number;
37
+ createdAt: Date;
38
+ scheduledAt: Date;
39
+ attempts: number;
40
+ maxAttempts: number;
41
+ nextRetryAt?: Date;
42
+ }
43
+ export interface CircuitBreakerState {
44
+ endpointId: string;
45
+ state: "closed" | "open" | "half-open";
46
+ failureCount: number;
47
+ lastFailureTime?: Date;
48
+ nextRetryTime?: Date;
49
+ }