Skip to content

Commit 34fbec7

Browse files
committed
Shorter sleep while waiting for encoder/decoder
If the client is using the same jsoniter config with multiple goroutines it's very likely that few initial operations will encounter a placeholder encoder/decoder while the real one is being created by another goroutine. Having a full second sleep seems too conservative, since encoder/decoder will be created in a very short time. This is very easy to reproduce in any real environment with a few concurrent requests of the same type. A few initial requests will have 1s+ response time. Changing to 10ms should smooth out marshal/unmarshal times for these initial concurrent requests.
1 parent 6a4ba7b commit 34fbec7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

feature_reflect.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool {
154154
}
155155

156156
func (encoder *placeholderEncoder) getRealEncoder() ValEncoder {
157-
for i := 0; i < 30; i++ {
157+
for i := 0; i < 500; i++ {
158158
realDecoder := encoder.cfg.getEncoderFromCache(encoder.cacheKey)
159159
_, isPlaceholder := realDecoder.(*placeholderEncoder)
160160
if isPlaceholder {
161-
time.Sleep(time.Second)
161+
time.Sleep(10 * time.Millisecond)
162162
} else {
163163
return realDecoder
164164
}
@@ -172,11 +172,11 @@ type placeholderDecoder struct {
172172
}
173173

174174
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
175-
for i := 0; i < 30; i++ {
175+
for i := 0; i < 500; i++ {
176176
realDecoder := decoder.cfg.getDecoderFromCache(decoder.cacheKey)
177177
_, isPlaceholder := realDecoder.(*placeholderDecoder)
178178
if isPlaceholder {
179-
time.Sleep(time.Second)
179+
time.Sleep(10 * time.Millisecond)
180180
} else {
181181
realDecoder.Decode(ptr, iter)
182182
return

0 commit comments

Comments
 (0)