gopool

package module
v0.0.0-...-8669a18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

gopool

  • By Golang realize distributed common connection pool.

  • Golang分布式的连接池,协程池,内含redis client连接池实现

  • go get github.com/aosen/gopool

example

var addrs []string = []string{"127.0.0.1:8000", "127.0.0.1:8001", "127.0.0.1:8002", "127.0.0.1:8003"}

var epool Pooler

func InitExpPool() (err error) {
	if epool == nil {
		epool, err = NewChanConnPool(&ConnPoolReq{
			Addrs: addrs,
			Create: func(addr string, timeout time.Duration) (interface{}, error) {
				cli, err := net.DialTimeout("tcp", addr, timeout)
				return cli, err
			},
			IsOpen: func(cli interface{}) bool {
				if cli != nil {
					return true
				}
				return false
			},
			Down: func(cli interface{}) {
				c := cli.(net.Conn)
				c.Close()
			},
		})
		return
	}
	return
}

func Get() (cli net.Conn, err error) {
	if epool == nil {
		err = errors.New("no init epool.")
		return
	}
	cli, err = epool.Get()
	return
}

func Put(cli net.Conn, safe bool) {
	if epool == nil {
		err := errors.New("no init epool.")
		return
	}
	epool.Put(cli, safe)
}

func GetHealthy() map[string]bool {
	if epool == nil {
		return nil
	}
	return epool.GetHealthy()
}

func GetConnCount() map[string]int {
	if epool == nil {
		return nil
	}
	return epool.GetConnCount()
}

Documentation

Overview

Copyright 2017 gopool Author. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Based on the channel of connection pool 基于channel的连接池

Copyright 2017 gopool Author. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

General distributed connection manager interface 通用分布式连接池管理接口

Copyright 2017 gopool Author. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instantiation of the connection pool configuration parameters 实例化连接池的配置参数

Index

Constants

View Source
const (
	//The default timeout connection services
	//默认连接服务的超时时间
	DefaultConnSvrTimeOut = "100ms"
	//Health check time period
	//健康检查的时间周期
	DefaultCheckAddrCycle = "3s"
	//The default connection service retries
	//默认连接服务的重试次数
	DefaultRetryTimes = 1
	//Connection pool size
	//连接池大小
	DefaultSize = 20
	//Default health threshold, the threshold value of the node to node, unhealthy and cooling
	//默认健康阈值,达到阈值将节点置为不健康节点,并冷却
	DefaultHealthyThreshold = 20
	//The default maximum cooling time, seconds
	//默认最大冷却时长 秒
	DefaultMaxCooldownTime = 120
	//Than the default minimum health, prevent all removed, If only one downstream, does not remove
	//默认最小健康比, 防止全部摘除, 如果只有一个下游,则不会摘除
	DefaultMinHealthyRatio = 0.8
	//The biggest life cycle, the default connection to life cycle will be recycled
	//默认连接的最大生命周期, 达到生命周期会被回收
	DefaultConnLife = 3600
)
View Source
const MaxRetryAfterCooldown = 3

Maximum error number has just been unfrozen connection 刚被解冻的连接最大出错数

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanConnPool

type ChanConnPool struct {
	// contains filtered or unexported fields
}

func NewChanConnPool

func NewChanConnPool(req *ConnPoolReq) (*ChanConnPool, error)

func (*ChanConnPool) Get

func (pool *ChanConnPool) Get() (interface{}, error)

func (*ChanConnPool) GetConnAllCount

func (pool *ChanConnPool) GetConnAllCount() int

func (*ChanConnPool) GetConnCount

func (pool *ChanConnPool) GetConnCount() map[string]int

func (*ChanConnPool) GetHealthy

func (pool *ChanConnPool) GetHealthy() map[string]bool

func (*ChanConnPool) GetUnhealthyNodeCount

func (pool *ChanConnPool) GetUnhealthyNodeCount() (int, []string)

func (*ChanConnPool) Put

func (pool *ChanConnPool) Put(cli interface{}, safe bool)

type ConnPoolReq

type ConnPoolReq struct {
	//Downstream address list
	//下游地址列表
	Addrs []string
	//Connection timeout of service
	//连接服务的超时时间
	ConnSvrTimeOut time.Duration
	//Health check time period
	//健康检查的时间周期
	CheckAddrCycle time.Duration
	//Connected to the downstream service retries
	//连接下游服务的重试次数
	RetryTimes int
	//Maximum number of connections, connection pool size
	//最大连接数,连接池大小
	Size int
	//Threshold, the health to nodes threshold will be set to false unhealthy node
	//健康阈值,达到阈值将节点置为不健康节点 false
	HealthyThreshold int64
	//The maximum cooling time. seconds
	//最大冷却时长 秒
	MaxCooldownTime int64
	//Than the minimum health, prevent all removed
	//最小健康比, 防止全部摘除
	MinHealthyRatio float64
	//Connect the biggest life cycle
	//连接最大生命周期
	ConnLife int64
	//Creating a connection to generate the client method
	//创建连接生成客户端的方法
	Create func(addr string, timeout time.Duration) (interface{}, error)
	//To determine whether a connection closed
	//判断连接是否关闭
	IsOpen func(c interface{}) bool
	//Close the connection
	//关闭连接
	Down func(c interface{})
}

初始化连接池请求格式

func (*ConnPoolReq) GetCheckAddrCycle

func (req *ConnPoolReq) GetCheckAddrCycle() time.Duration

func (*ConnPoolReq) GetConnLife

func (req *ConnPoolReq) GetConnLife() int64

func (*ConnPoolReq) GetConnSvrTimeOut

func (req *ConnPoolReq) GetConnSvrTimeOut() time.Duration

func (*ConnPoolReq) GetHealthyThreshold

func (req *ConnPoolReq) GetHealthyThreshold() int64

func (*ConnPoolReq) GetMaxCooldownTime

func (req *ConnPoolReq) GetMaxCooldownTime() int64

func (*ConnPoolReq) GetMinHealthyRatio

func (req *ConnPoolReq) GetMinHealthyRatio() float64

func (*ConnPoolReq) GetRetryTimes

func (req *ConnPoolReq) GetRetryTimes() int

func (*ConnPoolReq) GetSize

func (req *ConnPoolReq) GetSize() int

type Pooler

type Pooler interface {
	//Get connected
	//获取连接
	Get() (interface{}, error)
	//Recycling connection
	//回收连接
	Put(client interface{}, safe bool)
	//Access to the health status of the downstream nodes
	//获取下游节点的健康状态
	GetHealthy() map[string]bool
	//The current number of connections for the downstream nodes
	//获取各下游节点当前连接数
	GetConnCount() map[string]int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL