func checkAndPurgeMessages(conn *imap.Client, requests chan WorkRequest, checkRequests chan checkExistsRequest, wg *sync.WaitGroup) { defer wg.Done() timeout := time.NewTicker(NoopMinutes * time.Minute) done := false for { select { case request, ok := <-requests: if !ok { done = true break } // check and wait for response response := make(chan bool) cr := checkExistsRequest{UID: request.UID, MessageId: request.Value, Response: response} checkRequests <- cr // if response is false (does not exist), flag as Deleted if exists := <-response; !exists { log.Printf("not found in src. marking for deletion: %s", request.Value) err := AddDeletedFlag(conn, request.UID) if err != nil { log.Printf("Problems removing message from dst: %s", err.Error()) } } case <-timeout.C: imap.Wait(conn.Noop()) } if done { break } } log.Printf("expunging...") // expunge at the end allMsgs, _ := imap.NewSeqSet("") allMsgs.Add("1:*") imap.Wait(conn.Expunge(allMsgs)) log.Printf("expunge complete.") }