From 544ae52286a0bb4bb8fd14c18374675a55c2e862 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Fri, 10 Jul 2020 20:31:28 +0200 Subject: [PATCH] Do not lock files with flock by default anymore 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. --- lib/rackstash/adapter/file.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/rackstash/adapter/file.rb b/lib/rackstash/adapter/file.rb index 7943d41..152031f 100644 --- a/lib/rackstash/adapter/file.rb +++ b/lib/rackstash/adapter/file.rb @@ -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. # @@ -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 # @@ -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 @@ -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