evictedqueue.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. package trace // import "go.opentelemetry.io/otel/sdk/trace"
  15. // evictedQueue is a FIFO queue with a configurable capacity.
  16. type evictedQueue struct {
  17. queue []interface{}
  18. capacity int
  19. droppedCount int
  20. }
  21. func newEvictedQueue(capacity int) evictedQueue {
  22. // Do not pre-allocate queue, do this lazily.
  23. return evictedQueue{capacity: capacity}
  24. }
  25. // add adds value to the evictedQueue eq. If eq is at capacity, the oldest
  26. // queued value will be discarded and the drop count incremented.
  27. func (eq *evictedQueue) add(value interface{}) {
  28. if eq.capacity == 0 {
  29. eq.droppedCount++
  30. return
  31. }
  32. if eq.capacity > 0 && len(eq.queue) == eq.capacity {
  33. // Drop first-in while avoiding allocating more capacity to eq.queue.
  34. copy(eq.queue[:eq.capacity-1], eq.queue[1:])
  35. eq.queue = eq.queue[:eq.capacity-1]
  36. eq.droppedCount++
  37. }
  38. eq.queue = append(eq.queue, value)
  39. }