Q
Q
Qudres2017-11-24 11:45:15
go
Qudres, 2017-11-24 11:45:15

Go why (it closes by itself) ampq channel closes?

1) There is a queue with one message (for the test)
2) Again, for the test, the message is always returned to the queue Code example
msg.Nack(false, true)

func addSmsSender(ampq <-chan amqp.Delivery, status *string) {
  var me MyStruct
  for true {
    if *status != "Connected" {
      fmt.Printf("Alarm connect\r\n")
      fmt.Print(*status)
      time.Sleep(Constants.VirginTimeout * time.Millisecond)
      continue
    }

    msg, ok := <-ampq
    //если канал закрыт, то ложимся спать
    if !ok {
      fmt.Print("Я сплю\r\n")
      json.Unmarshal([]byte(msg.Body), &me)
      fmt.Printf("%+v\n",&me)
      time.Sleep(Constants.VirginTimeout * time.Millisecond)
      continue
    }
    fmt.Print("Read\r\n")

    err := json.Unmarshal([]byte(msg.Body), &me )
    if err != nil {
      //здесь обработка ошибок распаковки джсона
      fmt.Printf("JSON UNMARSHAL ERR")
      //ок говорим что ошибка в JSON и выкидываем обратно в очередь, мб повезет в след раз заработает
      msg.Nack(false, true)
      continue
    }
.......
           msg.Nack(false, true)

In fact, this is the most buggy code in my life. I note that after the second iteration, the message is not taken from the queue, it just lies there.
result of
Read
Read
I sleep
&{IDTurn:1021727027 IDUser:7410 IDPackageUser:29364}
I sleep
&{IDTurn:1021727027 IDUser:7410 IDPackageUser
:
29364
} UNMARSHAL ERR
If I change for true { .... } to for msg := range ampq { .... }, then the output changes to
Alarm connect
Alarm connect
....
Why does the channel itself close?
additional question, why in the last case I can't read the value by reference?
At the same time, when creating a worker, I add a listener to close the channel.
go func() {
    fmt.Printf("closing: %s", <-w.conn.NotifyClose(make(chan *amqp.Error)))
  }()

But it's not in the output. those. it turns out the channel is closed, but judging by the log it is still open.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question