File: http_stream_pool_quic_attempt.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 6,617 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http/http_stream_pool_quic_attempt.h"

#include <memory>
#include <vector>

#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_id_helper.h"
#include "base/values.h"
#include "net/base/connection_endpoint_metadata.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_error_details.h"
#include "net/base/net_errors.h"
#include "net/base/tracing.h"
#include "net/dns/host_resolver.h"
#include "net/dns/public/host_resolver_results.h"
#include "net/http/http_network_session.h"
#include "net/http/http_stream_pool.h"
#include "net/http/http_stream_pool_attempt_manager.h"
#include "net/http/http_stream_pool_group.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_with_source.h"
#include "net/quic/quic_session_alias_key.h"
#include "net/quic/quic_session_key.h"
#include "net/quic/quic_session_pool.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"

namespace net {

HttpStreamPool::QuicAttempt::QuicAttempt(AttemptManager* manager,
                                         QuicEndpoint quic_endpoint)
    : manager_(manager),
      quic_endpoint_(std::move(quic_endpoint)),
      start_time_(base::TimeTicks::Now()),
      net_log_(NetLogWithSource::Make(
          manager->net_log().net_log(),
          NetLogSourceType::HTTP_STREAM_POOL_QUIC_ATTEMPT)),
      track_(base::trace_event::GetNextGlobalTraceId()),
      flow_(perfetto::Flow::ProcessScoped(
          base::trace_event::GetNextGlobalTraceId())) {
  CHECK(manager_);
  TRACE_EVENT_INSTANT("net.stream", "QuicAttemptStart", manager_->track(),
                      flow_);
  TRACE_EVENT_BEGIN("net.stream", "QuicAttempt::QuicAttempt", track_, flow_,
                    "ip_endpoint", quic_endpoint_.ip_endpoint.ToString());

  net_log_.BeginEvent(
      NetLogEventType::HTTP_STREAM_POOL_QUIC_ATTEMPT_ALIVE, [&] {
        base::Value::Dict dict;
        dict.Set("quic_version",
                 quic::ParsedQuicVersionToString(quic_endpoint_.quic_version));
        dict.Set("ip_endpoint", quic_endpoint_.ip_endpoint.ToString());
        dict.Set("metadata", quic_endpoint_.metadata.ToValue());
        manager_->net_log().source().AddToEventParameters(dict);
        return dict;
      });
  manager_->net_log().AddEventReferencingSource(
      NetLogEventType::HTTP_STREAM_POOL_ATTEMPT_MANAGER_QUIC_ATTEMPT_BOUND,
      net_log_.source());

  SSLConfig ssl_config;
  ssl_config.disable_cert_verification_network_fetches =
      stream_key().disable_cert_network_fetches();
  int cert_verify_flags = ssl_config.GetCertVerifyFlags();

  base::TimeTicks dns_resolution_start_time =
      manager_->dns_resolution_start_time();
  // The DNS resolution end time could be null when the resolution is still
  // ongoing. In that case, use the current time to make sure the connect
  // start time is already greater than the DNS resolution end time.
  base::TimeTicks dns_resolution_end_time =
      manager_->dns_resolution_end_time().is_null()
          ? base::TimeTicks::Now()
          : manager_->dns_resolution_end_time();

  std::set<std::string> dns_aliases =
      manager_->service_endpoint_request()->GetDnsAliasResults();

  session_attempt_ = GetQuicSessionPool()->CreateSessionAttempt(
      this, GetKey().session_key(), quic_endpoint_, cert_verify_flags,
      dns_resolution_start_time, dns_resolution_end_time,
      /*use_dns_aliases=*/true, std::move(dns_aliases),
      manager_->CalculateMultiplexedSessionCreationInitiator(),
      /*connection_management_config=*/std::nullopt);
}

HttpStreamPool::QuicAttempt::~QuicAttempt() {
  net_log_.EndEventWithNetErrorCode(
      NetLogEventType::HTTP_STREAM_POOL_QUIC_ATTEMPT_ALIVE,
      result_.value_or(ERR_ABORTED));
  TRACE_EVENT_END("net.stream", track_, "result",
                  result_.value_or(ERR_ABORTED));
  TRACE_EVENT_INSTANT("net.stream", "QuicAttemptEnd", manager_->track(), flow_);
}

void HttpStreamPool::QuicAttempt::Start() {
  if (GetTcpBasedAttemptDelayBehavior() ==
      TcpBasedAttemptDelayBehavior::kStartTimerOnFirstQuicAttempt) {
    manager_->MaybeRunTcpBasedAttemptDelayTimer();
  }

  int rv = session_attempt_->Start(base::BindOnce(
      &QuicAttempt::OnSessionAttemptComplete, weak_ptr_factory_.GetWeakPtr()));
  if (rv == ERR_IO_PENDING) {
    slow_timer_.Start(FROM_HERE, HttpStreamPool::GetConnectionAttemptDelay(),
                      base::BindOnce(&QuicAttempt::OnSessionAttemptSlow,
                                     base::Unretained(this)));
  } else {
    OnSessionAttemptComplete(rv);
  }
}

QuicSessionPool* HttpStreamPool::QuicAttempt::GetQuicSessionPool() {
  return manager_->group()->http_network_session()->quic_session_pool();
}

const QuicSessionAliasKey& HttpStreamPool::QuicAttempt::GetKey() {
  return manager_->group()->quic_session_alias_key();
}

const NetLogWithSource& HttpStreamPool::QuicAttempt::GetNetLog() {
  return net_log_;
}

base::Value::Dict HttpStreamPool::QuicAttempt::GetInfoAsValue() const {
  base::Value::Dict dict;
  dict.Set("quic_version",
           quic::ParsedQuicVersionToString(quic_endpoint_.quic_version));
  dict.Set("ip_endpoint", quic_endpoint_.ip_endpoint.ToString());
  base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_;
  dict.Set("elapsed_ms", static_cast<int>(elapsed.InMilliseconds()));
  if (result_.has_value()) {
    dict.Set("result", *result_);
  }
  return dict;
}

const HttpStreamKey& HttpStreamPool::QuicAttempt::stream_key() const {
  return manager_->group()->stream_key();
}

void HttpStreamPool::QuicAttempt::OnSessionAttemptSlow() {
  CHECK(!is_slow_);
  is_slow_ = true;
  manager_->OnQuicAttemptSlow();
}

void HttpStreamPool::QuicAttempt::OnSessionAttemptComplete(int rv) {
  slow_timer_.Stop();
  if (rv == OK) {
    if (!manager_->CanUseExistingQuicSession()) {
      // QUIC session is closed or marked broken before stream can be created.
      rv = ERR_CONNECTION_CLOSED;
    }
  }

  if (rv == OK &&
      !GetQuicSessionPool()->has_quic_ever_worked_on_current_network()) {
    GetQuicSessionPool()->set_has_quic_ever_worked_on_current_network(true);
  }

  result_ = rv;
  QuicAttemptOutcome outcome(rv);
  if (session_attempt_) {
    outcome.session = session_attempt_->session();
    session_attempt_->PopulateNetErrorDetails(&outcome.error_details);
  }
  session_attempt_.reset();
  manager_->OnQuicAttemptComplete(std::move(outcome));
  // `this` is deleted.
}

}  // namespace net