登录
首页 >  Golang >  Go问答

DynamoDB 花费太多时间来响应单个项目

来源:stackoverflow

时间:2024-04-29 13:55:38 313浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《DynamoDB 花费太多时间来响应单个项目》,涉及到,有需要的可以收藏一下

问题内容

我有一个 go rest 服务,它连接到 dynamodb 以检索一些产品。该表包含大约 100.000 个产品,我通常检索大约 20.0000 个产品(然后在实际回复之前被其他进程过滤掉,但这与我的问题无关)。

我看到的问题是我的整个请求大约需要 4 秒,这对于我的目标来说非常高。我开始记录应用程序中的每一层,直到最终到达最小的部分,即从数据库中单独检索一个项目。

令我惊讶的是,每个项目检索大约需要 3.6 秒??!!.

无论下面的代码如何,导致这种情况的原因可能是什么?我在发电机配置上缺少什么吗?

func (db dynamorepository) fetchproducts(c *gin.context, pid string) ([]models.product, error) {
    output := make([]models.product, 0)
    chproduct := make(chan *models.product)

    sem := make(chan struct{}, 40000)
    wait := sync.waitgroup{}
    wait.add(1)
    go func (wg *sync.waitgroup) {
        defer wg.done()
        for i := 0; i < len(itemids); i++ {
            p := <-chproduct
            if p != nil {
                output = append(output, *p)
            }
        }
        close(chproduct)
    }(&wait)
    for index := range itemids {
        sem <- struct{}{}
        go func(ix int, s chan struct{}, ch chan *models.product) {
            defer func() {
               <- s
            }()
            infoslice := strings.split(itemids[ix], "_")
            if len(infoslice) != 2 {
                ch <- nil
                return
            }
            key := map[string]*dynamodb.attributevalue{
                "pk": {
                    n: aws.string(pid),
                },
                "sk": {
                    s: aws.string("product"),
                },
            }
            start := time.now()
            result, err := db.db.getbykey(c, db.config.aws.table, key)
            fmt.printf("item %s took %d\n", productid, time.since(start).milliseconds())
            if err != nil {
                ch <- nil
                return
            }
            if result.item == nil {
                ch <- nil
                return
            }
            var product models.product
            err = dynamodbattribute.unmarshalmap(result.item, &product)
            if err != nil {
                ch <- nil
                return
            }
            ch <- &product
        }(index, sem, chproduct)
    }
    wait.wait()

    if len(output) == 0 {
        return nil, nil
    }
    return output, nil
}

getbykey的实现:

func (provider *DynamoDBProvider) GetByKey(ctx context.Context, tableName string, key map[string]*dynamodb.AttributeValue) (*dynamodb.GetItemOutput, error) {
    input := &dynamodb.GetItemInput{
        TableName: aws.String(tableName),
        Key:       key,
    }

    result, err := provider.client.GetItem(input)

    return result, err
}

解决方案


这就是我最终所做的,它极大地改善了延迟。我按照本指南创建了自定义 HTTP 客户端:

https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/custom-http.html

延迟从近 4 秒变为 500 毫秒~。为什么?我不知道,但它有效。

本篇关于《DynamoDB 花费太多时间来响应单个项目》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>