Exemplo n.º 1
0
// if maxConcurrent == 0, no limit on concurrency.
func NewHttpClient(maxConcurrent, timeout int) *HttpClient {
	mi := maxConcurrent / 5
	if mi <= 0 {
		mi = DefaultMaxIdleConnsPerHost
	}
	ts := &Transport{
		Proxy: ProxyFromEnvironment,
		Dial: (&net.Dialer{
			Timeout:   10 * time.Second,
			KeepAlive: 60 * time.Second,
		}).Dial,
		TLSHandshakeTimeout: 10 * time.Second,
		MaxIdleConnsPerHost: mi,
	}
	hc := &Client{
		Transport: ts,
		Timeout:   time.Duration(timeout) * time.Second,
	}

	c := &HttpClient{}
	c.ts = ts
	c.hc = hc
	if maxConcurrent > 0 {
		c.concur = chanutil.NewSemaphore(maxConcurrent)
	}
	return c
}
Exemplo n.º 2
0
func NewRedisPool(
	newFn func() (Conn, error),
	testFn func(c Conn, t time.Time) error,
	idleTimeout time.Duration,
	maxConcurrent int) *RedisPool {

	mi := maxConcurrent / 5
	if mi <= 0 {
		mi = 2
	}

	rp := &Pool{
		Dial:         newFn,
		TestOnBorrow: testFn,
		MaxIdle:      mi,
		MaxActive:    0,
		Wait:         false,
		IdleTimeout:  idleTimeout,
	}

	p := &RedisPool{}
	p.p = rp
	if maxConcurrent > 0 {
		p.concur = chanutil.NewSemaphore(maxConcurrent)
	}
	return p
}
Exemplo n.º 3
0
func NewSQLDB(db *DB, maxConcurrent int) *SQLDB {
	mi := maxConcurrent / 5
	if mi <= 0 {
		mi = 2
	}
	db.SetMaxIdleConns(mi)
	return &SQLDB{
		concur: chanutil.NewSemaphore(maxConcurrent),
		db:     db,
	}
}
Exemplo n.º 4
0
// The maxconcurrent handler type is a middleware that can limit the
// maximum number of concurrent access.
// if maxConcurrent == 0, no limit on concurrency.
// if hesitateTime == 0, use DefaultHesitateTime.
func NewMaxConcurrentHandler(oh ContextHandler,
	maxConcurrent int, hesitateTime time.Duration,
	notifier MaxConcurrentNotifier) ContextHandler {

	if maxConcurrent <= 0 {
		return oh
	}
	if hesitateTime <= 0 {
		hesitateTime = DefaultHesitateTime
	}

	return &maxConcurrentHandler{
		oh:           oh,
		concur:       chanutil.NewSemaphore(maxConcurrent),
		hesitateTime: hesitateTime,
		notifier:     notifier,
	}
}