๐Ÿ“ฆ Bogdanp / Noise

๐Ÿ“„ Backend.swift ยท 171 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171import Dispatch
import Foundation
import Noise
import NoiseSerde
import OSLog

fileprivate let logger = Logger(
  subsystem: "io.defn.NoiseBackend",
  category: "Backend"
)

/// Statistics reported by Backends.
public struct BackendStats: Sendable {
  public let totalRequests: UInt64
  public let totalWaitDuration: Duration
  public let totalReadDuration: Duration
  public let totalWriteDuration: Duration
}

/// Client implementation for an async Racket backend.
public final class Backend: @unchecked Sendable {
  private let ip = Pipe() // in  from Racket's perspective
  private let op = Pipe() // out from Racket's perspective

  private let mu = DispatchSemaphore(value: 1) // mu guards everything below here
  private let out: OutputPort!
  private var seq = UVarint(0)
  fileprivate var pending = [UInt64: ResponseHandler]()
  private var totalRequests = UInt64(0)
  private var totalWaitDuration = Duration.zero
  private var totalReadDuration = Duration.zero
  private var totalWriteDuration = Duration.zero

  public init(withZo zo: URL, andMod modname: String, andProc proc: String) {
    out = FileHandleOutputPort(withHandle: ip.fileHandleForWriting)
    let server = Thread {
      self.serve(zo, modname, proc)
    }
    server.name = "Noise Backend (Server)"
    server.qualityOfService = .userInitiated
    server.start()
    let reader = Thread {
      self.read()
    }
    reader.name = "Noise Backend (Reader)"
    reader.qualityOfService = .userInitiated
    reader.start()
  }

  private func serve(_ zo: URL, _ modname: String, _ proc: String) {
    logger.debug("\(#function): booting Racket")
    let t0 = ContinuousClock.now
    let r = Racket(execPath: zo.path)
    let dt = ContinuousClock.now - t0
    logger.debug("\(#function): took \(dt) to boot Racket")
    r.bracket {
      logger.debug("\(#function): loading backend")
      let t0 = ContinuousClock.now
      r.load(zo: zo)
      let dt = ContinuousClock.now - t0
      logger.debug("\(#function): took \(dt) to load backend")
      let mod = Val.cons(Val.symbol("quote"), Val.cons(Val.symbol(modname), Val.null))
      let ifd = Val.fixnum(Int(ip.fileHandleForReading.fileDescriptor))
      let ofd = Val.fixnum(Int(op.fileHandleForWriting.fileDescriptor))
      let serve = r.require(Val.symbol(proc), from: mod).unsafeCar()
      serve.unsafeApply(Val.cons(ifd, Val.cons(ofd, Val.null)))
      preconditionFailure("Racket server exited")
    }
  }

  private func read() {
    let inp = FileHandleInputPort(withHandle: op.fileHandleForReading)
    var buf = Data(count: 8*1024) // will grow as needed
    while true {
      let id = UVarint.read(from: inp, using: &buf)
      //logger.debug("\(RequestId(value: id)): reading response; bufsize: \(buf.count)b")
      mu.wait()
      guard let handler = pending[id] else {
        logger.fault("\(RequestId(value: id)): future is gone")
        mu.signal()
        continue
      }
      mu.signal()
      let readDuration = handler.handle(from: inp, using: &buf)
      //logger.debug("\(RequestId(value: id)): took \(Duration(nanos: readDuration), privacy: .public) to read")
      mu.wait()
      pending.removeValue(forKey: id)
      let requestDuration = ContinuousClock.now - handler.time
      totalRequests += 1
      totalWaitDuration += requestDuration
      totalReadDuration += readDuration
      mu.signal()
      logger.debug("\(RequestId(value: id)): took \(requestDuration) to fulfill")
    }
  }

  public func send<T>(
    writeProc write: (OutputPort) -> Void,
    readProc read: @escaping (InputPort, inout Data) -> T,
    commandName: String = #function
  ) -> Future<String, T> {
    mu.wait()
    let id = seq
    seq += 1
    logger.debug("\(RequestId(value: id)): \(commandName)")
    let t0 = ContinuousClock.now
    id.write(to: out)
    write(out)
    out.flush()
    let dt = ContinuousClock.now - t0
    let fut = Future<String, T>()
    let handler = ResponseHandlerImpl<T>(id: id, fut: fut, read: read)
    pending[id] = handler
    totalWriteDuration += dt
    mu.signal()
    return fut
  }

  public func stats() -> BackendStats {
    mu.wait()
    defer { mu.signal() }
    return BackendStats(
      totalRequests: totalRequests,
      totalWaitDuration: totalWaitDuration,
      totalReadDuration: totalReadDuration,
      totalWriteDuration: totalWriteDuration
    )
  }
}

fileprivate struct Request<Data: Writable>: Writable {
  let id: UVarint
  let data: Data

  func write(to out: OutputPort) {
    id.write(to: out)
    data.write(to: out)
  }
}

fileprivate protocol ResponseHandler {
  var time: ContinuousClock.Instant { get }

  func handle(from inp: InputPort, using buf: inout Data) -> Duration
}

fileprivate struct ResponseHandlerImpl<T>: ResponseHandler where T: Sendable {
  let id: UInt64
  let fut: Future<String, T>
  let read: (InputPort, inout Data) -> T
  let time = ContinuousClock.now

  func handle(from inp: InputPort, using buf: inout Data) -> Duration {
    let t0 = ContinuousClock.now
    if inp.readByte() == 1 {
      fut.resolve(with: read(inp, &buf))
    } else {
      fut.reject(with: String.read(from: inp, using: &buf))
    }
    return ContinuousClock.now - t0
  }
}

fileprivate struct RequestId: CustomStringConvertible {
  let value: UInt64

  var description: String {
    return String(format: "#%06d", value)
  }
}