Skip to content

Commit

Permalink
Check if the value is nil before putting it into the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Oct 15, 2023
1 parent e13389a commit f201736
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
ErrCodeAcceptFailed
ErrCodeReadFailed
ErrCodePutFailed
ErrCodeNilPointer
ErrCodeCastFailed
ErrCodeHookVerificationFailed
ErrCodeHookReturnedError
Expand Down Expand Up @@ -96,6 +97,8 @@ var (

ErrPutFailed = NewGatewayDError(
ErrCodePutFailed, "failed to put in pool", nil)
ErrNilPointer = NewGatewayDError(
ErrCodeNilPointer, "nil pointer", nil)

ErrCastFailed = NewGatewayDError(
ErrCodeCastFailed, "failed to cast", nil)
Expand Down
12 changes: 12 additions & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (p *Pool) Put(key, value interface{}) *gerr.GatewayDError {
span.RecordError(gerr.ErrPoolExhausted)
return gerr.ErrPoolExhausted
}

if value == nil {
span.RecordError(gerr.ErrNilPointer)
return gerr.ErrNilPointer
}

p.pool.Store(key, value)
return nil
}
Expand All @@ -80,6 +86,12 @@ func (p *Pool) GetOrPut(key, value interface{}) (interface{}, bool, *gerr.Gatewa
span.RecordError(gerr.ErrPoolExhausted)
return nil, false, gerr.ErrPoolExhausted
}

if value == nil {
span.RecordError(gerr.ErrNilPointer)
return nil, false, gerr.ErrNilPointer
}

val, loaded := p.pool.LoadOrStore(key, value)
return val, loaded, nil
}
Expand Down

0 comments on commit f201736

Please sign in to comment.