summaryrefslogtreecommitdiff
path: root/lib/mattermost
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-12-20 20:01:48 +0100
committerKamil Trzcinski <ayufan@ayufan.eu>2016-12-20 20:01:48 +0100
commit2b7d759afdb3ce0327367cb7e719afb288334d39 (patch)
tree1a95c04546f08fe69cbf5cbafac4a1b0825a677e /lib/mattermost
parente9c14918e51828dfbfa2879d5a04f74d08fae290 (diff)
downloadgitlab-ce-2b7d759afdb3ce0327367cb7e719afb288334d39.tar.gz
Add with_lease to session [ci skip]
Diffstat (limited to 'lib/mattermost')
-rw-r--r--lib/mattermost/session.rb40
1 files changed, 32 insertions, 8 deletions
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
index 7a7912d02fc..38a71061097 100644
--- a/lib/mattermost/session.rb
+++ b/lib/mattermost/session.rb
@@ -26,6 +26,8 @@ module Mattermost
include Doorkeeper::Helpers::Controller
include HTTParty
+ LEASE_TIMEOUT = 60
+
base_uri Settings.mattermost.host
attr_accessor :current_resource_owner, :token
@@ -35,14 +37,16 @@ module Mattermost
end
def with_session
- raise NoSessionError unless create
-
- begin
- yield self
- rescue Errno::ECONNREFUSED
- raise NoSessionError
- ensure
- destroy
+ with_lease do
+ raise NoSessionError unless create
+
+ begin
+ yield self
+ rescue Errno::ECONNREFUSED
+ raise NoSessionError
+ ensure
+ destroy
+ end
end
end
@@ -130,5 +134,25 @@ module Mattermost
response.headers['token']
end
end
+
+ def with_lease
+ lease_uuid = lease_try_obtain
+ raise NoSessionError unless lease_uuid
+
+ begin
+ yield
+ ensure
+ Gitlab::ExclusiveLease.cancel(lease_key, lease_uuid)
+ end
+ end
+
+ def lease_key
+ "mattermost:session"
+ end
+
+ def lease_try_obtain
+ lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
+ lease.try_obtain
+ end
end
end