Mercurial > hg-stable
comparison mercurial/wireprototypes.py @ 37783:9d818539abfa
wireproto: move supportedcompengines out of wireproto
This function is used by both version 1 and version 2. It belongs in
a common module.
"wireprototypes" may not be the best module name. I may rename it...
Differential Revision: https://phab.mercurial-scm.org/D3398
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 16 Apr 2018 22:08:13 -0700 |
parents | 352932a11905 |
children | 856f381ad74b |
comparison
equal
deleted
inserted
replaced
37782:99accae4cc59 | 37783:9d818539abfa |
---|---|
9 bin, | 9 bin, |
10 hex, | 10 hex, |
11 ) | 11 ) |
12 from .thirdparty.zope import ( | 12 from .thirdparty.zope import ( |
13 interface as zi, | 13 interface as zi, |
14 ) | |
15 from .i18n import _ | |
16 from . import ( | |
17 error, | |
18 util, | |
14 ) | 19 ) |
15 | 20 |
16 # Names of the SSH protocol implementations. | 21 # Names of the SSH protocol implementations. |
17 SSHV1 = 'ssh-v1' | 22 SSHV1 = 'ssh-v1' |
18 # These are advertised over the wire. Increment the counters at the end | 23 # These are advertised over the wire. Increment the counters at the end |
317 | 322 |
318 if proto.name not in entry.transports: | 323 if proto.name not in entry.transports: |
319 return False | 324 return False |
320 | 325 |
321 return True | 326 return True |
327 | |
328 def supportedcompengines(ui, role): | |
329 """Obtain the list of supported compression engines for a request.""" | |
330 assert role in (util.CLIENTROLE, util.SERVERROLE) | |
331 | |
332 compengines = util.compengines.supportedwireengines(role) | |
333 | |
334 # Allow config to override default list and ordering. | |
335 if role == util.SERVERROLE: | |
336 configengines = ui.configlist('server', 'compressionengines') | |
337 config = 'server.compressionengines' | |
338 else: | |
339 # This is currently implemented mainly to facilitate testing. In most | |
340 # cases, the server should be in charge of choosing a compression engine | |
341 # because a server has the most to lose from a sub-optimal choice. (e.g. | |
342 # CPU DoS due to an expensive engine or a network DoS due to poor | |
343 # compression ratio). | |
344 configengines = ui.configlist('experimental', | |
345 'clientcompressionengines') | |
346 config = 'experimental.clientcompressionengines' | |
347 | |
348 # No explicit config. Filter out the ones that aren't supposed to be | |
349 # advertised and return default ordering. | |
350 if not configengines: | |
351 attr = 'serverpriority' if role == util.SERVERROLE else 'clientpriority' | |
352 return [e for e in compengines | |
353 if getattr(e.wireprotosupport(), attr) > 0] | |
354 | |
355 # If compression engines are listed in the config, assume there is a good | |
356 # reason for it (like server operators wanting to achieve specific | |
357 # performance characteristics). So fail fast if the config references | |
358 # unusable compression engines. | |
359 validnames = set(e.name() for e in compengines) | |
360 invalidnames = set(e for e in configengines if e not in validnames) | |
361 if invalidnames: | |
362 raise error.Abort(_('invalid compression engine defined in %s: %s') % | |
363 (config, ', '.join(sorted(invalidnames)))) | |
364 | |
365 compengines = [e for e in compengines if e.name() in configengines] | |
366 compengines = sorted(compengines, | |
367 key=lambda e: configengines.index(e.name())) | |
368 | |
369 if not compengines: | |
370 raise error.Abort(_('%s config option does not specify any known ' | |
371 'compression engines') % config, | |
372 hint=_('usable compression engines: %s') % | |
373 ', '.sorted(validnames)) | |
374 | |
375 return compengines |