dqrb4228 2019-09-13 04:37
浏览 161
已采纳

ElasticClient Olivere软件包与以前部署的ECS集群保持长期连接

We are currently creating an olivere elastic client in our API like so:

        elasticClient, err = elastic.NewClient(
            elastic.SetURL(elasticSearchUrl),
            elastic.SetSniff(false),
        )

        if err != nil {
            logger.ApplicationError(err)
            time.Sleep(3 * time.Second) 
        } else {
            logger.Information("ElasticSearch: connected")
            return elasticClient
        }
    ...

The single instance client is then used for all in-coming API requests.

However, after a new Elastic cluster is redeployed the connection seems to be still pointing to the old instance since that elasticSearchUrl was only used once for the initial connection.

This creates a problem when are new Elastic Cluster is deployed because a new connection is required.

What would be the best way to achieve a re-connect to the new Elastic Search cluster using this library?

  • 写回答

1条回答 默认 最新

  • douna2917 2019-09-17 03:17
    关注

    The reason for this is golang is using long-live connections by default. You can disable it by setting the DisableKeepAlives in Transport.

    var httpClient = &http.Client{
            Timeout: 60 * time.Second,
            Transport: &http.Transport{
                // Disabel long-live connection
                DisableKeepAlives: true,
                // This is to avoid the time_wait in connections
                MaxIdleConnsPerHost: -1,
            },
        }
    
    elasticClient, err = elastic.NewClient(
                elastic.SetURL(elasticSearchUrl),
                elastic.SetSniff(false),
                elastic.SetHttpClient(httpClient),
            )
       ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊