span_allocator.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2019 The Jaeger 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 jaeger
  15. import "sync"
  16. // SpanAllocator abstraction of managing span allocations
  17. type SpanAllocator interface {
  18. Get() *Span
  19. Put(*Span)
  20. }
  21. type syncPollSpanAllocator struct {
  22. spanPool sync.Pool
  23. }
  24. func newSyncPollSpanAllocator() SpanAllocator {
  25. return &syncPollSpanAllocator{
  26. spanPool: sync.Pool{New: func() interface{} {
  27. return &Span{}
  28. }},
  29. }
  30. }
  31. func (pool *syncPollSpanAllocator) Get() *Span {
  32. return pool.spanPool.Get().(*Span)
  33. }
  34. func (pool *syncPollSpanAllocator) Put(span *Span) {
  35. span.reset()
  36. pool.spanPool.Put(span)
  37. }
  38. type simpleSpanAllocator struct{}
  39. func (pool simpleSpanAllocator) Get() *Span {
  40. return &Span{}
  41. }
  42. func (pool simpleSpanAllocator) Put(span *Span) {
  43. // @comment https://github.com/jaegertracing/jaeger-client-go/pull/381#issuecomment-475904351
  44. // since finished spans are not reused, no need to reset them
  45. // span.reset()
  46. }