@@ -96,7 +96,7 @@ func (w *Watcher) watchNewLogs() {
9696 }
9797}
9898
99- // updateLogs checks the transparency log list for new logs and adds new 0workers for those to the watcher.
99+ // updateLogs checks the transparency log list for new logs and adds new workers for those to the watcher.
100100func (w * Watcher ) updateLogs () {
101101 // Get a list of urls of all CT logs
102102 logList , err := getAllLogs ()
@@ -105,78 +105,90 @@ func (w *Watcher) updateLogs() {
105105 return
106106 }
107107
108- w .addNewlyAvailableLogs (logList )
109-
110- if * config .AppConfig .General .DropOldLogs {
111- w .dropRemovedLogs (logList )
112- }
113- }
114-
115- // addNewlyAvailableLogs checks the transparency log list for new Log servers and adds workers for those to the watcher.
116- func (w * Watcher ) addNewlyAvailableLogs (logList loglist3.LogList ) {
117108 log .Println ("Checking for new ct logs..." )
118109
110+ // Track all URLs that should be monitored after reconciliation
111+ monitoredURLs := make (map [string ]struct {})
112+ newCTs := 0
113+
119114 w .workersMu .Lock ()
120115 defer w .workersMu .Unlock ()
121- newCTs := 0
122116
123- // Check the ct log list for new, unwatched logs
124- // For each CT log, create a worker and start downloading certs
125117 for _ , operator := range logList .Operators {
126118 // Iterate over each log of the operator
127119 for _ , transparencyLog := range operator .Logs {
128- newURL := normalizeCtlogURL (transparencyLog .URL )
120+ url := transparencyLog .URL
121+ desc := transparencyLog .Description
122+ normURL := normalizeCtlogURL (url )
129123
130124 if transparencyLog .State .LogStatus () == loglist3 .RetiredLogStatus {
131- log .Printf ("Skipping retired CT log: %s\n " , newURL )
125+ log .Printf ("Skipping retired CT log: %s\n " , normURL )
132126 continue
133127 }
134128
135- // Check if the log is already being watched
136- alreadyWatched := false
137-
138- for _ , ctWorker := range w .workers {
139- workerURL := normalizeCtlogURL (ctWorker .ctURL )
140- if workerURL == newURL {
141- alreadyWatched = true
142- break
143- }
129+ monitoredURLs [normURL ] = struct {}{}
130+ if w .addLogIfNew (operator .Name , desc , url ) {
131+ newCTs ++
144132 }
133+ }
134+ }
145135
146- // If the log is already being watched, continue
147- if alreadyWatched {
148- continue
149- }
136+ log .Printf ("New ct logs found: %d\n " , newCTs )
150137
151- w .wg .Add (1 )
152- newCTs ++
153-
154- // Metrics are initialized with 0.
155- // Only if recovery is enabled, it is initialized with the last saved index.
156- lastCTIndex := metrics .GetCTIndex (normalizeCtlogURL (transparencyLog .URL ))
157- ctWorker := worker {
158- name : transparencyLog .Description ,
159- operatorName : operator .Name ,
160- ctURL : transparencyLog .URL ,
161- entryChan : w .certChan ,
162- ctIndex : lastCTIndex ,
138+ // Optionally stop workers for logs not in the monitoredURLs set
139+ if * config .AppConfig .General .DropOldLogs {
140+ removed := 0
141+ for _ , ctWorker := range w .workers {
142+ normURL := normalizeCtlogURL (ctWorker .ctURL )
143+ if _ , ok := monitoredURLs [normURL ]; ! ok {
144+ log .Printf ("Stopping worker. CT URL not found in LogList or retired: '%s'\n " , ctWorker .ctURL )
145+ ctWorker .stop ()
146+ removed ++
163147 }
164- w .workers = append (w .workers , & ctWorker )
165- metrics .Init (operator .Name , normalizeCtlogURL (transparencyLog .URL ))
166-
167- // Start a goroutine for each worker
168- go func () {
169- defer w .wg .Done ()
170- ctWorker .startDownloadingCerts (w .context )
171- w .discardWorker (& ctWorker )
172- }()
173148 }
149+ log .Printf ("Removed ct logs: %d\n " , removed )
174150 }
175151
176- log .Printf ("New ct logs found: %d\n " , newCTs )
177152 log .Printf ("Currently monitored ct logs: %d\n " , len (w .workers ))
178153}
179154
155+ // addLogIfNew checks if a log is already being watched and adds it if not.
156+ // Returns true if a new log was added, false otherwise.
157+ func (w * Watcher ) addLogIfNew (operatorName , description , url string ) bool {
158+ normURL := normalizeCtlogURL (url )
159+
160+ // Check if the log is already being watched
161+ for _ , ctWorker := range w .workers {
162+ workerURL := normalizeCtlogURL (ctWorker .ctURL )
163+ if workerURL == normURL {
164+ return false
165+ }
166+ }
167+
168+ // Log is not being watched, so add it
169+ w .wg .Add (1 )
170+
171+ lastCTIndex := metrics .GetCTIndex (normURL )
172+ ctWorker := worker {
173+ name : description ,
174+ operatorName : operatorName ,
175+ ctURL : url ,
176+ entryChan : w .certChan ,
177+ ctIndex : lastCTIndex ,
178+ }
179+ w .workers = append (w .workers , & ctWorker )
180+ metrics .Init (operatorName , normURL )
181+
182+ // Start a goroutine for each worker
183+ go func () {
184+ defer w .wg .Done ()
185+ ctWorker .startDownloadingCerts (w .context )
186+ w .discardWorker (& ctWorker )
187+ }()
188+
189+ return true
190+ }
191+
180192// discardWorker removes a worker from the watcher's list of workers.
181193// This needs to be done when a worker stops.
182194func (w * Watcher ) discardWorker (worker * worker ) {
@@ -193,67 +205,6 @@ func (w *Watcher) discardWorker(worker *worker) {
193205 }
194206}
195207
196- // dropRemovedLogs checks if any of the currently monitored logs are no longer in the log list or are retired.
197- // If they are not, the CT Logs are probably no longer relevant and the corresponding workers will be stopped.
198- func (w * Watcher ) dropRemovedLogs (logList loglist3.LogList ) {
199- removedCTs := 0
200-
201- // Iterate over all workers and check if they are still in the logList
202- // If they are not, the CT Logs are probably no longer relevant.
203- // We should stop the worker if that didn't already happen.
204- for _ , ctWorker := range w .workers {
205- workerURL := normalizeCtlogURL (ctWorker .ctURL )
206-
207- onLogList := false
208- for _ , operator := range logList .Operators {
209- if ctWorker .operatorName != operator .Name {
210- // This operator is not the one we're looking for
211- continue
212- }
213-
214- // Iterate over each log of the operator
215- for _ , transparencyLog := range operator .Logs {
216- // Remove retired logs from the list
217- if transparencyLog .State .LogStatus () == loglist3 .RetiredLogStatus {
218- // Skip retired logs
219- continue
220- }
221-
222- // Check if the log is already being watched
223- logListURL := normalizeCtlogURL (transparencyLog .URL )
224- if workerURL == logListURL {
225- onLogList = true
226- break
227- }
228- }
229-
230- // Prevent further loop iterations
231- if onLogList {
232- break
233- }
234- }
235-
236- // Make sure to not drop logs that are defined locally in the additional logs list
237- for _ , additionalLogConfig := range config .AppConfig .General .AdditionalLogs {
238- additionalLogListURL := normalizeCtlogURL (additionalLogConfig .URL )
239- if workerURL == additionalLogListURL {
240- onLogList = true
241- break
242- }
243- }
244-
245- // If the log is not in the loglist, stop the worker
246- if ! onLogList {
247- log .Printf ("Stopping worker. CT URL not found in LogList or retired: '%s'\n " , ctWorker .ctURL )
248- removedCTs ++
249- ctWorker .stop ()
250- }
251- }
252-
253- log .Printf ("Removed ct logs: %d\n " , removedCTs )
254- log .Printf ("Currently monitored ct logs: %d\n " , len (w .workers ))
255- }
256-
257208// Stop stops the watcher.
258209func (w * Watcher ) Stop () {
259210 log .Printf ("Stopping watcher\n " )
0 commit comments