<commit_before>#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

import fd
import Nest
import Inquiline


public final class SynchronousWorker : WorkerType {
  let configuration: Configuration
  let logger: Logger
  let listeners: [Socket]

  var timeout: Int {
    return configuration.timeout / 2
  }

  let notify: (Void) -> Void

  let application: (RequestType) -> ResponseType
  var isAlive: Bool = false
  let parentPid: pid_t

  public init(configuration: Configuration, logger: Logger, listeners: [Listener], notify: @escaping (Void) -> Void, application: @escaping Application) {
    self.parentPid = getpid()
    self.logger = logger
    self.listeners = listeners.map { Socket(descriptor: $0.fileNumber) }
    self.configuration = configuration
    self.notify = notify
    self.application = application
  }

  func registerSignals() throws {
    let signals = try SignalHandler()
    signals.register(.interrupt, handleQuit)
    signals.register(.quit, handleQuit)
    signals.register(.terminate, handleTerminate)
    sharedHandler = signals
    SignalHandler.registerSignals()
  }

  public func run() {
    logger.info("Booting worker process with pid: \(getpid())")

    do {
      try registerSignals()
    } catch {
      logger.info("Failed to boot \(error)")
      return
    }
    isAlive = true

    listeners.forEach { $0.blocking = false }

    if listeners.count == 1 {
      runOne(listeners.first!)
    } else {
      runMultiple(listeners)
    }
  }

  func isParentAlive() -> Bool {
    if getppid() != parentPid {
      logger.info("Parent changed, shutting down")
      return false
    }

    return true
  }

  func runOne(_ listener: Socket) {
    while isAlive {
      sharedHandler?.process()
      notify()
      accept(listener)

      if !isParentAlive() {
        return
      }

      wait()
    }
  }

  func runMultiple(_ listeners: [Socket]) {
    while isAlive {
      sharedHandler?.process()
      notify()

      let sockets = wait().filter {
        $0.fileNumber != sharedHandler!.pipe.reader.fileNumber
      }

      sockets.forEach(accept)

      if !isParentAlive() {
        return
      }
    }
  }

  // MARK: Signal Handling

  func handleQuit() {
    isAlive = false
  }

  func handleTerminate() {<commit_msg>Remove `result of call is unused` warnings<commit_after>#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

import fd
import Nest
import Inquiline


public final class SynchronousWorker : WorkerType {
  let configuration: Configuration
  let logger: Logger
  let listeners: [Socket]

  var timeout: Int {
    return configuration.timeout / 2
  }

  let notify: (Void) -> Void

  let application: (RequestType) -> ResponseType
  var isAlive: Bool = false
  let parentPid: pid_t

  public init(configuration: Configuration, logger: Logger, listeners: [Listener], notify: @escaping (Void) -> Void, application: @escaping Application) {
    self.parentPid = getpid()
    self.logger = logger
    self.listeners = listeners.map { Socket(descriptor: $0.fileNumber) }
    self.configuration = configuration
    self.notify = notify
    self.application = application
  }

  func registerSignals() throws {
    let signals = try SignalHandler()
    signals.register(.interrupt, handleQuit)
    signals.register(.quit, handleQuit)
    signals.register(.terminate, handleTerminate)
    sharedHandler = signals
    SignalHandler.registerSignals()
  }

  public func run() {
    logger.info("Booting worker process with pid: \(getpid())")

    do {
      try registerSignals()
    } catch {
      logger.info("Failed to boot \(error)")
      return
    }
    isAlive = true

    listeners.forEach { $0.blocking = false }

    if listeners.count == 1 {
      runOne(listeners.first!)
    } else {
      runMultiple(listeners)
    }
  }

  func isParentAlive() -> Bool {
    if getppid() != parentPid {
      logger.info("Parent changed, shutting down")
      return false
    }

    return true
  }

  func runOne(_ listener: Socket) {
    while isAlive {
      _ = sharedHandler?.process()
      notify()
      accept(listener)

      if !isParentAlive() {
        return
      }

      _ = wait()
    }
  }

  func runMultiple(_ listeners: [Socket]) {
    while isAlive {
      _ = sharedHandler?.process()
      notify()

      let sockets = wait().filter {
        $0.fileNumber != sharedHandler!.pipe.reader.fileNumber
      }

      sockets.forEach(accept)

      if !isParentAlive() {
        return
      }
    }
  }

  // MARK: Signal Handling

  func handleQuit() {
    isAlive = false
  }

  func handleTerminate() {
{'fraction_non_alphanumeric': 0.10550358929736785, 'fraction_numerical': 0.0021753317380900587, 'mean_word_length': 2.6813450760608486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1069707', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1354, 'n_words': 356}