blob: 39d828a9d83742800bbe58e9e49479d93a3228b3 [file] [log] [blame]
QUICHE team82dee2f2019-01-18 12:35:12 -05001// Copyright (c) 2016 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
5#ifndef QUICHE_SPDY_CORE_WRITE_SCHEDULER_H_
6#define QUICHE_SPDY_CORE_WRITE_SCHEDULER_H_
7
8#include <cstdint>
bnc44712912019-08-15 18:58:14 -07009#include <string>
QUICHE team82dee2f2019-01-18 12:35:12 -050010#include <tuple>
11#include <vector>
12
bnc45ccf4b2020-01-21 19:05:27 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_export.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050014#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050015
16namespace spdy {
17
18// Abstract superclass for classes that decide which SPDY or HTTP/2 stream to
19// write next. Concrete subclasses implement various scheduling policies:
20//
21// PriorityWriteScheduler: implements SPDY priority-based stream scheduling,
22// where (writable) higher-priority streams are always given precedence
23// over lower-priority streams.
24//
25// Http2PriorityWriteScheduler: implements SPDY priority-based stream
26// scheduling coupled with the HTTP/2 stream dependency model. This is only
27// intended as a transitional step towards Http2WeightedWriteScheduler.
28//
29// Http2WeightedWriteScheduler (coming soon): implements the HTTP/2 stream
30// dependency model with weighted stream scheduling, fully conforming to
31// RFC 7540.
32//
33// The type used to represent stream IDs (StreamIdType) is templated in order
34// to allow for use by both SPDY and QUIC codebases. It must be a POD that
35// supports comparison (i.e., a numeric type).
36//
37// Each stream can be in one of two states: ready or not ready (for writing).
38// Ready state is changed by calling the MarkStreamReady() and
39// MarkStreamNotReady() methods. Only streams in the ready state can be
40// returned by PopNextReadyStream(); when returned by that method, the stream's
41// state changes to not ready.
42template <typename StreamIdType>
bnc45ccf4b2020-01-21 19:05:27 -080043class QUICHE_EXPORT_PRIVATE WriteScheduler {
QUICHE team82dee2f2019-01-18 12:35:12 -050044 public:
45 typedef StreamPrecedence<StreamIdType> StreamPrecedenceType;
46
47 virtual ~WriteScheduler() {}
48
49 // Registers new stream |stream_id| with the scheduler, assigning it the
50 // given precedence. If the scheduler supports stream dependencies, the
51 // stream is inserted into the dependency tree under
52 // |precedence.parent_id()|.
53 //
54 // Preconditions: |stream_id| should be unregistered, and
55 // |precedence.parent_id()| should be registered or |kHttp2RootStreamId|.
56 virtual void RegisterStream(StreamIdType stream_id,
57 const StreamPrecedenceType& precedence) = 0;
58
59 // Unregisters the given stream from the scheduler, which will no longer keep
60 // state for it.
61 //
62 // Preconditions: |stream_id| should be registered.
63 virtual void UnregisterStream(StreamIdType stream_id) = 0;
64
65 // Returns true if the given stream is currently registered.
66 virtual bool StreamRegistered(StreamIdType stream_id) const = 0;
67
68 // Returns the precedence of the specified stream. If the scheduler supports
69 // stream dependencies, calling |parent_id()| on the return value returns the
70 // stream's parent, and calling |exclusive()| returns true iff the specified
71 // stream is an only child of the parent stream.
72 //
73 // Preconditions: |stream_id| should be registered.
74 virtual StreamPrecedenceType GetStreamPrecedence(
75 StreamIdType stream_id) const = 0;
76
77 // Updates the precedence of the given stream. If the scheduler supports
78 // stream dependencies, |stream_id|'s parent will be updated to be
79 // |precedence.parent_id()| if it is not already.
80 //
81 // Preconditions: |stream_id| should be unregistered, and
82 // |precedence.parent_id()| should be registered or |kHttp2RootStreamId|.
83 virtual void UpdateStreamPrecedence(
84 StreamIdType stream_id,
85 const StreamPrecedenceType& precedence) = 0;
86
87 // Returns child streams of the given stream, if any. If the scheduler
88 // doesn't support stream dependencies, returns an empty vector.
89 //
90 // Preconditions: |stream_id| should be registered.
91 virtual std::vector<StreamIdType> GetStreamChildren(
92 StreamIdType stream_id) const = 0;
93
94 // Records time (in microseconds) of a read/write event for the given
95 // stream.
96 //
97 // Preconditions: |stream_id| should be registered.
98 virtual void RecordStreamEventTime(StreamIdType stream_id,
99 int64_t now_in_usec) = 0;
100
101 // Returns time (in microseconds) of the last read/write event for a stream
102 // with higher priority than the priority of the given stream, or 0 if there
103 // is no such event.
104 //
105 // Preconditions: |stream_id| should be registered.
106 virtual int64_t GetLatestEventWithPrecedence(
107 StreamIdType stream_id) const = 0;
108
109 // If the scheduler has any ready streams, returns the next scheduled
110 // ready stream, in the process transitioning the stream from ready to not
111 // ready.
112 //
113 // Preconditions: |HasReadyStreams() == true|
114 virtual StreamIdType PopNextReadyStream() = 0;
115
116 // If the scheduler has any ready streams, returns the next scheduled
117 // ready stream and its priority, in the process transitioning the stream from
118 // ready to not ready.
119 //
120 // Preconditions: |HasReadyStreams() == true|
121 virtual std::tuple<StreamIdType, StreamPrecedenceType>
122 PopNextReadyStreamAndPrecedence() = 0;
123
124 // Returns true if there's another stream ahead of the given stream in the
125 // scheduling queue. This function can be called to see if the given stream
126 // should yield work to another stream.
127 //
128 // Preconditions: |stream_id| should be registered.
129 virtual bool ShouldYield(StreamIdType stream_id) const = 0;
130
131 // Marks the stream as ready to write. If the stream was already ready, does
132 // nothing. If add_to_front is true, the stream is scheduled ahead of other
133 // streams of the same priority/weight, otherwise it is scheduled behind them.
134 //
135 // Preconditions: |stream_id| should be registered.
136 virtual void MarkStreamReady(StreamIdType stream_id, bool add_to_front) = 0;
137
138 // Marks the stream as not ready to write. If the stream is not registered or
139 // not ready, does nothing.
140 //
141 // Preconditions: |stream_id| should be registered.
142 virtual void MarkStreamNotReady(StreamIdType stream_id) = 0;
143
144 // Returns true iff the scheduler has any ready streams.
145 virtual bool HasReadyStreams() const = 0;
146
147 // Returns the number of streams currently marked ready.
148 virtual size_t NumReadyStreams() const = 0;
149
fayangb7edbb82019-07-23 06:08:00 -0700150 // Returns true if stream with |stream_id| is ready.
151 virtual bool IsStreamReady(StreamIdType stream_id) const = 0;
152
fayang753002d2019-07-24 11:54:43 -0700153 // Returns the number of registered streams.
154 virtual size_t NumRegisteredStreams() const = 0;
155
QUICHE team82dee2f2019-01-18 12:35:12 -0500156 // Returns summary of internal state, for logging/debugging.
bnc44712912019-08-15 18:58:14 -0700157 virtual std::string DebugString() const = 0;
QUICHE team82dee2f2019-01-18 12:35:12 -0500158};
159
160} // namespace spdy
161
162#endif // QUICHE_SPDY_CORE_WRITE_SCHEDULER_H_