1 // Copyright 2014 Google Inc. All rights reserved.
2 // Use of this source code is governed by the Apache 2.0
3 // license that can be found in the LICENSE file.
7 // This file implements a network dialer that limits the number of concurrent connections.
8 // It is only used for API calls.
18 var limitSem = make(chan int, 100) // TODO(dsymonds): Use environment variable.
25 // This should not normally happen.
26 log.Print("appengine: unbalanced limitSem release!")
30 func limitDial(network, addr string) (net.Conn, error) {
33 // Dial with a timeout in case the API host is MIA.
34 // The connection should normally be very fast.
35 conn, err := net.DialTimeout(network, addr, 500*time.Millisecond)
40 lc := &limitConn{Conn: conn}
41 runtime.SetFinalizer(lc, (*limitConn).Close) // shouldn't usually be required
45 type limitConn struct {
50 func (lc *limitConn) Close() error {
51 defer lc.close.Do(func() {
53 runtime.SetFinalizer(lc, nil)
55 return lc.Conn.Close()