Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

对推送失败的token做列宽容的处理,保护被误杀的tokens

  • Loading branch information...
commit a0c59537155609541cd4ccb1c3a6fbba7aced52e 1 parent af91fa1
@jeffkit authored
Showing with 49 additions and 6 deletions.
  1. +31 −0 apnsagent/constants.py
  2. +18 −6 apnsagent/notification.py
View
31 apnsagent/constants.py
@@ -1,17 +1,48 @@
#encoding=utf-8
+# 开发版文件的目录名
DEVELOP_DIR = 'develop'
+
+# 正式版文件的目录名
PRODUCTION_DIR = 'production'
+
+# 证书文件名
CER_FILE = 'cer.pem'
+
+# 密钥文件名
KEY_FILE = 'key.pem'
+
+# 配置文件名
CONF_FILE = 'conf.ini'
+
+# 推送频道前缀
PUSH_JOB_CHANNEL = 'push_job'
+
+# 推送Fallback前缀
PUSH_JOB_FALLBACK = 'push_job_fallback'
+
+# 开发版推送频道前缀
PUSH_JOB_CHANNEL_DEV = 'push_job_dev'
+
+# 开发版Fallback前缀
PUSH_JOB_FALLBACK_DEV = 'push_job_fallback_dev'
+
+# 开发版的Token集合
DEBUG_TOKENS = 'debug_tokens'
+
+# 非法Token,不对里面的Token进行推送
INVALID_TOKENS = 'invalid_tokens'
+# 推送发生错误的Token及其最后一次错误时间
+FAIL_TOKEN_TIME = 'fail_token_time'
+
+# 推送发生错误的token及其错误次数
+FAIL_TOKEN_COUNT = 'fail_token_time'
+
+# token最大失败次数
+TOKEN_MAX_FAIL_TIME = 5
+
+# Web的用户名密码
USERNAME = 'apns'
PASSWORD = '12345'
View
24 apnsagent/notification.py
@@ -240,6 +240,23 @@ def consume_message(self, channel):
log.debug('i am leaving push')
+ def handle_bad_token(self, token, fail_time):
+ log.debug('push message fail to send to %s.' % token)
+ # 设置token的失败次数及最后更新时间
+ count = self.rds.hincrby('%s:%s' % (constants.FAIL_TOKEN_COUNT,
+ self.app_key), token, 1)
+ if count >= constants.TOKEN_MAX_FAIL_TIME:
+ # 如果token连续失败的次数达到了阀值,放进invalid_tokens
+ self.rds.hdel('%s:%s' % (constants.FAIL_TOKEN_COUNT,
+ self.app_key), token)
+ self.rds.hdel('%s:%s' % (constants.FAIL_TOKEN_TIME,
+ self.app_key), token)
+ self.rds.sadd('%s:%s' % (constants.INVALID_TOKENS,
+ self.app_key), token)
+ else:
+ self.rds.hset('%s:%s' % (constants.FAIL_TOKEN_TIME,
+ self.app_key), token, fail_time)
+
def feedback(self):
"""
从apns获取feedback,处理无效token
@@ -248,12 +265,7 @@ def feedback(self):
try:
self.reconnect()
for (token, fail_time) in self.apns.feedback_server.items():
- log.debug('push message fail to send to %s.' % token)
- # self.client.push(token, enhance=True)
- # time.sleep(0.01)
- self.rds.sadd('%s:%s' % (constants.INVALID_TOKENS,
- self.app_key),
- token)
+ self.handle_bad_token(token, fail_time)
except:
self.log_error('get feedback fail')
time.sleep(60)
Please sign in to comment.
Something went wrong with that request. Please try again.