最近客服有报无法上报运动记录,通过日志查看是分布式锁等待超时所致。
redis出现一个分布式锁的TTL为-1,正常情况都会设置超时时间的。

通过k8s发现sport服务在50几天内重启了40几次,机器上内存比较紧缺,暂时只能重启,占用内存高的问题也先不解决。
看下之前加锁的代码:
def acquire_lock(redis_client, lock_key, lock_value, expire=10, timeout=5):
"""
获取锁
:param redis_client: redis;连接
:param lock_key: lock key
:param lock_value: lock value
:param expire: lock key过期时间
:param timeout: 等待超时
:return:
"""
assert isinstance(redis_client, RedisClient), [type(RedisClient), RedisClient]
wait_time = 0
sleep_time = 0.05
with redis_client.ctx_conn as conn:
while True:
stime = time.time()
if conn.execute('SETNX', lock_key, lock_value):
conn.execute('EXPIRE', lock_key, expire)
return True
elif not conn.execute('TTL', lock_key):
conn.execute('EXPIRE', lock_key, expire)
logger.info("acquire_lock :%s" % lock_key)
gevent.sleep(sleep_time)
etime = time.time()
wait_time += etime - stime
if wait_time >= timeout:
break
assert 0, [conn, lock_key, lock_value, expire, timeout]
从现状来分析,应该是SETNX之后没有执行EXPIRE,正常执行肯定没有问题;
我估计是因为进程刚好执行到SETNX的时候被k8s杀了,导致EXPIRE没有去执行
研究了一下redis的set命令,果然有这个功能
SET', lock_key, lock_value, NX, EX, expire
判断TTL 是-1的时候:
ttl当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key 的剩余生存时间
def acquire_lock(redis_client, lock_key, lock_value, expire=10, timeout=5):
"""
获取锁
:param redis_client: redis;连接
:param lock_key: lock key
:param lock_value: lock value
:param expire: lock key过期时间
:param timeout: 等待超时
:return:
"""
assert isinstance(redis_client, RedisClient), [type(RedisClient), RedisClient]
wait_time = 0
sleep_time = 0.05
logger.info("acquire_lock lock_key:%s lock_value:%s" % (lock_key, lock_value))
with redis_client.ctx_conn as conn:
while True:
stime = time.time()
if conn.execute('SET', lock_key, lock_value, "NX", "EX", expire):
return True
elif conn.execute('TTL', lock_key) == -1:
conn.execute('EXPIRE', lock_key, expire)
gevent.sleep(sleep_time)
etime = time.time()
wait_time += etime - stime
if wait_time >= timeout:
break
assert 0, [conn, lock_key, lock_value, expire, timeout]
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub