caddyserver / certmagic

Automatic HTTPS for any Go program: fully-managed TLS certificate issuance and renewal
https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc
Apache License 2.0
5k stars 289 forks source link

Use posix file advisory locks on supported platforms #295

Open elee1766 opened 3 months ago

elee1766 commented 3 months ago

the current storage implementation uses a home brew approach to locking a file for a specific process.

this is a feature built into posix compatible filesystems.

certmagic filestorage should capability detect if posix locks are available, and if they are, use them to protect access to files.

advisory locks have two major weaknesses, that users of sqlite, mdbx, lmdb, are probably well familiar with.

  1. they do not work well on network filesystems (depends on implementation, very flaky regardless)
  2. you cannot use them to lock within a process - you still need mutex in process code to protect access to it.

1 already exists as an issue in the current implementation

2 can easily be dealt with by go's mutex.

as a result, I think that it would be good to incorporate this feature into certmagic storage.

that said, I don't use the filesystem module in production, so I don't really care that much. maybe someone who does would be happy to do this work :)

some resources:

https://gavv.net/articles/file-locks/ https://github.com/juju/fslock

mholt commented 2 months ago

This could be a good idea. As mentioned in Slack, I don't know much about them, so I'm not sure I'd be the right person to implement or review this patch.