]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.h
adc786e8208cb1845dc13635d29a86cb1de510a9
[thirdparty/squid.git] / src / MemObject.h
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_MEMOBJECT_H
10 #define SQUID_MEMOBJECT_H
11
12 #include "base/DelayedAsyncCalls.h"
13 #include "dlink.h"
14 #include "http/RequestMethod.h"
15 #include "RemovalPolicy.h"
16 #include "SquidString.h"
17 #include "stmem.h"
18 #include "store/forward.h"
19 #include "StoreIOBuffer.h"
20 #include "StoreIOState.h"
21 #include "typedefs.h" //for IRCB
22
23 #if USE_DELAY_POOLS
24 #include "DelayId.h"
25 #endif
26
27 typedef void STMCB (void *data, StoreIOBuffer wroteBuffer);
28
29 class store_client;
30 class PeerSelector;
31
32 class MemObject
33 {
34 MEMPROXY_CLASS(MemObject);
35
36 public:
37 static size_t inUseCount();
38
39 void dump() const;
40 MemObject();
41 ~MemObject();
42
43 /// Sets store ID, log URI, and request method (unless already set). Does
44 /// not clobber the method so that, say, a HEAD hit for a GET entry keeps
45 /// the GET method that matches the entry key. Same for the other parts of
46 /// the trio because the entry filling code may expect them to be constant.
47 /// XXX: Avoid this method. We plan to remove it and make the trio constant
48 /// after addressing the XXX in MemStore::get().
49 void setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod);
50
51 /// whether setUris() has been called
52 bool hasUris() const;
53
54 void write(const StoreIOBuffer &buf);
55 void unlinkRequest() { request = nullptr; }
56
57 /// HTTP response before 304 (Not Modified) updates
58 /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply()
59 const HttpReply &baseReply() const { return *reply_; }
60
61 /// \returns nil -- if no 304 updates since replaceBaseReply()
62 /// \returns combination of baseReply() and 304 updates -- after updates
63 const HttpReplyPointer &updatedReply() const { return updatedReply_; }
64
65 /// \returns the updated-by-304(s) response (if it exists)
66 /// \returns baseReply() (otherwise)
67 const HttpReply &freshestReply() const {
68 if (updatedReply_)
69 return *updatedReply_;
70 else
71 return baseReply();
72 }
73
74 /// \returns writable base reply for parsing and other initial modifications
75 /// Base modifications can only be done when forming/loading the entry.
76 /// After that, use replaceBaseReply() to reset all of the replies.
77 HttpReply &adjustableBaseReply();
78
79 /// (re)sets base reply, usually just replacing the initial/empty object
80 /// also forgets the updated reply (if any)
81 void replaceBaseReply(const HttpReplyPointer &r);
82
83 /// (re)sets updated reply; \see updatedReply()
84 void updateReply(const HttpReply &r) { updatedReply_ = &r; }
85
86 /// reflects past Controller::updateOnNotModified(old, e304) calls:
87 /// for HTTP 304 entries: whether our entry was used as "e304"
88 /// for other entries: whether our entry was updated as "old"
89 bool appliedUpdates = false;
90
91 void stat (MemBuf * mb) const;
92 int64_t endOffset () const;
93
94 /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset()
95 void markEndOfReplyHeaders();
96
97 /// negative if unknown; otherwise, expected object_sz, expected endOffset
98 /// maximum, and stored reply headers+body size (all three are the same)
99 int64_t expectedReplySize() const;
100 int64_t size() const;
101 void reset();
102 int64_t lowestMemReaderOffset() const;
103 bool readAheadPolicyCanRead() const;
104 void addClient(store_client *);
105 /* XXX belongs in MemObject::swapout, once swaphdrsz is managed
106 * better
107 */
108 int64_t objectBytesOnDisk() const;
109 int64_t policyLowestOffsetToKeep(bool swap) const;
110 int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet
111 void trimSwappable();
112 void trimUnSwappable();
113 bool isContiguous() const;
114 int mostBytesWanted(int max, bool ignoreDelayPools) const;
115 void setNoDelay(bool const newValue);
116 #if USE_DELAY_POOLS
117 DelayId mostBytesAllowed() const;
118 #endif
119
120 #if URL_CHECKSUM_DEBUG
121
122 void checkUrlChecksum() const;
123 #endif
124
125 /// Before StoreID, code assumed that MemObject stores Request URI.
126 /// After StoreID, some old code still incorrectly assumes that.
127 /// Use this method to mark that incorrect assumption.
128 const char *urlXXX() const { return storeId(); }
129
130 /// Entry StoreID (usually just Request URI); if a buggy code requests this
131 /// before the information is available, returns an "[unknown_URI]" string.
132 const char *storeId() const;
133
134 /// client request URI used for logging; storeId() by default
135 const char *logUri() const;
136
137 HttpRequestMethod method;
138 mem_hdr data_hdr;
139 int64_t inmem_lo = 0;
140 dlink_list clients;
141
142 size_t clientCount() const {return nclients;}
143
144 bool clientIsFirst(void *sc) const {return (clients.head && sc == clients.head->data);}
145
146 int nclients = 0;
147
148 class SwapOut
149 {
150 public:
151 int64_t queue_offset = 0; ///< number of bytes sent to SwapDir for writing
152 StoreIOState::Pointer sio;
153
154 /// Decision states for StoreEntry::swapoutPossible() and related code.
155 typedef enum { swNeedsCheck = 0, swImpossible = -1, swPossible = +1, swStarted } Decision;
156 Decision decision = swNeedsCheck; ///< current decision state
157 };
158
159 SwapOut swapout;
160
161 /* TODO: Remove this change-minimizing hack */
162 using Io = Store::IoStatus;
163 static constexpr Io ioUndecided = Store::ioUndecided;
164 static constexpr Io ioReading = Store::ioReading;
165 static constexpr Io ioWriting = Store::ioWriting;
166 static constexpr Io ioDone = Store::ioDone;
167
168 /// State of an entry with regards to the [shared] in-transit table.
169 class XitTable
170 {
171 public:
172 int32_t index = -1; ///< entry position inside the in-transit table
173 Io io = ioUndecided; ///< current I/O state
174 };
175 XitTable xitTable; ///< current [shared] memory caching state for the entry
176
177 /// State of an entry with regards to the [shared] memory caching.
178 class MemCache
179 {
180 public:
181 int32_t index = -1; ///< entry position inside the memory cache
182 int64_t offset = 0; ///< bytes written/read to/from the memory cache so far
183
184 Io io = ioUndecided; ///< current I/O state
185 };
186 MemCache memCache; ///< current [shared] memory caching state for the entry
187
188 HttpRequestPointer request;
189
190 struct timeval start_ping;
191 IRCB *ping_reply_callback;
192 PeerSelector *ircb_data = nullptr;
193
194 /// used for notifying StoreEntry writers about 3rd-party initiated aborts
195 AsyncCallPointer abortCallback;
196 RemovalPolicyNode repl;
197 int id = 0;
198 int64_t object_sz = -1;
199 size_t swap_hdr_sz = 0;
200 #if URL_CHECKSUM_DEBUG
201 unsigned int chksum = 0;
202 #endif
203
204 SBuf vary_headers;
205
206 void delayRead(const AsyncCallPointer &);
207 void kickReads();
208
209 private:
210 HttpReplyPointer reply_; ///< \see baseReply()
211 HttpReplyPointer updatedReply_; ///< \see updatedReply()
212
213 mutable String storeId_; ///< StoreId for our entry (usually request URI)
214 mutable String logUri_; ///< URI used for logging (usually request URI)
215
216 DelayedAsyncCalls deferredReads;
217 };
218
219 /** global current memory removal policy */
220 extern RemovalPolicy *mem_policy;
221
222 #endif /* SQUID_MEMOBJECT_H */
223