From d3f99e611f8a1862c925dc978b3f019348fc6682 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Thu, 18 Feb 2021 12:02:29 +0800 Subject: [PATCH] idPool: fix initial request id compatibility to libfcgi * libfcgi (the example implementation of fcgi) rejects request if requestId is 0. Thus should not allocate 0 as request id. --- client.go | 15 ++++++++++----- client_internal_test.go | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index a3466d1..6e9dc66 100644 --- a/client.go +++ b/client.go @@ -84,8 +84,11 @@ func (p *idPool) Release(id uint16) { func newIDs(limit uint32) (p idPool) { // sanatize limit - if limit == 0 || limit > 65536 { - limit = 65536 + if limit == 0 || limit > 65535 { + // Note: limit is the size of the pool + // Since 0 cannot be requestId, the effective + // pool is from 1 to 65535, hence size is 65535. + limit = 65535 } // pool requestID for the client @@ -98,11 +101,11 @@ func newIDs(limit uint32) (p idPool) { // Ref: https://fast-cgi.github.io/spec#33-records ids := make(chan uint16) go func(maxID uint16) { - for i := uint16(0); i < maxID; i++ { + for i := uint16(1); i < maxID; i++ { ids <- i } ids <- uint16(maxID) - }(uint16(limit - 1)) + }(uint16(limit)) p.IDs = ids return @@ -387,7 +390,9 @@ type ClientFactory func() (Client, error) // // limit is the maximum number of request that the // applcation support. 0 means the maximum number -// available for 16bit request id (65536). +// available for 16bit request id (because 0 is not +// a valid reqeust id, 65535). +// // Default 0. // func SimpleClientFactory(connFactory ConnFactory, limit uint32) ClientFactory { diff --git a/client_internal_test.go b/client_internal_test.go index fb539de..8cc68a2 100644 --- a/client_internal_test.go +++ b/client_internal_test.go @@ -9,7 +9,7 @@ import ( func TestIDPool_Alloc(t *testing.T) { t.Logf("default limit: %d", 65535) ids := newIDs(0) - for i := uint32(0); i <= 65535; i++ { + for i := uint32(1); i <= 65535; i++ { if want, have := uint16(i), ids.Alloc(); want != have { t.Errorf("expected %d, got %d", want, have) } @@ -51,7 +51,7 @@ func TestIDPool_Alloc_withLimit(t *testing.T) { t.Logf("random limit: %d", limit) ids := newIDs(limit) - for i := uint32(0); i < limit; i++ { + for i := uint32(1); i <= limit; i++ { if want, have := uint16(i), ids.Alloc(); want != have { t.Errorf("expected %d, got %d", want, have) }