github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/logs/noaa_message_queue.go (about) 1 package logs 2 3 import ( 4 "sort" 5 "sync" 6 7 "github.com/cloudfoundry/sonde-go/events" 8 ) 9 10 type NoaaMessageQueue struct { 11 messages []*events.LogMessage 12 mutex sync.Mutex 13 } 14 15 func NewNoaaMessageQueue() *NoaaMessageQueue { 16 return &NoaaMessageQueue{} 17 } 18 19 func (pq *NoaaMessageQueue) PushMessage(message *events.LogMessage) { 20 pq.mutex.Lock() 21 defer pq.mutex.Unlock() 22 23 pq.messages = append(pq.messages, message) 24 } 25 26 // implement sort interface so we can sort messages as we receive them in PushMessage 27 func (pq *NoaaMessageQueue) Less(i, j int) bool { 28 return *pq.messages[i].Timestamp < *pq.messages[j].Timestamp 29 } 30 31 func (pq *NoaaMessageQueue) Swap(i, j int) { 32 pq.messages[i], pq.messages[j] = pq.messages[j], pq.messages[i] 33 } 34 35 func (pq *NoaaMessageQueue) Len() int { 36 return len(pq.messages) 37 } 38 39 func (pq *NoaaMessageQueue) EnumerateAndClear(onMessage func(*events.LogMessage)) { 40 pq.mutex.Lock() 41 defer pq.mutex.Unlock() 42 43 sort.Stable(pq) 44 45 for _, x := range pq.messages { 46 onMessage(x) 47 } 48 49 pq.messages = []*events.LogMessage{} 50 }