multi_threaded_proxy_resolver.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_
  5. #define NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <set>
  9. #include "base/memory/ref_counted.h"
  10. #include "net/base/completion_once_callback.h"
  11. #include "net/base/net_export.h"
  12. #include "net/proxy_resolution/proxy_resolver_factory.h"
  13. namespace net {
  14. class ProxyResolver;
  15. // MultiThreadedProxyResolverFactory creates instances of a ProxyResolver
  16. // implementation that runs synchronous ProxyResolver implementations on worker
  17. // threads.
  18. //
  19. // Threads are created lazily on demand, up to a maximum total. The advantage
  20. // of having a pool of threads, is faster performance. In particular, being
  21. // able to keep servicing PAC requests even if one blocks its execution.
  22. //
  23. // During initialization (CreateProxyResolver), a single thread is spun up to
  24. // test the script. If this succeeds, we cache the input script, and will re-use
  25. // this to lazily provision any new threads as needed.
  26. //
  27. // For each new thread that we spawn in a particular MultiThreadedProxyResolver
  28. // instance, a corresponding new ProxyResolver is created using the
  29. // ProxyResolverFactory returned by CreateProxyResolverFactory().
  30. //
  31. // Because we are creating multiple ProxyResolver instances, this means we
  32. // are duplicating script contexts for what is ordinarily seen as being a
  33. // single script. This can affect compatibility on some classes of PAC
  34. // script:
  35. //
  36. // (a) Scripts whose initialization has external dependencies on network or
  37. // time may end up successfully initializing on some threads, but not
  38. // others. So depending on what thread services the request, the result
  39. // may jump between several possibilities.
  40. //
  41. // (b) Scripts whose FindProxyForURL() depends on side-effects may now
  42. // work differently. For example, a PAC script which was incrementing
  43. // a global counter and using that to make a decision. In the
  44. // multi-threaded model, each thread may have a different value for this
  45. // counter, so it won't globally be seen as monotonically increasing!
  46. class NET_EXPORT_PRIVATE MultiThreadedProxyResolverFactory
  47. : public ProxyResolverFactory {
  48. public:
  49. MultiThreadedProxyResolverFactory(size_t max_num_threads,
  50. bool factory_expects_bytes);
  51. ~MultiThreadedProxyResolverFactory() override;
  52. int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script,
  53. std::unique_ptr<ProxyResolver>* resolver,
  54. CompletionOnceCallback callback,
  55. std::unique_ptr<Request>* request) override;
  56. private:
  57. class Job;
  58. // Invoked to create a ProxyResolverFactory instance to pass to a
  59. // MultiThreadedProxyResolver instance.
  60. virtual std::unique_ptr<ProxyResolverFactory>
  61. CreateProxyResolverFactory() = 0;
  62. void RemoveJob(Job* job);
  63. const size_t max_num_threads_;
  64. std::set<Job*> jobs_;
  65. };
  66. } // namespace net
  67. #endif // NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_