@@ -29,34 +29,34 @@ func (conn redisConnector) consumeMessage(ctx context.Context) {
29
29
"KEDA-Source-Name" : {conn .connectordata .SourceName },
30
30
}
31
31
32
- var messages []string
33
- var listItr int64
34
32
forever := make (chan bool )
35
33
36
34
go func () {
37
- listLength , err := conn .rdbConnection .LLen (ctx , conn .connectordata .Topic ).Result ()
38
- if err != nil {
39
- conn .logger .Fatal ("Error in consuming queue: " , zap .Error (err ))
40
- }
41
- for listItr = 0 ; listItr < listLength ; listItr ++ {
42
- msg , err := conn .rdbConnection .LPop (ctx , conn .connectordata .Topic ).Result ()
35
+ for {
36
+ // BLPop will block and wait for a new message if the list is empty
37
+ msg , err := conn .rdbConnection .BLPop (ctx , 0 , conn .connectordata .Topic ).Result ()
43
38
if err != nil {
44
39
conn .logger .Fatal ("Error in consuming queue: " , zap .Error ((err )))
45
40
}
46
- messages = append (messages , msg )
47
- }
48
- for _ , message := range messages {
49
- response , err := common .HandleHTTPRequest (message , headers , conn .connectordata , conn .logger )
50
- if err != nil {
51
- conn .errorHandler (ctx , err )
52
- } else {
53
- defer response .Body .Close ()
54
- body , err := io .ReadAll (response .Body )
41
+
42
+ if len (msg ) > 1 {
43
+ // BLPop returns a slice with topic and message, we need the second item
44
+ message := msg [1 ]
45
+ response , err := common .HandleHTTPRequest (message , headers , conn .connectordata , conn .logger )
55
46
if err != nil {
56
47
conn .errorHandler (ctx , err )
57
48
} else {
58
- if success := conn .responseHandler (ctx , string (body )); success {
59
- conn .logger .Info ("Message sending to response successful" )
49
+ body , err := io .ReadAll (response .Body )
50
+ if err != nil {
51
+ conn .errorHandler (ctx , err )
52
+ } else {
53
+ if success := conn .responseHandler (ctx , string (body )); success {
54
+ conn .logger .Info ("Message sending to response successful" )
55
+ }
56
+ }
57
+ err = response .Body .Close ()
58
+ if err != nil {
59
+ conn .logger .Error (err .Error ())
60
60
}
61
61
}
62
62
}
0 commit comments