@@ -104,38 +104,24 @@ impl Inner {
104
104
let task_id = TaskId :: from ( & service) ;
105
105
let group = self . services . entry ( task_id) . or_default ( ) ;
106
106
107
- // Cleanup group, evicting all terminated services, while we're at it.
108
- group. instances . retain ( |s| !s. handle . is_finished ( ) ) ;
109
-
110
- let id = ServiceId {
111
- task : task_id,
112
- instance_id : group. next_instance_id ,
113
- } ;
114
- group. next_instance_id += 1 ;
115
-
116
107
// Services are allowed to process as much work as possible before yielding to other,
117
108
// lower priority tasks. We want to prioritize service backlogs over creating more work
118
109
// for these services.
119
110
let future = tokio:: task:: unconstrained ( service. future ) ;
120
111
let future = ServiceMonitor :: wrap ( future) ;
121
112
let metrics = Arc :: clone ( future. metrics ( ) ) ;
122
113
123
- let jh = crate :: runtime:: spawn_in ( handle, task_id, future) ;
124
- let ( sjh , sjhe ) = crate :: service:: status:: split ( jh ) ;
114
+ let task_handle = crate :: runtime:: spawn_in ( handle, task_id, future) ;
115
+ let ( status_handle , handle ) = crate :: service:: status:: split ( task_handle ) ;
125
116
126
- let service = ServiceInstance {
127
- instance_id : id. instance_id ,
128
- metrics,
129
- handle : sjh,
130
- } ;
131
- group. instances . push ( service) ;
117
+ group. add ( metrics, status_handle) ;
132
118
133
- sjhe
119
+ handle
134
120
}
135
121
136
122
fn metrics ( & self ) -> impl Iterator < Item = ( ServiceId , ServiceMetrics ) > + ' _ {
137
123
self . services . iter ( ) . flat_map ( |( task_id, group) | {
138
- group. instances . iter ( ) . map ( |service| {
124
+ group. iter ( ) . map ( |service| {
139
125
let id = ServiceId {
140
126
task : * task_id,
141
127
instance_id : service. instance_id ,
@@ -176,6 +162,30 @@ struct ServiceGroup {
176
162
instances : Vec < ServiceInstance > ,
177
163
}
178
164
165
+ impl ServiceGroup {
166
+ /// Adds a started service to the service group.
167
+ pub fn add ( & mut self , metrics : Arc < RawMetrics > , handle : ServiceStatusJoinHandle ) {
168
+ // Cleanup the group, evicting all finished services, while we're at it.
169
+ self . instances . retain ( |s| !s. handle . is_finished ( ) ) ;
170
+
171
+ let instance_id = self . next_instance_id ;
172
+ self . next_instance_id += 1 ;
173
+
174
+ let service = ServiceInstance {
175
+ instance_id,
176
+ metrics,
177
+ handle,
178
+ } ;
179
+
180
+ self . instances . push ( service) ;
181
+ }
182
+
183
+ /// Returns an iterator over all currently alive services.
184
+ pub fn iter ( & self ) -> impl Iterator < Item = & ServiceInstance > {
185
+ self . instances . iter ( ) . filter ( |s| !s. handle . is_finished ( ) )
186
+ }
187
+ }
188
+
179
189
/// Collection of metadata the registry tracks per service instance.
180
190
#[ derive( Debug ) ]
181
191
struct ServiceInstance {
0 commit comments