Skip to content

Commit

Permalink
Added debug logging into seprate utils function
Browse files Browse the repository at this point in the history
  • Loading branch information
ragul28 committed Nov 17, 2020
1 parent c25b6e2 commit a5dcd53
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ svault/
!LICENSE

# Ignore files
checklist.md
checklist.md
Links.MD
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ run:
go build && ./rabbitmq-benchmark

run_p:
go build && ./rabbitmq-benchmark -t 1 -r producer -f 1000
go build && ./rabbitmq-benchmark -t 2 -r producer -debug -f 1000

run_c:
go build && ./rabbitmq-benchmark -t 1 -r consumer -debug
go build && ./rabbitmq-benchmark -t 5 -r consumer -debug

start_mq:
docker-compose up -d
Expand Down
9 changes: 4 additions & 5 deletions queue/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ func ConsumerMQ(cfg utils.ConfigStore) {
for {
select {
case <-notify:
fmt.Println("Detects connection failuer, retring..")
log.Println("Detects connection failuer, retrying..")
ch, q, notify, msgs = failuerRetry(cfg)
case d = <-msgs:
if cfg.EnableDebug {
log.Printf("consumer message: %s\n", d.Body)
}
utils.DebugLogging(fmt.Sprintf("Consumer message:%s\n", d.Body), cfg.EnableDebug)
// Manual ack for consumed messages
d.Ack(false)
}
Expand All @@ -70,8 +68,9 @@ func failuerRetry(cfg utils.ConfigStore) (*amqp.Channel, amqp.Queue, chan *amqp.
if err != nil {
log.Println("Sleep 15 sec before retrying the publish")
time.Sleep(15 * time.Second)

} else {
fmt.Println("Reconnection is successful.")
log.Println("Reconnection is successful.")
msgs, _ := consumer(ch, q, cfg.EnableQuorum)
return ch, q, notify, msgs
}
Expand Down
8 changes: 5 additions & 3 deletions queue/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// publisher Publish messages
func publisher(message string, ch *amqp.Channel, q amqp.Queue) error {
func publisher(message string, ch *amqp.Channel, q amqp.Queue, endebug bool) error {

err := ch.Publish(
"", // exchange
Expand All @@ -27,21 +27,23 @@ func publisher(message string, ch *amqp.Channel, q amqp.Queue) error {
return err
}

fmt.Println("published message: " + message)
utils.DebugLogging(fmt.Sprintf("published message: %s\n", message), endebug)

return nil
}

// PublishMQ worker func
func PublishMQ(cfg utils.ConfigStore) {

ch, q, _, err := InitRabbitMQ(cfg.RabbitURL, cfg.QueueName, cfg.EnableQuorum)
if err != nil {
log.Fatalf("%s: %s", "Failed to publish a message", err)
}

for {
if (ch != nil && q != amqp.Queue{}) {
rand.Seed(time.Now().UnixNano())
err = publisher(utils.RandString(cfg.MsgSize), ch, q)
err = publisher(utils.RandString(cfg.MsgSize), ch, q, cfg.EnableDebug)
time.Sleep(time.Duration(cfg.TimeFrequencyMS) * time.Millisecond)
}

Expand Down
10 changes: 10 additions & 0 deletions utils/logdebug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "log"

// DebugLogging helper func to log debug
func DebugLogging(logstring string, enableDebug bool) {
if enableDebug {
log.Printf(logstring)
}
}

0 comments on commit a5dcd53

Please sign in to comment.