我有一个API端点,它只执行读取请求。通常,整个操作可以在100ms下完成
[03/Aug/2015:19:35:53 -0700] "GET /query?email=xxx%40aol.com&hash=xxx HTTP/1.1" 200 186 - "myapp-1.0.6o" "xxx.appspot.com" ms=74 cpu_ms=27 cpm_usd=0.000021 instance=00c61b117c55f2b00cdd73904665675ced040765 app_engine_release=1.9.24
但是,我可以看到一些请求并且不常见,最多可以激活20秒!
[03/Aug/2015:18:59:50 -0700] "GET /query?email=yyy%40gmail.com&hash=yyy HTTP/1.1" 200 193 - "myapp-1.0.6o" "xxx.appspot.com" ms=18288 cpu_ms=61 cpm_usd=0.000022 instance=00c61b117c55f2b00cdd73904665675ced040765 app_engine_release=1.9.24
现在,服务器负载仍然很轻。它会在一分钟内收到1-5个请求。我们仍然处于免费配额使用状态。
API python代码非常简单。我认为这不是这种缓慢行动的罪魁祸首。
class User(ndb.Model):
email = ndb.StringProperty(required = True)
timestamp = ndb.DateTimeProperty(required = True)
class QueryHandler(webapp2.RequestHandler):
def get(self):
email = self.request.get('email')
hash = self.request.get('hash')
expected_hash = Utils.hash(email)
result = {
'email' : email,
'user_timestamp' : 0,
'server_timestamp' : 0,
'free_trial_duration' : _FREE_TRIAL_DURATION
}
user_timestamp = 0
if hash == expected_hash:
user_timestamp = memcache.get(email)
if user_timestamp is None:
user = User.get_by_id(email)
if user is not None:
user_timestamp = int(time.mktime(user.timestamp.timetuple()))
result['user_timestamp'] = user_timestamp
memcache.add(email, user_timestamp, _MEMCACHE_DURATION)
else:
result['user_timestamp'] = user_timestamp
else:
logging.debug('QueryHandler, email = ' + email + ', hash = ' + hash + ', expected_hash = ' + expected_hash)
server_timestamp = max(int(time.time()), user_timestamp)
result['server_timestamp'] = server_timestamp
self.response.headers['Content-Type'] = 'application/json'
json_result = json.encode(result)
self.response.out.write(json_result)
logging.debug('QueryHandler, result = ' + json_result)
app = webapp2.WSGIApplication([
('/query', QueryHandler),
], debug=False)
Google App Engine服务器质量似乎更多。有什么我们可以做的,以避免API消费的不合理缓慢?