Commit Graph

421 Commits

Author SHA1 Message Date
Oliver Gould 3aa5ac0453 proxy: Update prost to 0.4.0 (#1127)
prost-0.4.0 has been released, which removes unnecessary dependencies.
tower-grpc is being updated simultaneously, as this is the proxy's
primary use of prost.

See: https://github.com/danburkert/prost/releases/tag/v0.4.0
2018-06-14 16:29:41 -07:00
Oliver Gould 8becd7bfa1 proxy: Update `rand` to 0.5.1 (#1125)
* proxy: Update `rand` to 0.5.1

The proxy depends on rand-0.4, which is superceded by newer APIs in
rand-0.5. Since we're already using rand-0.5 via the tower-balance
crate, it seems appropriate to upgrade the proxy.

* Expand lock files in reviews
2018-06-14 15:09:58 -07:00
Oliver Gould e8e163a2e9 proxy: Convert `convert` from crate to module (#1115)
In e2093e3, we created a `convert` crate when refactoring the proxy's
gRPC bindings into a dedicated crate.

It's not really necessary to handle `convert` as a crate, given that it
holds a single 39-line file that's mostly comments. It's possible to
"vendor" this file in the proxy, and controller-grpc crate doesn't
even need this trait (in fact, the proxy probably doesn't either).
2018-06-13 16:18:51 -07:00
Oliver Gould 72cd70bef2 proxy: Update tower-balance (#1093)
To pick up https://github.com/tower-rs/tower/pull/86
2018-06-08 15:52:52 -07:00
Oliver Gould 2ed7c4ad40 proxy: Use a PeakEWMA outbound load balancer (#1080)
`tower-balance` has been updated with a Peak-EWMA load balancer; and a
new crate, `tower-h2-balance` has been introduced to make the load
balancer aware of some H2 stream events.

The Peak-EWMA balancer is designed to reduce tail latency by maintaining
an Exponentially Weighted Moving Average of latencies to each endpoint
which decay over a 10s window.
2018-06-07 22:06:12 -07:00
Eliza Weisman 0687665444 proxy: Forward TLS config to client watches (#1087)
This commit adds the initial wiring to forward TLS config changes to the 
watches used by TLS clients as well as TLS servers. As the TLS clients
are not yet implemented, the config type is currently `()`, but once
the client config is implemented, we should be able to drop it in 
seamlessly.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Co-authored-by: Brian Smith <brian@briansmith.org>
2018-06-07 19:16:54 -07:00
Eliza Weisman d70bb6d06c proxy: More DNS cleanup (#1052)
Depends on #1032. 

This branch makes some additional changes to the proxy's DNS code. In
particular, since we no longer need to clone the resolver on every lookup,
it removes some `clone()` calls in `DestinationSet::reset_dns_query`.
I've also changed the DNS futures to use the new contextual logging code 
on master.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-07 17:16:07 -07:00
Brian Smith c5b13fb3f7 Proxy: Better encapsulate the details of TLS config watching. (#1082, #1083)
* Fix non-Linux builds.

The change to signal.rs is needed for Windows.
The change to config.rs is needed for Windows and maybe other platforms.

Signed-off-by: Brian Smith <brian@briansmith.org>

* Proxy: Better encapsulate the details of TLS config watching.

Encapsulate more of the TLS configuration logic in the TLS submodule. This allows
for easier refactoring. In particular, this will make adding the client TLS configuration
easier.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-06-07 13:08:37 -10:00
Eliza Weisman fbbab10c6e proxy: detect TLS configuration changes using inotify on Linux (#1077)
This branch adds an inotify-based implementation of filesystem watches
for the TLS config files. On Linux, where inotify is available, this is
used instead of the polling-based code I added in #1056 and #1076.

In order to avoid the issues detecting changes to files in Kubernetes 
ConfigMaps described in #1061, we watch the directory _containing_ the
files we care about rather than the files themselves. I've tested this 
manually in Docker for Mac Kubernetes and can confirm that ConfigMap
changes are detected successfully.

Closes #1061. Closes #369.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-07 14:45:20 -07:00
Brian Smith 19d92e9ad2 Proxy: Map Kubernetes Pod Namespace/Name to TLS identity. (#1074)
* Proxy: Map Kubernetes Pod Namespace/Name to TLS identity.

Map the Kubernetes identity into a DNS name that can be used to
validate the peer's certificate. The final mapping is TBD; the
important thing for now is that the mapped name doesn't collide
with any real DNS name.

Encapsulate the mapping logic within the TLS submodule.

Minimize `Arc`ing and `Clone`ing of TLS identities.

This has no effect in default configurations since the settings that
enable the functionality are not set by default.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-06-07 11:14:57 -10:00
Eliza Weisman f17b09898a proxy: Canonicalize TLS config paths before polling filesystem metadata (#1076)
This branch changes the polling-based implementation of TLS config file watches
to fully canonicalize the path to each config file prior to polling for its
metadata. Doing so fixes the issues detecting changes when the watched path is
a symbolic link to another symbolic link (see #1061), which is how Kubernetes
implements ConfigMaps mounted as volumes.

I've manually tested this with Conduit running in Docker for Mac Kubernetes,
by volume-mounting a ConfigMap containing the TLS config files, and 
regenerating, deleting, and adding the certificates. Watching the Conduit logs
confirms that the changes are now successfully detected.

Note that we have to re-canonicalize the path every time we poll the filesystem
for metadata. Otherwise, if the file is a symlink and the link target changes,
we will continue polling the _old_ link target's path, and fail to detect any
changes to the _new_ link target.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-06 13:43:17 -07:00
Brian Smith 143f3668d1 Implement `dns::Name` using webpki's `DNSName`. (#1071)
webpki's DNSName type does full validation and normalization (lowercasing) of
DNS names, which is exactly what `dns::Name` does. webpki's DNSName type
considers a DNS name to be valid according to the rules for TLS certificates,
which is slightly stricter than what a DNS library might otherwise allow. In
anticipation of possible compatibility issues, introduce separate tls::DnsName
and dns::Name names for this type. In the future, if we find that tls::DnsName
is too strict for non-TLS cases, we can have these types diverge without
affecting TLS validation.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-06-05 14:23:08 -10:00
Sean McArthur 6c7173075c proxy: update to released hyper 0.12 (#1069) 2018-06-05 17:05:10 -07:00
Brian Smith a70e324da3 Ensure Request IDs are unique & avoid `Arc`ing. (#1066)
Encapsulate HTTP request ID generation logic.

Request IDs need to be globally unique, so there can only be one request ID
sequence per process. Simplify the request ID generation with that in mind,
and make it more efficient.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-06-05 13:42:46 -10:00
Brian Smith f9cc52e1c0 Stop cloning & hashing HTTP requests & related types. (#1058)
During code review of another change I noticed that a lot of types seem
to derive `Hash` (and `Eq`, `PartialEq`) even though the types should
never (for performance reasons) be used as keys of a hash table, and
where it is kind of questionable what equality should mean for those
types. Then I noticed that similarly many types implement `Clone` even
though I expect we should never be cloning them, again because of our
performance goals.

Because these types derive these traits, then whenever we add a field
to them, that field also has to implement these traits. That means we
then have to expand the problem, deriving implementations of these
traits for types that don't otherwise want/need to implement these
traits. This makes review complicated, because, for example, we have
to decide whether something should be compared case-insensitively or
case-sensitively when really we don't want to compare those things at
all.

To prove that we can get by by doing less, to speed up code review
(particularly related to some stuff related to TLS), stop deriving
`Clone`, `Eq`, `PartialEq`, and `Hash` for these types.

I believe that, in particular, the change to key the Tap hash table
based on request ID, instead of the whole request, should speed up
the tap feature since we don't hash and/or compare every field,
recursively, of requests.

Later more such cleanup of this sort should be done.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-06-05 08:58:18 -10:00
Eliza Weisman 73163ad019 proxy: Add TLS identity to endpoint metadata and wire it through to `Connect::new` (#1008)
Depends on #1006. Depends on #1041.

This PR adds a `tls_identity` field to the endpoint `Metadata` struct, which
contains the `TlsIdentity` metadata sent by the control plane's Destination
service. 

I changed the `ctx::transport::Client` context struct to hold a `Metadata`,
rather than just the labels, so the TLS support determination is always
available. In addition, I've added it as an additional parameter to 
`transport::Connect::new`, so that when we create a new connection, the TLS
code will be able to determine whether or not TLS is supported and, if it is, 
how to verify the endpoint's identity.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-04 20:08:55 -07:00
Eliza Weisman 1678d6b33b proxy: Change `DEFAULT_OUTBOUND_ROUTER_CAPACITY` from 10,000 to 100 (#1060)
The proxy can't actually support 10K clients currently (for one, we can't open
10K resolution streams to the destination service). 100 is a more-realistic 
but sufficiently-high default.
2018-06-04 14:34:34 -07:00
Eliza Weisman 896fe75929 proxy: Reload TLS config on changes (#1056)
This PR modifies the proxy's TLS code so that the TLS config files are reloaded
when any of them has changed (including if they did not previously exist).

If reloading the configs returns an error, we log an error and continue using
the old config.

Currently, this is implemented by polling the file system for the time they
were last modified at a fixed interval. However, I've implemented this so 
that the changes are passed around as a `Stream`, and that reloading and
updating the config is in a separate function the one that detects changes.
Therefore, it should be fairly easy to plug in support for `inotify` (and 
other FS watch APIs) later, as long as we can use them to generate a 
`Stream` of changes.

Closes #369 

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-04 13:36:28 -07:00
Eliza Weisman f73e34e0d8 proxy: Update `dns` module to use new Trust-DNS `AsyncResolver` API (#1032)
Depends on #974.  Closes #859.

This PR updates the proxy's `dns` module to use the new `AsyncResolver` API I
added to `trust-dns-resolver` in bluejekyll/trust-dns#487. This allows us to 
spawn one `Future` that will drive DNS resolution in the background, rather
than having to repeatedly clone a heavyweight `ResolverFuture` for every 
lookup. It also means that we no longer have to clone the name to resolve in
quite as many places.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-01 14:36:37 -07:00
Eliza Weisman 0e29e2f5cf proxy: Honor TTLs for DNS responses (#974)
Closes #711. Depends on #967.

This PR changes the proxy's `destination` module to honor the TTLs associated
with DNS lookups, now that bluejekyll/trust-dns#444 has been merged and we can
access this information from the Trust-DNS Resolver API. 

The `destination::background::DestinationSet` type has been modified so that, 
when a successful result is received for a DNS query, the DNS server will be 
polled again after the deadline associated with that query, rather than after
a fixed deadline. The fixed deadline is still used to determine when to poll
again for negative DNS responses or for errors.

Furthermore, Conduit now accepts an optional CONDUIT_PROXY_DNS_MIN_TTL 
environment variable that will configure a minimum TTL for DNS results. If the
deadline of a DNS response gives it a TTL shorter than the configured minimum,
Conduit will not poll DNS again until after that minimum TTL is elapsed. By
default, there is no minimum value set, as this feature is intended primarily
for when Conduit is run locally for development purposes.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-06-01 12:17:48 -07:00
Sean McArthur fbbe28d032 proxy: update h2 to cancel reset requests (#1051)
This includes the changes that should detect when a client sends a `RST_STREAM`, and cancels our pending response future or streaming body.

Closes #986
2018-06-01 02:53:21 +02:00
Brian Smith 044754ee24 Add initial infrastructure for optionally accepting TLS connections (#1047)
* Add initial infrastructure for optinally accepting TLS connections.

If the environment gives us the paths to the certificate chain and private key
then use TLS for all accepted TCP connections. Otherwise, continue on using
plaintext for all accepted TCP connections. The default behavior--no TLS--isn't
changed.

Later we'll make this smarter by adding protocol detection so that when the TLS
configuration is available, we'll accept both TLS and non-TLS connections.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-05-31 12:20:57 -10:00
Eliza Weisman be9486c239 proto: Add TLS identity to WeightedAddr message (#1041)
Required for #1008.

This PR adds the `TlsIdentity` message to the Destination service proto,
to describe what strategy the proxy should use for verifying an endpoint's TLS
certificates. It also adds a `TlsIdentity` field to the `WeightedAddr` message.

Currently, there is one possible variant for `TlsIdentity`, `KubernetesPodName`, 
which consists of the Kubernetes pod name of the endpoint, the namespace of
the endpoint, and the namespace of that pod's Conduit control plane. The proxy
should attempt to connect over TLS if the control plane namespace matches its 
own control plane namespace. The pod name and namespace are used to verify 
the endpoint's TLS certificate.

See https://github.com/runconduit/conduit/issues/386#issuecomment-392948046.

This change was initially part of #1008, but I factored it out to make the diff
smaller.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-31 11:48:25 -07:00
Oliver Gould 30ae471dda proxy: Add rich logging contexts (#1037)
While debugging proxy issues, I found it necessary to change how logging contexts are
instrumented, especially for clients.

This change moves away from using `Debug` types to in favor of `Display` types.
Furthermore, the `logging` module now provides a uniform set of logging contexts to be
used throughout the application.  All clients, servers, and background tasks should now be
instrumented so that their log messages contain predictable metadata.

Some small improvements have been made to ensure that logging contexts are correct
when a `Future` is dropped (which is important for some H2 uses, especially).
2018-05-30 13:41:59 -07:00
Oliver Gould 91075e7d32 proxy: Fix bench tests and require bench tests in CI (#1038)
b3170af changed the DstLabels api, but the bench test was not updated
accordingly.

Furthermore, since bench tests require a nightly rust version, we've
avoided running them in CI. This makes it easy for these tests to break, however.

This updates the benches/record.rs. Additionally, in CI, we pin the rust nightly'
version to a known-good version so that we can reliably run these bench test
without the fear of external changes breaking our build.
2018-05-30 07:20:28 -07:00
Oliver Gould 1c8916550e proxy: Ensure labels are reliably ordered (#1030)
The proxy receives a hash map of endpoint labels from the destination
service. As this map is serialized into a string, its keys and values
do not have a stable ordering.

To fix this, we sort the keys for all labels before constructing an
instance of `DstLabels`.

This change was much more difficult to test than it was to fix, so tests
this change was tested manually.

Fixes #1015
2018-05-30 07:13:26 -07:00
Eliza Weisman ec72012982 proxy: Remove dynamic label updating on bound services (#1006)
Depends on tower-rs/tower#75. Required for #386

In order for the proxy to use the TLS support metadata from the Destination 
service correctly, we determined that the code for dynamically changing the
labels on an already-bound service should be removed, and any change in
metadata should cause an endpoint to be rebound.

I've modified the proxy so that we no longer update the labels using 
`futures-watch` (as a sidenote, we no longer depend on that crate). Metadata
update events now cause the `tower-discover::Discover` implementation for 
`DestinationSet` to re-insert the changed endpoint into the load balancer.
Upstream PR tower-rs/tower#75 in tower-balance changes the load balancer 
to honor duplicate insertions by replacing the old endpoint rather than 
ignoring them; that change is necessary for the tests to pass on this branch.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-29 12:48:59 -07:00
Brian Smith 4ede9a7ef3 Fix location of raw pointer comment in `ContextGuard`. (#1027)
Commit b861a6df317c937123825098a7ef0b50cf52e281 moved the code the
comment was describing, but didn't move the comment.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-05-26 18:30:37 -10:00
Brian Smith 79a38327d2 Abstract I/O interface into a trait. (#1020)
* Rename so_original_dst.rs to addr_info.rs.

Prepare for expanding the functionality of this module by renaming it.

Signed-off-by: Brian Smith <brian@briansmith.org>

* Abstract I/O interface into a trait.

Instead of pattern matching over an `Io` variant, use a `Box<Io>` to
abstract the I/O interface. This will make it easier to add a TLS
transport.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-05-26 10:04:31 -10:00
Eliza Weisman 4cca72fb92 proxy: Fix missing logging contexts on inbound/outbound (#1025)
Changes to `BoundPort::listen_and_fold` inadvertently broke the 
`::logging::context_future`s on the `serve` futures for the Inbound and 
outbound proxies, leading to log messages that didn't have the appropriate
context. This fixes that.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-25 16:31:33 -07:00
Brian Smith 7764e97a25 Prepare `BoundPort::listen_and_fold` for upcoming TLS work. (#1018)
Refactor `listen_and_fold()` to make it possible to insert more futures
into the chain before the folding.

Signed-off-by: Brian Smith <brian@briansmith.org>
2018-05-25 09:59:05 -10:00
Oliver Gould 0e6c4c1450 proxy: Record EOS when bodies are dropped (#1003)
It appears that hyper does not necessarily poll bodies to completion,
and instead simply drops a body as soon as `content-length` is reached
(hyperium/hyper#1521).

This change implements Drop for MeasuredBody such that the stream-end
event is triggered if it had not been triggered previously. This ensures
that response latencies and counts are recorded for HTTP/1 streams.

Fixes #994
2018-05-24 10:40:29 -07:00
Oliver Gould 4fdbd1631a proxy: Fix h1 body implementation (#995)
In the h1-h2 glue code, we incorrectly called `is_empty()` to determine
if an H1 stream had ended. `is_empty` only returns true if there was no
body at all (rather than if the body has been fully consumed).

By changing this to call `hyper::body::Payload::is_end_stream`, h1
bodies now behave the same as h2 bodies.

Relates to #994
2018-05-24 07:23:14 -07:00
Oliver Gould 1d5ef1e4d5 proxy: Record HTTP latency at first data frame (#981)
Currently, the proxy records a request's latency as the time between
when a request is opened and when its response stream completes. This is
not what we intend to record, especially when a response is long-lived.

In order to more accurate record latency, we want to track the time at
which the first response body frame is received (which is a close
approximation of time-to-first-byte).

Telemetry aggregation has been changed to use the first-frame time to
compute latencies; tests have been updated to exercise this behavior; and
the metrics documentation has been updated to reflect this change.

Addresses #818 
Relates to #980
2018-05-23 16:02:44 -07:00
Carl Lerche f41e74fd2c Proxy: Bump h2 version to v0.1.8 (#990)
Signed-off-by: Carl Lerche <me@carllerche.com>
2018-05-23 12:45:14 -07:00
Oliver Gould afa7fef976 proxy: Alter telemetry to use discrete instants (#980)
Proxy tasks emit events to the telemetry system. These events are used
aggregate counts and latencies, as well as to inform Tap requests.
Initially, these events included durations, describing the relevant time
that elapsed between this event and another.

This approach is somewhat inflexible -- it unnecessarily constrains the
set of measurements that can computed in the telemetry system.

To remedy this, the `Event` types can be changed to report discrete
`Instant`s (rather than `Duration`s). Then, when latencies are computed
in the telemetry system, these discrete instants can be compared to
produce durations.

There are no functional changes in this PR.
2018-05-22 14:57:00 -07:00
Eliza Weisman d709ec37e3 proxy: Remove configure-and-bind-to-executor pattern (#967)
A common pattern when using the old Tokio API was separating the configuration
of a task from binding it to an executor to run on. This was often necessary
when we wanted to construct a type corresponding to some task before the
reactor on which it would execute was initialized. Typically, this was 
accomplished with two separate types, one of which represented the 
configuration and exposed only a method to take a reactor `Handle` and
transform it to the other type, representing the actual task.

After we migrate to the new Tokio API in #944, executors no longer need to be
passed explictly, as we can use `DefaultExecutor::current` or 
`current_thread::TaskExecutor::current` to spawn a task on the current 
executor. Therefore, a lot of this complexity can be refactored away.

This PR refactors the `Config` and `Process` structs in
i`control::destination::background` into a single `Background` struct, and 
removes the `dns::Config` and `telemetry::MakeControl` structs (`dns::Resolver`
and `telemetry::Control` are now constructed directly). It should not cause
any functional changes.

Closes #966

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-21 15:40:33 -07:00
Eliza Weisman 7bf4c1bc41 proxy: Use `impl Trait` to unbox some futures (#969)
Now that `impl Trait` is stable, we don't need to box as many futures. We still
need to box before spawning them on an executor, but the component futures no 
longer require their own boxes. 

Signed-off-by: Eliza Weisman <eliza@buoyant.io
2018-05-19 13:19:05 -07:00
Eliza Weisman 1b1623dd83 proxy: Upgrade Conduit to use the new version of Tokio (#944)
Closes #888.  Closes #867.

This branch upgrades Conduit to use the new Tokio API. It was also necessary to
upgrade some other dependencies (including `hyper`, and `trust-dns`) alongside
this upgrade. 

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-17 16:38:15 -07:00
Sean McArthur d9fd091411 proxy: rebind services on connect errors (#952)
Instead of having connect errors destroy all buffered requests,
this changes Bind to return a service that can rebind itself when
there is a connect error.

It won't try to establish the new connection itself, but waits for
the buffer to poll again. Combing this with changes in tower-buffer
to remove canceled requests from the buffer should mean that we
won't loop on connect errors for forever.

Signed-off-by: Sean McArthur <sean@seanmonstar.com>
2018-05-17 14:15:16 -07:00
Oliver Gould cd923abf94 proxy: Drop destination resolutions when unused (#956)
A proxy dispatches requests over a constrained number of routes. When
the router's upper bound is reached---and potentially in other future
scenarios---router capacity is created by removing unused routes, their
load balancers, and all related endpoint stacks.

However, in the current regime, the controller subsystem will continue
to monitor discovery observations. As the number of active observations
expands over time, the controller task ends up with more and more work
to do.

This change introduces a shared atomic boolean between the resolution
returned to the load balancer and the state maintained when
communicating with the service. Before the controller polls its active
resolutions, it first ensures that all unused resolutions are dropped.
2018-05-16 17:28:11 -07:00
Eliza Weisman 4473fd114d proxy: Fix end events not firing when a stream is ended by a DATA frame (#957)
A recent upstream change in `tower-h2` (tower-rs/tower-h2@d9b3140) caused some
HTTP/2 streams that were previously terminated by TRAILERS frames to be 
terminated by empty DATA frames with the end of stream bit set, instead.

This broke some tests in my dev branch for #944, as our test server also uses
`tower-h2`, and some of the metrics tests were no longer seeing the expected
`StreamResponseEnd` events due to this change. This issue may also occur in
other cases, resulting in incorrect metrics.

This PR changes `MeasuredBody::poll_data` to trigger the Stream End event if
it sees a DATA frame that ends the stream.

Fixes #954 

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-16 16:24:29 -07:00
Oliver Gould b23ed6e651 rfc: proxy: Split `control::discovery` into submodules (#955)
While preparing #946, I was again struck by the `discovery` module being very weighty
(nearly 800 dense lines). The intent of this change is only to improve readability. There
are no functional changes. The following aesthetic changes have been made:

* `control::discovery` has been renamed to `control::destination` to be more consistent
  with the rest of conduit's terminology (destinations aren't the only thing that need to
  be discovered).
* In that vein, the `Discovery` type has been renamed `Resolver` (since it exposes one
  function, `resolve`).
* The `Watch` type has been renamed `Resolution`. This disambiguates the type form
  `futures_watch::Watch`(which is used in the same code) and makes it more clearly the
  product of a `Resolver`.
* The `Background` and `DiscoveryWork` names were very opaque.  `Background` is now
  `background::Config` to indicate that it can't actually _do_ anything; and
  `DiscoveryWork` is now `background::Process` to indicate that it's responsible for
  processing destination updates.
* `DestinationSet` is now a private implementation detail in the `background` module.
* An internal `ResolveRequest` type replaces an unnamed tuple (now that it's used across
  files).
* `rustfmt` has been run on `background.rs` and `endpoint.rs`
2018-05-15 17:23:01 -07:00
Eliza Weisman 86a75907ca proxy: Move absolute URI detection to `bind::Protocol` (#938)
This is in preparation for landing the Tokio upgrade. 

In the upcoming Hyper release, the handling of absolute form request URIs
moved from `hyper::Request` to the `hyper::client::connect::Connect` trait.
Once we upgrade to the new Tokio, we will have to upgrade our Hyper 
dependency as well.

Currently, Conduit detects whether the request URI is in absolute form in
`h1::normalize_our_view_of_uri` and adds an extension to the request if it is.
This will no longer work with the new Hyper, as that function is called from
the `bind::NormalizeUri` service, which is not constructed until after the 
client connection is established. Therefore, it is necessary to move this
information to `bind::Protocol`, so that it can be passed to 
`transparency::client::HyperConnect` (our implementation of Hyper's `Connect`
trait) when we are using the newest Hyper. 

For now, however, I've left in the `UriIsAbsoluteForm` extension and continued
to set it in  `h1::normalize_our_view_of_uri`, since we currently still use it
on the current Hyper version. I thought it was good to minimize the changes to
this existing code, as it will be removed when we migrate to the new Hyper.

This PR shouldn't cause any functional changes.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-15 13:16:58 -07:00
Carl Lerche be1b06fa22 Proxy: Update h2 dependency. (#948)
This also updates `bytes` as the newest version is required by `h2`.

Signed-off-by: Carl Lerche <me@carllerche.com>
2018-05-14 11:23:25 -07:00
Eliza Weisman 57c8504899 proxy: Add a lazy version of ThreadRng (#936)
This is in preparation for landing the Tokio upgrade. 

In order to be generic over Tokio's current thread and threadpool executors,
a number of types in Conduit which were not previously `Send` are now required
to be `Send`. A majority of this work will be done in the main Tokio upgrade
PR, as it is in many cases not possible to make these types `Send` _without_
using the new Tokio API (in order to remove `Handle`s, etc.); however, I'm
factoring out everything possible and trying to land it in separate PRs.

The p2c load balancer constructed in `Outbound` is currently parameterized
over a random number generator. We currently construct it by getting the 
thread-local RNG, and passing it to the load balancer constructor. However,
the thread-local RNG is not `Send`. I've fixed this issue by creating a new
zero-sized empty struct type which implements `rand::Rng` simply by calling
`thread_rng()` every time its' called, and passing that to 
`choose::power_of_two_choices` instead. Since this is an empty type which 
contains no data, and the correct thread-local RNG is accessed whenever
the methods are called, this new type can trivially be `Send`. According to
the `rand` crate's documentation, this is the correct way to use `ThreadRng`
anyway:
> Retrieve the lazily-initialized thread-local random number generator, seeded
> by the system. Intended to be used in method chaining style, e.g. 
> `thread_rng().gen::<i32>()`.
> (from https://docs.rs/rand/0.4.2/rand/fn.thread_rng.html)

This shouldn't lead to any functional changes.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-11 17:26:23 -07:00
Eliza Weisman 281281f5bc proxy: Make `outbound_updates_newer_services` test forward-compatible (#939)
This is in preparation for landing the Tokio upgrade.

The test `discovery::outbound_updates_newer_services` currently contains an
assertion that an HTTP/2 request to an HTTP/1 service will return a response
with status code 500. This is because the current version of Hyper on which
Conduit depends does not support protocol upgrades.

However, commit hyperium/hyper@bc6af88a32, which
adds support for this kind of protocol upgrade, was recently merged to Hyper's
master branch. Therefore, this assertion will no longer be correct once we 
depend on the upcoming Hyper release. When we migrate to the new Tokio, it will
be necessary to upgrade our Hyper dependency as well, and this test will fail.

I've modified the test to no longer make assertions about the response's status
code, so that it's compatible with both the current and future Hyper versions.
If the response is not `Ok`, the test will still fail, since 
`tests::support::Client::request()` `expect`s that the response is successful,
but the status code is ignored. I've added a comment in the test explaining 
this.

Eventually, when the master version of Conduit depends on the latest Hyper, we
may want to change this test to assert that the status code is 200 instead. We
may also want to add more tests for Hyper's protocol upgrade functionality, but
that seems out of scope for this PR.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-05-11 14:36:03 -07:00
Oliver Gould 030bd404fa proxy/router: Implement LRU cache eviction (#925)
The router's cache has no means to evict unused entries when capacity is
reached.

This change does the following:

- Wraps cache values in a smart pointer that tracks the last time of
  access for each entry. The smart pointer updates the access time when
  the reference to entry is dropped.
- When capacity is not available, all nodes that have not been accessed
  within some minimal idle age are dropped.

Accesses and updates to the map are O(1) when capacity is available.
Reclaiming capacity is O(n), so it's expected that the router is
configured with enough capacity such that capacity need not be reclaimed
usually.
2018-05-10 19:06:31 -07:00
Oliver Gould 1842418c97 proxy/router: Create a separate `cache` module (#920)
The router's `Inner` type contains a map of routes. Recently, this map's
capacity has become constrained to prevent leakage for long-running
processes.

This change prepares for a fuller LRU implementation by moving the
router's `Inner` type to a new (tested) module, `cache`.
2018-05-10 14:00:50 -07:00
Oliver Gould b238d97137 router: Store `recognize` outside of lock (#913)
The router stores its cache and `Recognize` implementation within a `Mutex`,
but there is no need for the recognizer to be locked.

This change creates a new `Cache` type that is locked independently of
`Recognize`. In order to accomplish this, `Recognize::bind_service` has
been changed to take an immutable reference to its `self`.

The (unused) `Single` type has been removed because it relied on
`bind_service` being mutable.
2018-05-10 11:51:01 -07:00