evictedqueue.go (1475B)
1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package trace // import "go.opentelemetry.io/otel/sdk/trace" 16 17 // evictedQueue is a FIFO queue with a configurable capacity. 18 type evictedQueue struct { 19 queue []interface{} 20 capacity int 21 droppedCount int 22 } 23 24 func newEvictedQueue(capacity int) evictedQueue { 25 // Do not pre-allocate queue, do this lazily. 26 return evictedQueue{capacity: capacity} 27 } 28 29 // add adds value to the evictedQueue eq. If eq is at capacity, the oldest 30 // queued value will be discarded and the drop count incremented. 31 func (eq *evictedQueue) add(value interface{}) { 32 if eq.capacity == 0 { 33 eq.droppedCount++ 34 return 35 } 36 37 if eq.capacity > 0 && len(eq.queue) == eq.capacity { 38 // Drop first-in while avoiding allocating more capacity to eq.queue. 39 copy(eq.queue[:eq.capacity-1], eq.queue[1:]) 40 eq.queue = eq.queue[:eq.capacity-1] 41 eq.droppedCount++ 42 } 43 eq.queue = append(eq.queue, value) 44 }