Skip to content

Commit

Permalink
Do not lock files with flock by default anymore
Browse files Browse the repository at this point in the history
Newer Windows versions (i.e. >= 10.0.14393) apparently follow POSIX more
strictly. With current Windows and Linux versions, we can thus get away
with just writing to the file and depending on the OS to properly
serialize those writes.

On older versions or with some network filesystems, the user might still
have to enable file locking if they are writing to the same log file
from multiple processes.
  • Loading branch information
meineerde committed Jul 10, 2020
1 parent 12aba2e commit 544ae52
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/rackstash/adapter/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module Adapter
# concurrent writes of multiple processes (e.g. multiple worker processes of
# an application server) don't produce interleaved log lines.
#
# When using Windows, we can only guarantee writes up to the underlying
# When using some older versions of Windows (< 10.0.14393) or Linux
# (< 4.2.6), we likely can only guarantee writes up to the underlying
# drive's sector size to be atomic (usually either 512 Bytes or 4 KiByte).
# Larger log lines might be interleaved or partially lost.
#
Expand All @@ -36,10 +37,10 @@ module Adapter
# applies to NFS and most FUSE filesystems like sshfs. However, SMB/CIFS is
# likely safe to use here.
#
# When reading the log file, the reader might still see incomplete writes
# depending on the OS and filesystem. Since we are only writing complete
# lines, it should be safe to continue reading until you observe a newline
# (`\n`) character.
# In any case, when reading the log file, the reader might still see
# incomplete writes depending on the OS and filesystem. Since we are only
# writing complete lines, it should be safe to continue reading until you
# observe a newline (`\n`) character.
#
# Assuming you are creating the log adapter like this
#
Expand Down Expand Up @@ -116,7 +117,7 @@ def self.from_uri(uri)
# @param auto_reopen (see #auto_reopen=)
# @param rotate (see #rotate=)
# @param lock (see #lock=)
def initialize(path, auto_reopen: true, rotate: nil, lock: Gem.win_platform?)
def initialize(path, auto_reopen: true, rotate: nil, lock: false)
@base_path = ::File.expand_path(path).freeze

self.auto_reopen = auto_reopen
Expand All @@ -135,8 +136,9 @@ def auto_reopen?

# @param lock [Boolean] set to `true` to aquire an exclusive write lock
# for each write to the log file. This can ensure more consistent writes
# from multiple processes on some filesystems. We enable this by default
# on Windows only since it can be quite expensive.
# from multiple processes on some filesystems. This might be required
# for older Windows or Linux or with some filesystems not implementing
# strict POSIX compatibility (such as NFS or most FUSE filesystems)
def lock=(lock)
@lock = !!lock
end
Expand Down

0 comments on commit 544ae52

Please sign in to comment.