@@ -99,6 +99,14 @@ class CurrentThread
9999 static void detachWriteResource ();
100100 static ResourceLink getWriteResourceLink ();
101101
102+ // For IO Throttling
103+ static void attachReadThrottler (const ThrottlerPtr & throttler);
104+ static void detachReadThrottler ();
105+ static ThrottlerPtr getReadThrottler ();
106+ static void attachWriteThrottler (const ThrottlerPtr & throttler);
107+ static void detachWriteThrottler ();
108+ static ThrottlerPtr getWriteThrottler ();
109+
102110 // / Initializes query with current thread as master thread in constructor, and detaches it in destructor
103111 struct QueryScope : private boost ::noncopyable
104112 {
@@ -111,37 +119,93 @@ class CurrentThread
111119 };
112120
113121 // / Scoped attach/detach of IO resource links
114- struct IOScope : private boost ::noncopyable
122+ struct IOSchedulingScope : private boost ::noncopyable
115123 {
116- explicit IOScope (ResourceLink read_resource_link, ResourceLink write_resource_link)
124+ IOSchedulingScope (ResourceLink read_resource_link, ResourceLink write_resource_link)
125+ {
126+ readResource (read_resource_link);
127+ writeResource (write_resource_link);
128+ }
129+
130+ explicit IOSchedulingScope (const IOSchedulingSettings & settings)
131+ : IOSchedulingScope(settings.read_resource_link, settings.write_resource_link)
132+ {}
133+
134+ ~IOSchedulingScope ()
135+ {
136+ if (read_resource_attached)
137+ detachReadResource ();
138+ if (write_resource_attached)
139+ detachWriteResource ();
140+ }
141+
142+ private:
143+ void readResource (ResourceLink link)
117144 {
118- if (read_resource_link )
145+ if (link )
119146 {
120- attachReadResource (read_resource_link );
121- read_attached = true ;
147+ attachReadResource (link );
148+ read_resource_attached = true ;
122149 }
123- if (write_resource_link)
150+ }
151+
152+ void writeResource (ResourceLink link)
153+ {
154+ if (link)
124155 {
125- attachWriteResource (write_resource_link );
126- write_attached = true ;
156+ attachWriteResource (link );
157+ write_resource_attached = true ;
127158 }
128159 }
129160
130- explicit IOScope ( const IOSchedulingSettings & settings)
131- : IOScope(settings.read_resource_link, settings.write_resource_link)
132- {}
161+ bool read_resource_attached = false ;
162+ bool write_resource_attached = false ;
163+ };
133164
134- ~IOScope ()
165+ // / Scoped attach/detach of read throttler
166+ struct ReadThrottlingScope : private boost ::noncopyable
167+ {
168+ explicit ReadThrottlingScope (const ThrottlerPtr & read_throttler_)
135169 {
136- if (read_attached)
137- detachReadResource ();
138- if (write_attached)
139- detachWriteResource ();
170+ if (read_throttler_)
171+ {
172+ attachReadThrottler (read_throttler_);
173+ read_throttler_attached = true ;
174+ }
175+ }
176+
177+ ~ReadThrottlingScope ()
178+ {
179+ if (read_throttler_attached)
180+ detachReadThrottler ();
181+ }
182+
183+ private:
184+ bool read_throttler_attached = false ;
185+ };
186+
187+ // / Scoped attach/detach of write throttler
188+ struct WriteThrottlingScope : private boost ::noncopyable
189+ {
190+ explicit WriteThrottlingScope (const ThrottlerPtr & write_throttler_)
191+ {
192+ if (write_throttler_)
193+ {
194+ attachWriteThrottler (write_throttler_);
195+ write_throttler_attached = true ;
196+ }
140197 }
141198
142- bool read_attached = false ;
143- bool write_attached = false ;
199+ ~WriteThrottlingScope ()
200+ {
201+ if (write_throttler_attached)
202+ detachWriteThrottler ();
203+ }
204+
205+ private:
206+ bool write_throttler_attached = false ;
144207 };
208+
145209};
146210
147211}
0 commit comments