-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathapp_router.py
More file actions
70 lines (59 loc) · 2.25 KB
/
Copy pathapp_router.py
File metadata and controls
70 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import sys
import time
import threading
import requests
class AppRouter(object):
def __init__(self, app_id, region):
self.app_id = app_id
self.region = region
self.hosts = {}
self.session = requests.Session()
self.lock = threading.Lock()
self.expired_at = 0
prefix = app_id[:8].lower()
is_cn_n1 = False
if region == "US":
domain = "lncldglobal.com"
elif region == "CN":
if app_id.endswith("-9Nh9j0Va"):
domain = "lncldapi.com"
elif app_id.endswith("-MdYXbMMI"):
domain = "lncldglobal.com"
else:
domain = "{}.lc-cn-n1-shared.com".format(prefix)
is_cn_n1 = True
else:
raise RuntimeError("invalid region: {}".format(region))
if is_cn_n1:
self.hosts.update(dict.fromkeys(
["api", "engine", "stats", "push"], domain))
else:
self.hosts["api"] = "{}.api.{}".format(prefix, domain)
self.hosts["engine"] = "{}.engine.{}".format(prefix, domain)
self.hosts["stats"] = "{}.stats.{}".format(prefix, domain)
self.hosts["push"] = "{}.push.{}".format(prefix, domain)
def get(self, type_):
with self.lock:
if time.time() > self.expired_at:
self.expired_at += 600
threading.Thread(target=self.refresh).start()
return self.hosts[type_]
def refresh(self):
url = "https://app-router.com/2/route?appId={}".format(self.app_id)
try:
result = self.session.get(url, timeout=5).json()
with self.lock:
self.update(result)
except Exception as e:
print("refresh app router failed:", e, file=sys.stderr)
def update(self, content):
self.hosts["api"] = content["api_server"]
self.hosts["engine"] = content["engine_server"]
self.hosts["stats"] = content["stats_server"]
self.hosts["push"] = content["push_server"]
self.expired_at = time.time() + content["ttl"]