Skip to content

Commit

Permalink
Mitigate race condition when under load (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFirstAvenger authored Jun 15, 2024
1 parent 790899e commit eade0ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fix issue with changeset form not honoring initial node
- Elixir 1.16 in CI
- Increase formatter line length
- Mitigate race condition in trace_started? function.

## 0.6.0

Expand Down
16 changes: 12 additions & 4 deletions lib/flame_on/capture/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ defmodule FlameOn.Capture.Server do
singleton to track that it has started for a given capture
"""
def trace_started? do
set = ETS.Set.wrap_existing!(__MODULE__)
{:started?, started?} = ETS.Set.get!(set, :started?, {:started?, false})
if !started?, do: ETS.Set.put!(set, {:started?, true})
started?
case ETS.Set.wrap_existing(__MODULE__) do
{:ok, set} ->
{:started?, started?} = ETS.Set.get!(set, :started?, {:started?, false})
if !started?, do: ETS.Set.put!(set, {:started?, true})
started?

# in high traffic instances, the table may have just shut down from a previous run when this is run
# In this case, we don't want to start a new trace, so we return true to skip starting/stopping a new trace
# https://github.com/DockYard/flame_on/issues/41
{:error, :table_not_found} ->
true
end
end

def stop_trace do
Expand Down

0 comments on commit eade0ed

Please sign in to comment.