jieshun-parking 0.6.3 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 318dc7eecf5a1390f6027cff4886d4beb60068a4cdfb220d4b280bd95625e8a6
|
4
|
+
data.tar.gz: 484b8893041d1c8ae4bb7b1880dc437dcaaaa0529f02cea311503fb0c12e87b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6cea4b4d3353a966032dd245e4b2055c5f56f65af528f437f11bd4baab1c60fa4208aec10d64b43db82606ad836d3d8d55bee64a6af8706aff7b9b2db862d5c
|
7
|
+
data.tar.gz: f0dd3715ca33b6639dd3bd2a120ab0686d1f7affd9cd58e01fe01a2a050cac304b442c35480022fdc066b1076ad89e1eb02c2778b191d7bc303f226f1a650793
|
@@ -4,7 +4,8 @@ module Jieshun
|
|
4
4
|
class DataCenterClient
|
5
5
|
|
6
6
|
REDIS_AUTH_KEY = 'jieshun.parking.datacenter.auth.token'
|
7
|
-
|
7
|
+
REDIS_AUTH_KEY_LOCK = 'jieshun.parking.datacenter.auth.token.lock'
|
8
|
+
MAX_RETRY_COUNT = 3 # 定义refresh_token 最大重试次数
|
8
9
|
def initialize(configuration, http_client = StandardHttpClient.new)
|
9
10
|
@configuration = configuration
|
10
11
|
@http_client = http_client
|
@@ -15,17 +16,38 @@ module Jieshun
|
|
15
16
|
@redis_client.redis_get(REDIS_AUTH_KEY)
|
16
17
|
end
|
17
18
|
|
19
|
+
|
18
20
|
def login_if_required
|
19
21
|
auth_token = get_auth_token
|
20
22
|
if Jieshun::Parking.always_login || auth_token.nil? || auth_token.empty?
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@redis_client.
|
26
|
-
|
23
|
+
# 使用 Redis 锁来避免并发刷新
|
24
|
+
lock_key = REDIS_AUTH_KEY_LOCK
|
25
|
+
begin
|
26
|
+
# 设置一个5秒过期的锁
|
27
|
+
if @redis_client.redis_set_nx_lock(lock_key, 5)
|
28
|
+
# 重试机制
|
29
|
+
Retry.with_retries(max_tries: MAX_RETRY_COUNT, sleep_seconds: 1) do
|
30
|
+
refresh_token do |auth_token|
|
31
|
+
yield(auth_token)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
rescue StandardError => e
|
36
|
+
Jieshun::Parking.logger.info("Failed to refresh token;error: #{e.message}")
|
37
|
+
ensure
|
38
|
+
# 确保锁最终被释放
|
39
|
+
@redis_client.redis_del(lock_key)
|
27
40
|
end
|
28
|
-
|
41
|
+
end
|
42
|
+
yield(auth_token)
|
43
|
+
end
|
44
|
+
|
45
|
+
def refresh_token
|
46
|
+
hash = { pno: @configuration.pno, secret: @configuration.secret }
|
47
|
+
@http_client.post_json("#{@configuration.server_url}/api/login", {}, hash) do |result|
|
48
|
+
auth_token = result.body['tn']
|
49
|
+
expire_time = result.body['timeOut'] - 1000
|
50
|
+
@redis_client.redis_set(REDIS_AUTH_KEY, auth_token, expire_time) # token will be expired after 2 hours
|
29
51
|
yield(auth_token)
|
30
52
|
end
|
31
53
|
end
|
@@ -7,6 +7,9 @@ module Jieshun
|
|
7
7
|
require 'uri'
|
8
8
|
require 'json'
|
9
9
|
|
10
|
+
# 设置连接和读取的超时时间(以秒为单位)
|
11
|
+
TIMEOUT = 30 # 例如,30 秒超时
|
12
|
+
|
10
13
|
def post_form(server_url, headers, hash)
|
11
14
|
post(server_url, 'application/x-www-form-urlencoded', headers, hash) do |result|
|
12
15
|
yield(result)
|
@@ -24,7 +27,7 @@ module Jieshun
|
|
24
27
|
req = Net::HTTP::Post.new(uri)
|
25
28
|
headers.each { |k, v| req[k] = v }
|
26
29
|
req["Content-Type"] = content_type
|
27
|
-
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
30
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', read_timeout: TIMEOUT, open_timeout: TIMEOUT) do |http|
|
28
31
|
if Jieshun::Parking.debug_mode && Jieshun::Parking.logger
|
29
32
|
Jieshun::Parking.logger.debug("Jieshun Parking Request, url: #{server_url}, body: #{JSON.pretty_generate(hash)}")
|
30
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jieshun-parking
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LCola
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
|
-
rubygems_version: 3.
|
83
|
+
rubygems_version: 3.2.3
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: 捷顺停车场 API
|