示例#1
0
func (p *ConnPool) Get() (conn io.Closer, err error) {
	if slowLogEnabled {
		start_t := time.Now()
		defer func() {
			end_t := time.Now()
			diff := float64(end_t.UnixNano()-start_t.UnixNano()) / 1000000
			if diff >= float64(slowLogLimit) {
				logger.Debug("%s get conn from pool cost too much, duration: %f ms, pool: %+v", LOG_TAG, diff, p)
			}
		}()
	}
	p.Lock()
	if p.conns >= p.MaxConns && len(p.free) == 0 {
		p.Unlock()
		return nil, ErrMaxConn
	}

	new_conn := false
	if len(p.free) > 0 {
		// return the first free connection in the pool
		conn = p.free[0]
		p.free = p.free[1:]
	} else {
		conn, err = p.New()
		if err != nil {
			p.Unlock()
			return nil, err
		}
		new_conn = true
	}
	p.Unlock()

	err = p.Ping(conn)
	if err != nil {
		p.Lock()
		logger.Error("%s ping conn fail: %v, pool: %+v", LOG_TAG, err, p)
		if !new_conn && p.conns > 0 {
			p.conns -= 1
		}
		p.Unlock()
		conn.Close()
		return nil, err
	}
	if new_conn {
		p.Lock()
		p.conns += 1
		logger.Trace("%s open new conn: %v, pool: %+v", LOG_TAG, conn, p)
		p.Unlock()
	} else {
		logger.Trace("%s get existent conn: %v, pool: %+v", LOG_TAG, conn, p)
	}

	return conn, nil
}
示例#2
0
func heartbeat() {
	strIp := getIp()
	if "" == strIp || lastIp == strIp {
		logger.Debug("strIp:%s, lastIp: %s.\n", strIp, lastIp)
		return
	}

	logger.Debugln("domainMap's size:", len(domainMap))
	if 0 == len(domainMap) {
		return
	}

	var modifyResult bool
	for _, domain := range g.Config().Domains {
		if _, ok := domainMap[domain.DomainName]; ok {
			if recordResults, exists := recordMap[domain.DomainName]; exists {
				for _, recordResult := range recordResults {
					if existsRecordName(domain.DomainName, recordResult.Name) && strIp != recordResult.Value {
						modifyResult = ModifyRecord(domainMap[domain.DomainName], recordResult.Id, recordResult.Name, strIp)
					}
				}
			}
		}
	}

	if modifyResult {
		lastIp = strIp
		logger.Infoln("last ip have changed into ", strIp)

		if g.Config().Redis.Enabled {
			// send to redis
			rc := g.RedisConnPool.Get()
			defer rc.Close()
			rc.Do("LPUSH", "COMMAND_udai", g.Config().ExecuteCommand)
		}
	}
}
示例#3
0
func getIp() string {
	strUrl := g.Config().GetIpApi
	logger.Debug("REQUEST_URL:%s\n", strUrl)
	httpRequest := httplib.Get(strUrl).SetTimeout(3*time.Second, 10*time.Second)
	httpResponse, err := httpRequest.Bytes()
	if nil != err {
		logger.Errorln("GET_IP error", err)
		return ""
	}

	strIp := ""
	var resp g.ServletResponse
	err = json.Unmarshal(httpResponse, &resp)
	if err != nil {
		logger.Error("decode GET_IP response fail %v\n", err)
	} else if false == resp.Success {
		logger.Error("GET_IP fail %s\n", resp.Message)
	} else {
		strIp = resp.Message
	}

	logger.Infoln("RESPONSE_IP:", strIp)
	return strIp
}