File tree 3 files changed +15
-3
lines changed
3 files changed +15
-3
lines changed Original file line number Diff line number Diff line change 7
7
8
8
type Consumer struct {
9
9
callback func (* sarama.ConsumerMessage )
10
- ready chan bool
10
+ ready chan bool // 채널이 존재한다면 준비 상태, 채널이 닫혔다면 구동되고 있는 상태
11
11
}
12
12
13
13
func NewConsumer (callback func (message * sarama.ConsumerMessage )) * Consumer {
@@ -45,6 +45,7 @@ func (c *Consumer) Setup(session sarama.ConsumerGroupSession) error {
45
45
46
46
// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited.
47
47
func (c * Consumer ) Cleanup (session sarama.ConsumerGroupSession ) error {
48
+ c .ready = make (chan bool )
48
49
log .Print ("Consumer stopped" )
49
50
return nil
50
51
}
Original file line number Diff line number Diff line change @@ -67,10 +67,10 @@ func (k *ConsumeOperator[Msg]) Init() {
67
67
func (k * ConsumeOperator [Msg ]) Running () bool {
68
68
select {
69
69
case _ , ok := <- k .consumer .ready :
70
- if ! ok { // Channel 닫힘, Ready 됐다는 거
70
+ if ! ok { // Channel 닫힘, 가동 중 (Ready X)
71
71
return true
72
72
}
73
- default : // Channel 안 닫힘, Ready 인 됐다는 거
73
+ default : // Channel 열림, Consume 가능 상태 (Ready)
74
74
return false
75
75
}
76
76
@@ -90,7 +90,17 @@ func (k *ConsumeOperator[Msg]) StartConsume() error {
90
90
ctx , cancel := context .WithCancel (context .Background ())
91
91
k .cancel = & cancel
92
92
93
+ // TODO: Race Condition 발생 가능성 있음 (Consume 직후랑 Running 체크 사이에 상태 변경되어 다중으로 실행될 수 있음)
94
+ if k .Running () {
95
+ return errors .New ("already running" )
96
+ }
97
+
93
98
go func () {
99
+ // NOTE: Ctx Err 시에도 Consumer Cleanup 은 무조건 발생, 아래 라인을 굳이 또 실행할 필요가 없습니다.
100
+ //defer func() {
101
+ // k.consumer.ready = make(chan bool)
102
+ //}()
103
+
94
104
for {
95
105
if err := client .Consume (ctx , []string {k .topic }, k .consumer ); err != nil {
96
106
if errors .Is (err , sarama .ErrClosedConsumerGroup ) {
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ type ConsumeOperator[InMsg any, Msg any] interface {
5
5
Serializer () MessageSerializer [InMsg , Msg ]
6
6
Callback () Callback [Msg ]
7
7
Consume (message InMsg )
8
+ Running () bool
8
9
StartConsume () error
9
10
StopConsume () error
10
11
}
You can’t perform that action at this time.
0 commit comments