Skip to content
This repository has been archived by the owner on Jul 22, 2018. It is now read-only.

maxCap does not restrict created connections #26

Closed
ts2909 opened this issue Mar 7, 2018 · 1 comment
Closed

maxCap does not restrict created connections #26

ts2909 opened this issue Mar 7, 2018 · 1 comment

Comments

@ts2909
Copy link

ts2909 commented Mar 7, 2018

If many goroutines access the pool, more connections are created than expected, perhaps you can devise a way to only create the connection when the send is clear.

func TestCreations(t *testing.T) {
	var m sync.Mutex
	var connectionCount int
	p, _ := NewChannelPool(InitialCap, MaximumCap, func() (net.Conn, error) {
		m.Lock()
		defer m.Unlock()
		connectionCount++
		return net.Dial(network, address)
	})
	defer p.Close()

	var wg sync.WaitGroup
	numWorkers := MaximumCap * 2
	wg.Add(numWorkers)
	for i := 0; i < numWorkers; i++ {
		go func() {
			_, err := p.Get()
			if err != nil {
				t.Fatal(err)
			}
			// c.Close()
			wg.Done()
		}()
	}
	wg.Wait()
	if connectionCount != MaximumCap {
		t.Errorf("expected connection count %d, got %d", MaximumCap, connectionCount)
	}
}
$ go test -run=Creations -race
--- FAIL: TestCreations (0.04s)
        channel_test.go:76: expected connection count 30, got 60
FAIL
@fatih
Copy link
Owner

fatih commented Jul 22, 2018

I'm archiving and closing this project. It's not going to be maintained anymore. Thanks all for their valuable feedback and contributions. If you have questions, feel free to reach me from https://twitter.com/fatih

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants