Class: GlobalUid::Server
- Inherits:
-
Object
- Object
- GlobalUid::Server
- Defined in:
- lib/global_uid/server.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #allocate(klass, count: 1) ⇒ Object
- #connect ⇒ Object
- #create_uid_table!(name:, uid_type: nil, start_id: nil) ⇒ Object
- #disconnect! ⇒ Object
- #disconnected? ⇒ Boolean
- #drop_uid_table!(name:) ⇒ Object
-
#initialize(name, increment_by:, connection_retry:, connection_timeout:, query_timeout:) ⇒ Server
constructor
A new instance of Server.
- #update_retry_at(seconds) ⇒ Object
Constructor Details
#initialize(name, increment_by:, connection_retry:, connection_timeout:, query_timeout:) ⇒ Server
Returns a new instance of Server.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/global_uid/server.rb', line 6 def initialize(name, increment_by:, connection_retry:, connection_timeout:, query_timeout:) @connection = nil @name = name @retry_at = nil @allocators = {} @increment_by = increment_by @connection_retry = connection_retry @connection_timeout = connection_timeout @query_timeout = query_timeout end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/global_uid/server.rb', line 4 def connection @connection end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/global_uid/server.rb', line 4 def name @name end |
Instance Method Details
#active? ⇒ Boolean
31 32 33 |
# File 'lib/global_uid/server.rb', line 31 def active? !disconnected? end |
#allocate(klass, count: 1) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/global_uid/server.rb', line 67 def allocate(klass, count: 1) # TODO: Replace Timeout.timeout with DB level timeout # Timeout.timeout is unpredictable Timeout.timeout(query_timeout, TimeoutException) do if count == 1 allocator(klass).allocate_one else allocator(klass).allocate_many(count: count) end end end |
#connect ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/global_uid/server.rb', line 17 def connect return @connection if active? || !retry_connection? @connection = mysql2_connection(name) begin validate_connection_increment if active? rescue InvalidIncrementException => e GlobalUid.configuration.notifier.call(e) disconnect! end @connection end |
#create_uid_table!(name:, uid_type: nil, start_id: nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/global_uid/server.rb', line 48 def create_uid_table!(name:, uid_type: nil, start_id: nil) uid_type ||= "bigint(21) UNSIGNED" start_id ||= 1 connection.execute("CREATE TABLE IF NOT EXISTS `#{name}` ( `id` #{uid_type} NOT NULL AUTO_INCREMENT, `stub` char(1) NOT NULL DEFAULT '', PRIMARY KEY (`id`), UNIQUE KEY `stub` (`stub`) ) ENGINE=#{GlobalUid.configuration.storage_engine}") # prime the pump on each server connection.execute("INSERT IGNORE INTO `#{name}` VALUES(#{start_id}, 'a')") end |
#disconnect! ⇒ Object
43 44 45 46 |
# File 'lib/global_uid/server.rb', line 43 def disconnect! @connection = nil @allocators = {} end |
#disconnected? ⇒ Boolean
35 36 37 |
# File 'lib/global_uid/server.rb', line 35 def disconnected? @connection.nil? end |
#drop_uid_table!(name:) ⇒ Object
63 64 65 |
# File 'lib/global_uid/server.rb', line 63 def drop_uid_table!(name:) connection.execute("DROP TABLE IF EXISTS `#{name}`") end |
#update_retry_at(seconds) ⇒ Object
39 40 41 |
# File 'lib/global_uid/server.rb', line 39 def update_retry_at(seconds) @retry_at = Time.now + seconds end |