Module: Aliyun::ESS::Connection::Management::ClassMethods

Defined in:
lib/aliyun/ess/connection.rb

Overview

Manage the creation and destruction of connections for Aliyun::OSS::Base and its subclasses. Connections are created with establish_connection!.

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Returns true if a connection has been made yet.

Returns:

  • (Boolean)


175
176
177
# File 'lib/aliyun/ess/connection.rb', line 175

def connected?
  !connections.empty?
end

#connectionObject

Returns the connection for the current class, or Base’s default connection if the current class does not have its own connection.

If not connection has been established yet, NoConnectionEstablished will be raised.



166
167
168
169
170
171
172
# File 'lib/aliyun/ess/connection.rb', line 166

def connection
  if connected?
    connections[connection_name] || default_connection
  else
    raise NoConnectionEstablished
  end
end

#disconnect(name = connection_name) ⇒ Object

Removes the connection for the current class. If there is no connection for the current class, the default connection will be removed.



181
182
183
184
185
186
# File 'lib/aliyun/ess/connection.rb', line 181

def disconnect(name = connection_name)
  name       = default_connection unless connections.has_key?(name)
  connection = connections[name]
  connection.http.finish if connection.persistent?
  connections.delete(name)
end

#disconnect!Object

Clears all connections, from all classes, with prejudice.



189
190
191
# File 'lib/aliyun/ess/connection.rb', line 189

def disconnect!
  connections.each_key {|connection| disconnect(connection)}
end

#establish_connection!(options = {}) ⇒ Object

Creates a new connection with which to make requests to the ESS servers for the calling class.

Aliyun::ESS::Base.establish_connection!(:access_key_id => '...', :secret_access_key => '...')

Required arguments

  • :access_key_id - The access key id for your OSS account. Provided by Aliyun.

  • :secret_access_key - The secret access key for your OSS account. Provided by Aliyun.

If any of these required arguments is missing, a MissingAccessKey exception will be raised.

Optional arguments

  • :server - The server to make requests to. You can use this to specify your bucket in the subdomain,

or your own domain’s cname if you are using virtual hosted buckets. Defaults to oss.aliyuncs.com.

  • :port - The port to the requests should be made on. Defaults to 80 or 443 if the :use_ssl

argument is set.

  • :use_ssl - Whether requests should be made over SSL. If set to true, the :port argument

will be implicitly set to 443, unless specified otherwise. Defaults to false.

  • :persistent - Whether to use a persistent connection to the server. Having this on provides around a two fold

performance increase but for long running processes some firewalls may find the long lived connection suspicious and close the connection. If you run into connection errors, try setting :persistent to false. Defaults to false.



155
156
157
158
159
160
# File 'lib/aliyun/ess/connection.rb', line 155

def establish_connection!(options = {})
  # After you've already established the default connection, just specify 
  # the difference for subsequent connections
  options = default_connection.options.merge(options) if connected?
  connections[connection_name] = Connection.connect(options)
end