|
| 1 | +package xcode |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/bitrise-io/go-utils/v2/log" |
| 12 | + "github.com/gofrs/flock" |
| 13 | + |
| 14 | + "github.com/bitrise-io/bitrise-build-cache-cli/v3/internal/config/xcelerate" |
| 15 | + "github.com/bitrise-io/bitrise-build-cache-cli/v3/internal/paths" |
| 16 | + "github.com/bitrise-io/bitrise-build-cache-cli/v3/internal/utils" |
| 17 | +) |
| 18 | + |
| 19 | +// ErrProxyAlreadyRunning means another process holds the proxy lock. |
| 20 | +var ErrProxyAlreadyRunning = errors.New("xcelerate proxy already running") |
| 21 | + |
| 22 | +// The file is both the lock and the pid advertisement: exclusion comes from the |
| 23 | +// kernel lock, while stop-proxy and the xcodebuild wrapper read the pid out of it. |
| 24 | +// It is never removed — unlinking a flock file lets two processes each believe they |
| 25 | +// hold it. |
| 26 | +func proxyPidFile(osProxy utils.OsProxy) string { |
| 27 | + return xcelerate.PathFor(osProxy, paths.ProxyPidFileName) |
| 28 | +} |
| 29 | + |
| 30 | +// proxyOwner reports whether a proxy is serving, and the pid it advertised. |
| 31 | +// |
| 32 | +// The lock is the authority; the pid is only for the message. Deciding on the pid |
| 33 | +// first would report "not running" whenever the advertisement happens to be |
| 34 | +// mid-write — WriteFile truncates before it fills — and that answer starts a |
| 35 | +// second proxy. |
| 36 | +func proxyOwner(osProxy utils.OsProxy) (int, bool) { |
| 37 | + path := proxyPidFile(osProxy) |
| 38 | + |
| 39 | + // Probing would create the file, so an absent one is answered without one. |
| 40 | + content, exists, err := osProxy.ReadFileIfExists(path) |
| 41 | + if err != nil || !exists { |
| 42 | + return 0, false |
| 43 | + } |
| 44 | + |
| 45 | + probe := flock.New(path) |
| 46 | + free, err := probe.TryLock() |
| 47 | + if err != nil { |
| 48 | + return 0, false |
| 49 | + } |
| 50 | + if free { |
| 51 | + _ = probe.Unlock() |
| 52 | + |
| 53 | + return 0, false |
| 54 | + } |
| 55 | + |
| 56 | + // Held, so a pid we cannot parse means "running, identity unknown". |
| 57 | + pid, err := strconv.Atoi(strings.TrimSpace(content)) |
| 58 | + if err != nil || pid <= 0 { |
| 59 | + return 0, true |
| 60 | + } |
| 61 | + |
| 62 | + return pid, true |
| 63 | +} |
| 64 | + |
| 65 | +// withProxySingleton runs serve as the only proxy on this machine. Contention is |
| 66 | +// not a failure: another proxy is already serving, so this one has nothing to do |
| 67 | +// and says so rather than erroring. |
| 68 | +// |
| 69 | +// The only way to take the lock, so the policy cannot be bypassed by a future |
| 70 | +// caller claiming it and deciding for itself. |
| 71 | +func withProxySingleton(osProxy utils.OsProxy, logger log.Logger, serve func() error) error { |
| 72 | + path := proxyPidFile(osProxy) |
| 73 | + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 74 | + return fmt.Errorf("create proxy pid dir: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + lock := flock.New(path) |
| 78 | + locked, err := lock.TryLock() |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("lock %s: %w", path, err) |
| 81 | + } |
| 82 | + if !locked { |
| 83 | + pid, _ := proxyOwner(osProxy) |
| 84 | + logger.Infof("Skipping proxy startup: %s (pid: %d)", ErrProxyAlreadyRunning, pid) |
| 85 | + |
| 86 | + return nil |
| 87 | + } |
| 88 | + defer func() { |
| 89 | + if err := lock.Unlock(); err != nil { |
| 90 | + logger.Warnf("Failed to release proxy lock: %s", err) |
| 91 | + } |
| 92 | + }() |
| 93 | + |
| 94 | + // Advertised after the lock is held, so a reader never sees a pid that does not |
| 95 | + // own the proxy. |
| 96 | + if err := osProxy.WriteFile(path, []byte(strconv.Itoa(os.Getpid())), 0o644); err != nil { |
| 97 | + return fmt.Errorf("advertise proxy pid: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + return serve() |
| 101 | +} |
0 commit comments