From 1d34696de2fb46133e1d969d8d2aed1de3da2ff2 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Thu, 12 Jan 2017 13:25:49 -0500 Subject: [PATCH] 9831: Avoid attempting the same name twice, even if an attempt takes less than 1ms. --- .../api/app/controllers/application_controller.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb index 37e100add3..cdfbdbbcc1 100644 --- a/services/api/app/controllers/application_controller.rb +++ b/services/api/app/controllers/application_controller.rb @@ -129,7 +129,16 @@ class ApplicationController < ActionController::Base raise unless /^Key \(owner_uuid, name\)=\([a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}, .*?\) already exists\./.match detail @object.uuid = nil - @object.name = "#{name_stem} (#{db_current_time.utc.iso8601(3)})" + + new_name = "#{name_stem} (#{db_current_time.utc.iso8601(3)})" + if new_name == @object.name + # If the database is fast enough to do two attempts in the + # same millisecond, we need to wait to ensure we try a + # different timestamp on each attempt. + sleep 0.002 + new_name = "#{name_stem} (#{db_current_time.utc.iso8601(3)})" + end + @object.name = new_name retry end show -- 2.30.2