误用.Net Redis客户端工具CSRedisCore,自己挖坑自己填

前导  

  上次Redis MQ分布式改造完成之后, 编排的容器稳定运行了一个多月,昨天突然收到ETL端同事通知,没有采集到解析日志了。

640?wx_fmt=png

 赶紧进服务器看了一下,用于数据接收的receiver容器挂掉了, 尝试docker container start [containerid],  几分钟后该容器再次崩溃。 

Redis连接超限

  docker log [containerid]  查看容器日志; 重点:CSRedis.RedisException: ERR max number of clients reached

640?wx_fmt=png

 日志上显示连接Redis服务器的客户端数量超限,头脑快速思考,目前编排的某容器使用CSRedisCore 对于16个Redis DB实例化了16个客户端,但Redis服务器也不至于这么不经折腾吧。

赶紧进redis.io官网搜集相关资料。

After the client is initialized, Redis checks if we are already at the limit of the number of clients that it is possible to handle simultaneously (this is configured using the maxclients configuration directive, see the next section of this document for further information).

In case it can't accept the current client because the maximum number of clients was already accepted, Redis tries to send an error to the client in order to make it aware of this condition, and closes the connection immediately. The error message will be able to reach the client even if the connection is closed immediately by Redis because the new socket output buffer is usually big enough to contain the error, so the kernel will handle the transmission of the error.

 大致意思是:Redis服务器maxclients配置了客户端连接数, 如果当前连接的客户端超限,Redis会回发一个错误消息给客户端,并迅速关闭客户端连接。

立刻登录Redis服务器查看默认配置,确认当前Redis服务器默认配置是10000。

After the client is initialized, Redis checks if we are already at the limit of the number of clients that it is possible to handle simultaneously (this is configured using the maxclients configuration directive, see the next section of this document for further information).

In case it can't accept the current client because the maximum number of clients was already accepted, Redis tries to send an error to the client in order to make it aware of this condition, and closes the connection immediately. The error message will be able to reach the client even if the connection is closed immediately by Redis because the new socket output buffer is usually big enough to contain the error, so the kernel will handle the transmission of the error.

640?wx_fmt=png 左图表明:通过Redis-Cli 登录进服务器立即就被踢下线。

基本可认定redis客户端使用方式有问题。

CSRedisCore使用方式

 继续查看相关资料,可在redis服务器上利用redis-cli命令:info clients、client list仔细分析客户端连接。

info clients 命令显示现场确实有10000的连接数;


640?wx_fmt=gifclient list命令显示连接如下

640?wx_fmt=png

官方对client list命令输出字段的解释:

  • addr: The client address, that is, the client IP and the remote port number it used to connect with the Redis server.

  • fd: The client socket file descriptor number.

  • name: The client name as set by CLIENT SETNAME.

  • age: The number of seconds the connection existed for.

  • idle: The number of seconds the connection is idle.

  • flags: The kind of client (N means normal client, check the full list of flags).

  • omem: The amount of memory used by the client for the output buffer.

  • cmd: The last executed command.

根据以上解释,表明 Redis服务器收到很多ip=172.16.1.3(故障容器在网桥内的Ip 地址)的客户端连接,这些连接最后发出的是ping命令(这是一个测试命令)

640?wx_fmt=png

故障容器使用的Redis客户端是CSRedisCore,该客户端只是单纯将 Msg 写入Redis list 数据结构,CSRedisCore上相关github issue给了我一些启发。

发现自己将CSRedisClient实例化代码写在 .netcore api Controller构造函数,这样每次请求构造Controller时都 实例化一次Redis客户端,最终Redis客户端不超限才怪。

依赖注入三种模式: 单例(系统内单一实例,一次性注入);瞬态(每次请求产生实例并注入);自定义范围。

有关dotnet apicontroller 以瞬态模式 注入,请查阅链接。

 赶紧将CSRedisCore实例化代码移到 startup.cs 并注册为单例。

大胆求证

info clients命令显示稳定在53个Redis连接。

640?wx_fmt=png

client list命令显示:172.16.1.3(故障容器)建立了50个客户端连接,编排的另一个容器webapp建立了2个连接,redis-cli命令登录到服务器建立了1个连接。

640?wx_fmt=png

那么问题来了,修改之后,receiver容器为什么还稳定建立了50个redis连接?

进一步与CSRedisCore原作者沟通,确定CSRedisCore有预热机制,默认在连接池中预热了50个连接。

bingo,故障和困惑全部排查清楚。

总结

经此一役,在使用CSRedisCore客户端时, 要深入理解

① Stackexchange.Redis 使用的多路复用连接机制(使用时很容易想到注册到单例),CSRedisCore开源库采用连接池机制,在高并发场景下强烈建议注册为单例, 否则在生产使用中可能会误用在瞬态请求中实例化,导致redis客户端几天之后被占满。

② CSRedisCore会默认建立连接池,预热50个连接, 开发者心里要有数。

额外的方法论: 尽量不要从某度找答案,要学会问问题,并尝试从官方、stackoverflow 、github社区寻求解答,你挖过的坑也许别人早就挖过并踏平过。

原文链接:https://www.cnblogs.com/JulianHuang/p/11541658.html


.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313764.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java——类加载机制

** 一、什么是类的加载 ** 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构。类的加载的最终产品是位于堆区中的Class…

.NET中国峰会议题征集

月初做的调查《》,参与人数576人,愿意参与分享.NET Core经验的142人,今天发起分会场主题演讲和闪电演讲议题.2014年微软组织成立.NET基金会,微软在成为主要的开源参与者的道路上又前进了一步。2014年以来已经有众多知名公司加入.N…

E. Almost Sorted(构造,递归)

E. Almost Sorted 我们定义 almost sorted 数组为,ai1≥ai−1a_{i 1} \geq a_i - 1ai1​≥ai​−1,也就是说, 先写几项出来看看: n 1 1 n 2 1 2 2 1 n 3 1 2 3 1 3 2 2 1 3 3 2 1 容易发现一定是,形如x,x−1,x−…

D. Cut and Stick(Codeforces Round #716 (Div. 2))

D. Cut and Stick 给定一个长度为nnn的数组,里面元素为a1,a2,a3,…,an−1,an,(1≤ai≤n)a_1, a_2, a_3, \dots, a_{n- 1}, a_n, (1 \leq a_i \leq n)a1​,a2​,a3​,…,an−1​,an​,(1≤ai​≤n),有mmm次询问,每次给定l,rl, rl,r&#xff0…

一些学习教程资料等你来拿

近期整理自己的云盘中发现近年来私藏了很多学习资料和教程,本着独乐乐不如众乐乐的精神,特将其分享出来供有兴趣的童鞋学习。进入公众号,输入关键词"敏捷"/"agile"/"scrum",即可获得敏捷开发类别的…

Java——编译与反编译

** 一、基础知识 ** 1.1 编程语言 在介绍编译和反编译之前,我们先来简单介绍下编程语言(Programming Language)。编程语言(Programming Language)分为低级语言(Low-level Language)和高级语…

程序员自家种水果,新鲜包邮配送

上次猕猴桃的活动<好多小伙伴&#xff0c;买了一箱尝过后又下单了好几箱。事实证明&#xff0c;品质才是销量的最佳保证。有些粉丝找到我说&#xff0c;自己家也有果园自己种水果&#xff0c;都是当天采摘当天发货的纯天然水果。于是他们给我寄了一些自家种的苹果、香瓜、冬…

2016-2017 ACM-ICPC CHINA-Final(7 / 12)

2016-2017 ACM-ICPC CHINA-Final A. Number Theory Problem&#xff08;规律、签到&#xff09; #include <bits/stdc.h>using namespace std;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdo…

【招聘(广州)】-年薪30W起-自助打印领域业内第一

印萌是一家为高校打印店提供整套“无人自助打印”解决方案的互联网科技公司&#xff0c;已获得数百万元融资&#xff0c;公司正向盈利。目前产品覆盖清华大学、北京大学、中山大学、复旦大学、浙江大学等700多所高校&#xff0c;多达1800间打印店&#xff0c;累计为全国1200万大…

尾递归

1、递归 简单的来说递归就是一个函数直接或间接地调用自身&#xff0c;是为直接或间接递归。一般来说&#xff0c;递归需要有边界条件、递归前进段和递归返回段。当边界条件不满足时&#xff0c;递归前进&#xff1b;当边界条件满足时&#xff0c;递归返回。 用递归需要注意以…

小 Q 与函数求和 1(牛客练习赛 81 E)

小 Q 与函数求和 1 ∑i1n∑j1nϕ(ijgcd⁡(i,j)K)∑i1n∑j1ngcd⁡(i,j)Kϕ(ij)∑i1n∑j1ngcd⁡(i,j)Kϕ(i)ϕ(j)gcd⁡(i,j)ϕ(gcd⁡(i,j))∑i1n∑j1ngcd⁡(i,j)K1ϕ(i)ϕ(j)ϕ(gcd⁡(i,j))∑d1ndK1inv(ϕ(d))∑i1nd∑j1ndϕ(id)ϕ(jd)[gcd⁡(i,j)1]∑d1ndK1inv(phi(d))∑k1ndϕ(…

asp.net core 中使用 signalR(二)

asp.net core 使用 signalR&#xff08;二&#xff09;Intro上次介绍了 asp.net core 中使用 signalR 服务端的开发&#xff0c;这次总结一下web前端如何接入和使用 signalR&#xff0c;本文主要分两部分&#xff0c;一是直接使用 aspnet/signalr 这个微软开发好的 signalR 的客…

Java位运算实现加、减、乘、除、取余(Mark)

先mark&#xff0c;后续再搞 位运算基础&#xff1a; https://blog.csdn.net/goskalrie/article/details/52796360 加减乘除 1&#xff1a;https://blog.csdn.net/zuochao_2013/article/details/79183621 2&#xff1a;https://www.jianshu.com/p/7bba031b11e7 https://ww…

小 Q 与树(dsu on tree + segment tree)牛客练习赛 81 D

小 Q 与树 给定一棵带权的树&#xff0c;每条边的距离都为111&#xff0c;要我们求∑u1n∑v1nmin(au,av)dis(u,v)\sum\limits_{u 1} ^{n} \sum\limits_{v 1} ^{n}min(a_u, a_v)dis(u, v)u1∑n​v1∑n​min(au​,av​)dis(u,v)&#xff0c; min(au,av)dis(u,v)min(au,av)(dep…

多场景抢红包业务引发.NETCore下使用适配器模式实现业务接口分离

事情的起因我们公司现有一块业务叫做抢红包&#xff0c;最初的想法只是实现了一个初代版本&#xff0c;就是给指定的好友单发红包&#xff0c;随着业务的发展&#xff0c;发红包和抢红包的场景也越来越多&#xff0c;目前主要应用的场景有&#xff1a;单聊发红包、群聊发红包、…

设计模式——创建型模型

目录 单例模式&#xff08;singleton&#xff09;构建模式&#xff08;Builder&#xff09;原型模式&#xff08;Prototype&#xff09;工厂方法模式&#xff08;Factory&#xff09;抽象工厂模式&#xff08;Abstract Factory&#xff09; ** 一、5种创建型模型 ** 1.1 单…

Mr. Panda and Kakin(拓展欧几里得 + O(1)快速乘)

Mr. Panda and Kakin 给定n,cn, cn,c&#xff0c;要我们找到nnn是两个相邻质数的乘积&#xff0c;要我们找到xxx&#xff0c;满足x2303≡c(modn)x ^{2 ^{30} 3} \equiv c \pmod nx2303≡c(modn)&#xff0c;1010≤n≤1018,0<c<n10 ^{10} \leq n \leq 10 ^ {18}, 0 <…

【C】KoobooJson在asp.net core中的使用

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 BY-SA 版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/j_teng/article/details/100924973“KoobooJson是一款体积小巧没有任何依赖且性能表现强劲的Json工具…

单例模式的创建方式

说明&#xff1a; 以下均为线程安全的实现方式。 懒汉、饿汉的非线程安全的意义不大&#xff0c;未实现。 1. 饿汉模式 类初始化时直接创建对象 public class Singleton {private static Singleton singleton new Singleton();//饿汉模式&#xff0c;类初始化时直接创建pub…

P3899 [湖南集训]谈笑风生(线段树合并)

P3899 [湖南集训]谈笑风生 给定一颗以111号节点为根的树&#xff0c;如果a≠ba \neq ba​b&#xff0c;且aaa是bbb的祖先&#xff0c;则aaa比bbb更厉害&#xff0c;如果a≠ba \neq ba​b&#xff0c;且dis(a,b)≤xdis(a, b) \leq xdis(a,b)≤x&#xff0c;xxx为给定的一个…