|  | 
|  | 1 | +/* eslint-disable @typescript-eslint/no-use-before-define */ | 
|  | 2 | +import * as fs from "fs"; | 
|  | 3 | +import * as os from "os"; | 
|  | 4 | +import path from "path"; | 
|  | 5 | +import { performance } from "perf_hooks"; | 
|  | 6 | +import { | 
|  | 7 | +  AbstractCancellationTokenSource, | 
|  | 8 | +  CancellationId, | 
|  | 9 | +  CancellationReceiverStrategy, | 
|  | 10 | +  CancellationSenderStrategy, | 
|  | 11 | +  CancellationStrategy, | 
|  | 12 | +  CancellationToken, | 
|  | 13 | +  Emitter, | 
|  | 14 | +  Event, | 
|  | 15 | +} from "vscode-languageserver"; | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * File based cancellation mostly taken from pyright: https://github.com/microsoft/pyright/blob/a9d2528574087cc2f8c10a7c3aaeb287eb64a870/packages/pyright-internal/src/common/cancellationUtils.ts#L48 | 
|  | 19 | + */ | 
|  | 20 | + | 
|  | 21 | +class FileBasedToken implements CancellationToken { | 
|  | 22 | +  private _isCancelled = false; | 
|  | 23 | +  private _emitter: Emitter<any> | undefined; | 
|  | 24 | + | 
|  | 25 | +  constructor(private _cancellationFilePath: string) {} | 
|  | 26 | + | 
|  | 27 | +  public cancel(): void { | 
|  | 28 | +    if (!this._isCancelled) { | 
|  | 29 | +      this._isCancelled = true; | 
|  | 30 | +      if (this._emitter) { | 
|  | 31 | +        this._emitter.fire(undefined); | 
|  | 32 | +        this.dispose(); | 
|  | 33 | +      } | 
|  | 34 | +    } | 
|  | 35 | +  } | 
|  | 36 | + | 
|  | 37 | +  get isCancellationRequested(): boolean { | 
|  | 38 | +    if (this._isCancelled) { | 
|  | 39 | +      return true; | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    if (this._pipeExists()) { | 
|  | 43 | +      // the first time it encounters cancellation file, it will | 
|  | 44 | +      // cancel itself and raise cancellation event. | 
|  | 45 | +      // in this mode, cancel() might not be called explicitly by jsonrpc layer | 
|  | 46 | +      this.cancel(); | 
|  | 47 | +    } | 
|  | 48 | + | 
|  | 49 | +    return this._isCancelled; | 
|  | 50 | +  } | 
|  | 51 | + | 
|  | 52 | +  get onCancellationRequested(): Event<any> { | 
|  | 53 | +    if (!this._emitter) { | 
|  | 54 | +      this._emitter = new Emitter<any>(); | 
|  | 55 | +    } | 
|  | 56 | +    return this._emitter.event; | 
|  | 57 | +  } | 
|  | 58 | + | 
|  | 59 | +  public dispose(): void { | 
|  | 60 | +    if (this._emitter) { | 
|  | 61 | +      this._emitter.dispose(); | 
|  | 62 | +      this._emitter = undefined; | 
|  | 63 | +    } | 
|  | 64 | +  } | 
|  | 65 | + | 
|  | 66 | +  private _pipeExists(): boolean { | 
|  | 67 | +    try { | 
|  | 68 | +      fs.statSync(this._cancellationFilePath); | 
|  | 69 | +      return true; | 
|  | 70 | +    } catch (e) { | 
|  | 71 | +      return false; | 
|  | 72 | +    } | 
|  | 73 | +  } | 
|  | 74 | +} | 
|  | 75 | + | 
|  | 76 | +export class FileBasedCancellationTokenSource | 
|  | 77 | +  implements AbstractCancellationTokenSource { | 
|  | 78 | +  private _token: CancellationToken | undefined; | 
|  | 79 | +  constructor(private _cancellationFilePath: string) {} | 
|  | 80 | + | 
|  | 81 | +  get token(): CancellationToken { | 
|  | 82 | +    if (!this._token) { | 
|  | 83 | +      // be lazy and create the token only when | 
|  | 84 | +      // actually needed | 
|  | 85 | +      this._token = new FileBasedToken(this._cancellationFilePath); | 
|  | 86 | +    } | 
|  | 87 | +    return this._token; | 
|  | 88 | +  } | 
|  | 89 | + | 
|  | 90 | +  cancel(): void { | 
|  | 91 | +    if (!this._token) { | 
|  | 92 | +      // save an object by returning the default | 
|  | 93 | +      // cancelled token when cancellation happens | 
|  | 94 | +      // before someone asks for the token | 
|  | 95 | +      this._token = CancellationToken.Cancelled; | 
|  | 96 | +    } else { | 
|  | 97 | +      (this._token as FileBasedToken).cancel(); | 
|  | 98 | +    } | 
|  | 99 | +  } | 
|  | 100 | + | 
|  | 101 | +  dispose(): void { | 
|  | 102 | +    if (!this._token) { | 
|  | 103 | +      // ensure to initialize with an empty token if we had none | 
|  | 104 | +      this._token = CancellationToken.None; | 
|  | 105 | +    } else if (this._token instanceof FileBasedToken) { | 
|  | 106 | +      // actually dispose | 
|  | 107 | +      this._token.dispose(); | 
|  | 108 | +    } | 
|  | 109 | +  } | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +export function getCancellationFolderPath(folderName: string): string { | 
|  | 113 | +  return path.join(os.tmpdir(), "elm-language-server-cancellation", folderName); | 
|  | 114 | +} | 
|  | 115 | + | 
|  | 116 | +export function getCancellationFilePath( | 
|  | 117 | +  folderName: string, | 
|  | 118 | +  id: CancellationId, | 
|  | 119 | +): string { | 
|  | 120 | +  return path.join( | 
|  | 121 | +    getCancellationFolderPath(folderName), | 
|  | 122 | +    `cancellation-${String(id)}.tmp`, | 
|  | 123 | +  ); | 
|  | 124 | +} | 
|  | 125 | + | 
|  | 126 | +class FileCancellationReceiverStrategy implements CancellationReceiverStrategy { | 
|  | 127 | +  constructor(readonly folderName: string) {} | 
|  | 128 | + | 
|  | 129 | +  createCancellationTokenSource( | 
|  | 130 | +    id: CancellationId, | 
|  | 131 | +  ): AbstractCancellationTokenSource { | 
|  | 132 | +    return new FileBasedCancellationTokenSource( | 
|  | 133 | +      getCancellationFilePath(this.folderName, id), | 
|  | 134 | +    ); | 
|  | 135 | +  } | 
|  | 136 | +} | 
|  | 137 | + | 
|  | 138 | +let cancellationFolderName: string; | 
|  | 139 | + | 
|  | 140 | +export function getCancellationStrategyFromArgv( | 
|  | 141 | +  argv: string[], | 
|  | 142 | +): CancellationStrategy { | 
|  | 143 | +  let receiver: CancellationReceiverStrategy | undefined; | 
|  | 144 | + | 
|  | 145 | +  for (let i = 0; i < argv.length; i++) { | 
|  | 146 | +    const arg = argv[i]; | 
|  | 147 | +    if (arg === "--cancellationReceive") { | 
|  | 148 | +      receiver = createReceiverStrategyFromArgv(argv[i + 1]); | 
|  | 149 | +    } else { | 
|  | 150 | +      const args = arg.split("="); | 
|  | 151 | +      if (args[0] === "--cancellationReceive") { | 
|  | 152 | +        receiver = createReceiverStrategyFromArgv(args[1]); | 
|  | 153 | +      } | 
|  | 154 | +    } | 
|  | 155 | +  } | 
|  | 156 | + | 
|  | 157 | +  if (receiver && !cancellationFolderName) { | 
|  | 158 | +    cancellationFolderName = (receiver as FileCancellationReceiverStrategy) | 
|  | 159 | +      .folderName; | 
|  | 160 | +  } | 
|  | 161 | + | 
|  | 162 | +  receiver = receiver ? receiver : CancellationReceiverStrategy.Message; | 
|  | 163 | +  return { receiver, sender: CancellationSenderStrategy.Message }; | 
|  | 164 | + | 
|  | 165 | +  function createReceiverStrategyFromArgv( | 
|  | 166 | +    arg: string, | 
|  | 167 | +  ): CancellationReceiverStrategy | undefined { | 
|  | 168 | +    const folderName = extractCancellationFolderName(arg); | 
|  | 169 | +    return folderName | 
|  | 170 | +      ? new FileCancellationReceiverStrategy(folderName) | 
|  | 171 | +      : undefined; | 
|  | 172 | +  } | 
|  | 173 | + | 
|  | 174 | +  function extractCancellationFolderName(arg: string): string | undefined { | 
|  | 175 | +    const fileRegex = /^file:(.+)$/; | 
|  | 176 | +    const folderName = fileRegex.exec(arg); | 
|  | 177 | +    return folderName ? folderName[1] : undefined; | 
|  | 178 | +  } | 
|  | 179 | +} | 
|  | 180 | + | 
|  | 181 | +export class OperationCanceledException {} | 
|  | 182 | + | 
|  | 183 | +export interface ICancellationToken { | 
|  | 184 | +  isCancellationRequested(): boolean; | 
|  | 185 | + | 
|  | 186 | +  /** @throws OperationCanceledException if isCancellationRequested is true */ | 
|  | 187 | +  throwIfCancellationRequested(): void; | 
|  | 188 | +} | 
|  | 189 | + | 
|  | 190 | +/** | 
|  | 191 | + * ThrottledCancellationToken taken from Typescript: https://github.com/microsoft/TypeScript/blob/79ffd03f8b73010fa03cef624e5f1770bc9c975b/src/services/services.ts#L1152 | 
|  | 192 | + */ | 
|  | 193 | +export class ThrottledCancellationToken implements ICancellationToken { | 
|  | 194 | +  // Store when we last tried to cancel.  Checking cancellation can be expensive (as we have | 
|  | 195 | +  // to marshall over to the host layer).  So we only bother actually checking once enough | 
|  | 196 | +  // time has passed. | 
|  | 197 | +  private lastCancellationCheckTime = 0; | 
|  | 198 | + | 
|  | 199 | +  constructor( | 
|  | 200 | +    private cancellationToken: CancellationToken, | 
|  | 201 | +    private readonly throttleWaitMilliseconds = 20, | 
|  | 202 | +  ) {} | 
|  | 203 | + | 
|  | 204 | +  public isCancellationRequested(): boolean { | 
|  | 205 | +    const time = performance.now(); | 
|  | 206 | +    const duration = Math.abs(time - this.lastCancellationCheckTime); | 
|  | 207 | +    if (duration >= this.throttleWaitMilliseconds) { | 
|  | 208 | +      // Check no more than once every throttle wait milliseconds | 
|  | 209 | +      this.lastCancellationCheckTime = time; | 
|  | 210 | +      return this.cancellationToken.isCancellationRequested; | 
|  | 211 | +    } | 
|  | 212 | + | 
|  | 213 | +    return false; | 
|  | 214 | +  } | 
|  | 215 | + | 
|  | 216 | +  public throwIfCancellationRequested(): void { | 
|  | 217 | +    if (this.isCancellationRequested()) { | 
|  | 218 | +      throw new OperationCanceledException(); | 
|  | 219 | +    } | 
|  | 220 | +  } | 
|  | 221 | +} | 
0 commit comments