如何在多个Goroutine循环内测量/测量延迟

时间:2019-02-28 13:36:31

标签: go rabbitmq instrumentation

为了在订阅rabbitmq队列时重现错误(404) Reason: "NOT_FOUND - no queue,我使用以下代码同时声明和使用队列:

package main

import (
    "fmt"
    "log"
    "os"
    "sync"
    "time"

    uuid "github.com/satori/go.uuid"
    "github.com/streadway/amqp"
)

func exit1(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

func main() {
    rUSER := "bunny"
    rPASS := "test"
    rHOST := "my-rabbit"
    rPORT := "5672"
    rVHOST := "hole"

    // read from ENV
    if e := os.Getenv("RABBITMQ_USER"); e != "" {
        rUSER = e
    }
    if e := os.Getenv("RABBITMQ_PASS"); e != "" {
        rPASS = e
    }
    if e := os.Getenv("RABBITMQ_HOST"); e != "" {
        rHOST = e
    }
    if e := os.Getenv("RABBITMQ_PORT"); e != "" {
        rPORT = e
    }
    if e := os.Getenv("RABBITMQ_VHOST"); e != "" {
        rVHOST = e
    }

    conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%s/%s",
        rUSER, rPASS, rHOST, rPORT, rVHOST))
    exit1(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    exit1(err, "Failed to open a channel")
    defer ch.Close()

    // buggy part
    args := map[string]interface{}{
        "x-message-ttl": int32(3000),
        "x-expires":     int32(8000), // <-- culprit
    }

    concurrent := 500

    wg := sync.WaitGroup{}
    semaphore := make(chan struct{}, concurrent)

    for i := 0; i < 1000; i++ {
        semaphore <- struct{}{}
        wg.Add(1)
        go func() {
            queueName := fmt.Sprintf("carrot-%s-%s", time.Now().Format("2006-01-02"), uuid.Must(uuid.NewV4()))
            fmt.Printf("Creating queue: %s\n", queueName)
            defer func() {
                <-semaphore
                wg.Done()
            }()
            q, err := ch.QueueDeclare(
                queueName,
                false, // durable
                false, // delete when usused
                false, // exclusive
                false, // no-wait
                args,  // arguments
            )
            exit1(err, "Failed to declare a queue")

            // how to measure here time elapsed between ch.Consume is called

            _, err = ch.Consume(
                q.Name, // queue
                "",     // consumer
                true,   // auto-ack
                false,  // exclusive
                false,  // no-local
                false,  // no-wait
                nil,    // args
            )
            exit1(err, "Failed to register a consumer")
        }()
    }
    wg.Wait()
}

提供更多上下文,代码将正常工作并订阅所有内容 1000在并发客户端< 100较少时排队,但是在添加更多并发客户端时,会出现一种“竞争条件”,并且客户端开始收到错误404,这是因为声明了队列的TTL,在这种情况下为8秒:

 "x-expires":     int32(8000),

一个更好的解决方案是使用互斥队列,否则,在客户端可以使用它之前先删除该队列,但是在此“ buggy”代码中,我想衡量ch.QueueDeclare之间的延迟和ch.Consume

一个客户基本上在做:

  q, err := ch.QueueDeclare(
            queueName,
            false, // durable
            false, // delete when usused
            false, // exclusive
            false, // no-wait
            args,  // arguments
        )

然后:

 _, err = ch.Consume(
          q.Name, // queue
          "",     // consumer
          true,   // auto-ack
          false,  // exclusive
          false,  // no-local
          false,  // no-wait
          nil,    // args
        )

有多个并发客户端时,在执行QueueDeclare之后,似乎延迟了8s秒,之后才调用Consume,因此,错误{{ 1}},但是我可以通过什么方法以及如何适应代码来测量这种延迟呢?

0 个答案:

没有答案