Skip to content

Commit

Permalink
Merge pull request #66 from yookoala/fix/idpool
Browse files Browse the repository at this point in the history
idPool: fix initial request id compatibility to libfcgi
  • Loading branch information
yookoala authored Feb 18, 2021
2 parents b30b52d + d3f99e6 commit 8d99b26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 8d99b26

Please sign in to comment.