Skip to content

Commit

Permalink
Revert "pre-define map capacities (#197)"
Browse files Browse the repository at this point in the history
This reverts commit e707bf5.
  • Loading branch information
kentquirk authored Oct 17, 2022
1 parent 2d49571 commit c9ce44d
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions libhoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,9 @@ func Flush() {
// Contrary to its name, SendNow does not block and send data
// immediately, but only enqueues to be sent asynchronously.
// It is equivalent to:
//
// ev := libhoney.NewEvent()
// ev.Add(data)
// ev.Send()
// ev := libhoney.NewEvent()
// ev.Add(data)
// ev.Send()
//
// Deprecated: SendNow is deprecated and may be removed in a future major release.
func SendNow(data interface{}) error {
Expand Down Expand Up @@ -845,8 +844,8 @@ func (e *Event) SendPresampled() (err error) {
e.sendLock.Lock()
defer e.sendLock.Unlock()

e.lock.RLock()
defer e.lock.RUnlock()
e.fieldHolder.lock.RLock()
defer e.fieldHolder.lock.RUnlock()
if len(e.data) == 0 {
return errors.New("No metrics added to event. Won't send empty event.")
}
Expand Down Expand Up @@ -923,10 +922,9 @@ func (b *Builder) AddDynamicField(name string, fn func() interface{}) error {
// Contrary to its name, SendNow does not block and send data
// immediately, but only enqueues to be sent asynchronously.
// It is equivalent to:
//
// ev := builder.NewEvent()
// ev.Add(data)
// ev.Send()
// ev := builder.NewEvent()
// ev.Add(data)
// ev.Send()
//
// Deprecated: SendNow is deprecated and may be removed in a future major release.
func (b *Builder) SendNow(data interface{}) error {
Expand All @@ -949,23 +947,18 @@ func (b *Builder) NewEvent() *Event {
Timestamp: time.Now(),
client: b.client,
}
// Set up locks
e.data = make(map[string]interface{})

b.lock.RLock()
defer b.lock.RUnlock()
b.dynFieldsLock.RLock()
defer b.dynFieldsLock.RUnlock()
e.lock.Lock()
defer e.lock.Unlock()

e.data = make(map[string]interface{}, len(b.data)+len(b.dynFields))
for k, v := range b.data {
e.data[k] = v
}

// create dynamic metrics.
// create dynamic metrics
b.dynFieldsLock.RLock()
defer b.dynFieldsLock.RUnlock()
for _, dynField := range b.dynFields {
// Perform the data mutation while locked.
e.data[dynField.name] = dynField.fn()
e.AddField(dynField.name, dynField.fn())
}
return e
}
Expand All @@ -980,14 +973,12 @@ func (b *Builder) Clone() *Builder {
APIHost: b.APIHost,
client: b.client,
}

newB.data = make(map[string]interface{})
b.lock.RLock()
defer b.lock.RUnlock()
newB.data = make(map[string]interface{}, len(b.data))
for k, v := range b.data {
newB.data[k] = v
}

// copy dynamic metric generators
b.dynFieldsLock.RLock()
defer b.dynFieldsLock.RUnlock()
Expand Down

0 comments on commit c9ce44d

Please sign in to comment.