Skip to content

Commit 60b1bb3

Browse files
committed
keep Time class untouched
1 parent 3120cf0 commit 60b1bb3

File tree

10 files changed

+19
-15
lines changed

10 files changed

+19
-15
lines changed

examples/producer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
)
1616
)
1717

18-
p_msg = PulsarSdk::Producer::Message.new("dang qian shi jian #{Time.now}")
18+
p_msg = PulsarSdk::Producer::Message.new("dang qian shi jian #{TimeX.now}")
1919

2020
# ++======消息发送后等待系统响应======++
2121
producer.execute(base_cmd, p_msg)
2222

2323
# ++======调用后立即返回,服务器可能还没收到消息======++
2424
producer.execute_async(base_cmd, p_msg)
2525

26-
# ++======发送消息后需要获取消息回执,因为回执与producer_id和request_id有关必须要知道真实的producer才能获取到回执======++
26+
# ++======发送消息后需要获取消息回执,因为回执与producer_id和request_id有关必须要知道真实的producer才能获取到======++
2727
real_producer = producer.real_producer(p_msg)
2828
real_producer.execute(base_cmd, p_msg)
2929
real_producer.receipt
@@ -39,7 +39,7 @@
3939
)
4040
)
4141
# message will available in 10 second
42-
deliver_at = Time.now + 10
42+
deliver_at = TimeX.now + 10
4343
p_msg = PulsarSdk::Producer::Message.new(
4444
"dang qian shi jian publush at: #{now}, performat at: #{deliver_at}",
4545
Pulsar::Proto::MessageMetadata.new(deliver_at_time: deliver_at.timestamp)

lib/pulsar_sdk/connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(proxy_addr, broker_addr = nil, tls_options = nil, auth_provider =
1818
@tls_options = tls_options&.dup
1919
@auth_provider = auth_provider
2020
@socket = nil
21-
@last_data_received_at = Time.now
21+
@last_data_received_at = TimeX.now
2222
@state = Status.new
2323
self.operation_timeout = 30
2424
self.connection_timeout = 5
@@ -247,7 +247,7 @@ def handle_send_receipt(send_receipt)
247247
end
248248

249249
def set_last_data_received
250-
@last_data_received_at = Time.now
250+
@last_data_received_at = TimeX.now
251251
end
252252

253253
def handle_ping

lib/pulsar_sdk/producer/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(msg, metadata = nil)
88

99
@metadata ||= Pulsar::Proto::MessageMetadata.new
1010
publish_time = @metadata.publish_time
11-
@metadata.publish_time = publish_time.zero? ? Time.now.timestamp : publish_time
11+
@metadata.publish_time = publish_time.zero? ? TimeX.now.timestamp : publish_time
1212
end
1313

1414
def producer_name=(v)

lib/pulsar_sdk/producer/router.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def route(key, total, delay = 0)
1717

1818
return (@handler.call(key) % total) unless key.to_s.empty?
1919

20-
Murmur3.int32_hash(Time.now.timestamp) % total
20+
Murmur3.int32_hash(TimeX.now.timestamp) % total
2121
end
2222

2323
# 将hash值限制在32位内,防止key过长导致过多内存占用

lib/pulsar_sdk/protocol/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Message
1414
define_method "#{x}_at" do
1515
v = self.public_send("#{x}_time").to_i
1616
return if v.zero?
17-
Time.at_timestamp(v)
17+
TimeX.at_timestamp(v)
1818
end
1919
end
2020

lib/pulsar_sdk/tweaks.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66

77
# 扩展type的判断方法,方便书写,统一以 typeof_ 开头
88
Pulsar::Proto::BaseCommand.prepend PulsarSdk::Tweaks::BaseCommand
9-
Time.prepend PulsarSdk::Tweaks::TimeAtMicrosecond

lib/pulsar_sdk/tweaks/time_at_microsecond.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ def timestamp
1414
module ClassMethods
1515
def at_timestamp(v)
1616
second, micro = v.divmod(1000)
17-
Time.at(second, micro * 1000)
17+
self.at(second, micro * 1000)
1818
end
1919
end
2020
end
2121
end
2222
end
23+
24+
# 扩展默认时间方法,增加毫秒时间戳相关处理
25+
class TimeX < Time
26+
prepend PulsarSdk::Tweaks::TimeAtMicrosecond
27+
end

lib/pulsar_sdk/tweaks/timeout_queue.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def pop(timeout = nil)
2828
@received.wait(@mutex)
2929
end
3030
elsif @receive_queue.empty? && timeout != 0
31-
timeout_at = Time.now.to_f + timeout
32-
while @receive_queue.empty? && (res = timeout_at - Time.now.to_f) > 0
31+
timeout_at = TimeX.now.to_f + timeout
32+
while @receive_queue.empty? && (res = timeout_at - TimeX.now.to_f) > 0
3333
@received.wait(@mutex, res)
3434
end
3535
end

lib/pulsar_sdk/tweaks/wait_map.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def delete(id, timeout = nil)
4545
signal.wait(mutex)
4646
end
4747
elsif @responses.empty? && timeout != 0
48-
timeout_at = Time.now.to_f + timeout
49-
while @responses.empty? && (res = timeout_at - Time.now.to_f) > 0
48+
timeout_at = TimeX.now.to_f + timeout
49+
while @responses.empty? && (res = timeout_at - TimeX.now.to_f) > 0
5050
signal.wait(mutex, res)
5151
end
5252
end

lib/pulsar_sdk/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PulsarSdk
2-
VERSION = "0.3.2"
2+
VERSION = "0.4.1"
33
end

0 commit comments

Comments
 (0)