本文整理汇总了Golang中github.com/wfxiang08/rpc_proxy/utils/log.Println函数的典型用法代码示例。如果您正苦于以下问题:Golang Println函数的具体用法?Golang Println怎么用?Golang Println使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Println函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NextWorker
//
// 获取下一个可用的Worker
//
func (pq *PriorityQueue) NextWorker() *Worker {
now := time.Now()
for pq.Len() > 0 {
result := (*pq)[0]
if result.index != INVALID_INDEX && result.Expire.After(now) {
// 只要活着,就留在优先级队列中,等待分配任务
// log.Println("Find Valid Worker...")
result.priority -= 1
// 调整Worker的优先级
heap.Fix(pq, result.index)
return result
} else {
if result.index != INVALID_INDEX {
log.Errorf("Invalid Item index in PriorityQueue#NextWorker")
} else {
log.Println("Worker Expired")
// 只有过期的元素才删除
heap.Remove(pq, result.index)
}
}
}
log.Println("Has Not Worker...")
return nil
}
示例2: NewBackService
// 创建一个BackService
func NewBackService(serviceName string, poller *zmq.Poller, topo *zk.Topology) *BackService {
backSockets := NewBackSockets(poller)
service := &BackService{
ServiceName: serviceName,
backend: backSockets,
poller: poller,
topo: topo,
}
var evtbus chan interface{} = make(chan interface{}, 2)
servicePath := topo.ProductServicePath(serviceName)
endpoints, err := topo.WatchChildren(servicePath, evtbus)
if err != nil {
log.Println("Error: ", err)
panic("Reading Service List Failed: ")
}
go func() {
for true {
// 如何监听endpoints的变化呢?
addrSet := make(map[string]bool)
nowStr := time.Now().Format("@2006-01-02 15:04:05")
for _, endpoint := range endpoints {
// 这些endpoint变化该如何处理呢?
log.Println(utils.Green("---->Find Endpoint: "), endpoint, "For Service: ", serviceName)
endpointInfo, _ := topo.GetServiceEndPoint(serviceName, endpoint)
addr, ok := endpointInfo["frontend"]
if ok {
addrStr := addr.(string)
log.Println(utils.Green("---->Add endpoint to backend: "), addrStr, nowStr, "For Service: ", serviceName)
addrSet[addrStr] = true
}
}
service.backend.UpdateEndpointAddrs(addrSet)
// 等待事件
<-evtbus
// 读取数据,继续监听
endpoints, err = topo.WatchChildren(servicePath, evtbus)
}
}()
ticker := time.NewTicker(time.Millisecond * 1000)
go func() {
for _ = range ticker.C {
service.backend.PurgeEndpoints()
}
}()
return service
}
示例3: DeleteServiceEndpoint
//
// 删除Service Endpoint
//
func (s *ServiceEndpoint) DeleteServiceEndpoint(top *zk.Topology) {
path := top.ProductServiceEndPointPath(s.Service, s.ServiceId)
if ok, _ := top.Exist(path); ok {
zkhelper.DeleteRecursive(top.ZkConn, path, -1)
log.Println(Red("DeleteServiceEndpoint"), "Path: ", path)
}
}
示例4: NewBackServices
func NewBackServices(poller *zmq.Poller, productName string, topo *zk.Topology) *BackServices {
// 创建BackServices
result := &BackServices{
Services: make(map[string]*BackService),
OfflineServices: make(map[string]*BackService),
poller: poller,
topo: topo,
}
var evtbus chan interface{} = make(chan interface{}, 2)
servicesPath := topo.ProductServicesPath()
path, e1 := topo.CreateDir(servicesPath) // 保证Service目录存在,否则会报错
fmt.Println("Path: ", path, "error: ", e1)
services, err := topo.WatchChildren(servicesPath, evtbus)
if err != nil {
log.Println("Error: ", err)
// TODO: 这个地方需要优化
panic("Reading Service List Failed")
}
go func() {
for true {
result.Lock()
for _, service := range services {
log.Println("Service: ", service)
if _, ok := result.Services[service]; !ok {
result.addBackService(service)
}
}
result.Unlock()
// 等待事件
<-evtbus
// 读取数据,继续监听(连接过期了就过期了,再次Watch即可)
services, err = topo.WatchChildren(servicesPath, evtbus)
}
}()
// 读取zk, 等待
log.Println("ProductName: ", result.topo.ProductName)
return result
}
示例5: CreateDir
// 创建指定的Path
func (top *Topology) CreateDir(path string) (string, error) {
dir := top.FullPath(path)
if ok, _ := top.Exist(dir); ok {
log.Println("Path Exists")
return dir, nil
} else {
return zkhelper.CreateRecursive(top.ZkConn, dir, "", 0, zkhelper.DefaultDirACLs())
}
}
示例6: HandleRequest
//
// 将消息发送到Backend上去
//
func (s *BackService) HandleRequest(client_id string, msgs []string) (total int, err error, msg *[]byte) {
backSocket := s.backend.NextSocket()
if backSocket == nil {
// 没有后端服务
if config.VERBOSE {
log.Println(utils.Red("No BackSocket Found for service:"), s.ServiceName)
}
errMsg := GetWorkerNotFoundData(s.ServiceName, 0)
return 0, nil, &errMsg
} else {
if config.VERBOSE {
log.Println("SendMessage With: ", backSocket.Addr, "For Service: ", s.ServiceName)
}
total, err = backSocket.SendMessage("", client_id, "", msgs)
return total, err, nil
}
}
示例7: connect
// 在第一次使用时再连接
func (p *BackSocket) connect() error {
var err error
p.Socket, err = zmq.NewSocket(zmq.DEALER)
if err == nil {
// 这个Id存在问题:
socketSeq += 1
p.Socket.SetIdentity(fmt.Sprintf("proxy-%d-%d", os.Getpid(), socketSeq))
p.Socket.Connect(p.Addr)
// 都只看数据的输入
// 数据的输出经过异步处理,不用考虑时间的问题
p.poller.Add(p.Socket, zmq.POLLIN)
log.Println("Socket Create Succeed")
return nil
} else {
log.Println("Socket Create Failed: ", err)
return err
}
}
示例8: SendMessage
func (p *BackSocket) SendMessage(parts ...interface{}) (total int, err error) {
if p.Socket == nil {
err := p.connect()
if err != nil {
log.Println("Socket Connect Failed")
return 0, err
}
}
return p.Socket.SendMessage(parts...)
}
示例9: SetRpcProxyData
//
// 设置RPC Proxy的数据:
// 绑定的前端的ip/port, 例如: {"rpc_front": "tcp://127.0.0.1:5550"}
//
func (top *Topology) SetRpcProxyData(proxyInfo map[string]interface{}) error {
path := top.FullPath("/rpc_proxy")
data, err := json.Marshal(proxyInfo)
if err != nil {
return err
}
// topo.FlagEphemeral 这里的ProxyInfo是手动配置的,需要持久化
path, err = CreateOrUpdate(top.ZkConn, path, string(data), 0, zkhelper.DefaultDirACLs(), true)
log.Println(green("SetRpcProxyData"), "Path: ", path, ", Error: ", err, ", Data: ", string(data))
return err
}
示例10: PurgeExpired
func (pq *PPQueue) PurgeExpired() {
now := time.Now()
expiredWokers := make([]*Worker, 0)
// 给workerQueue中的所有的worker发送心跳消息
for _, worker := range pq.WorkerQueue {
if worker.Expire.Before(now) {
fmt.Println("Purge Worker: ", worker.Identity, ", At Index: ", worker.index)
expiredWokers = append(expiredWokers, worker)
}
}
log.Println("expiredWokers: ", len(expiredWokers))
// 删除过期的Worker
for _, worker := range expiredWokers {
log.Println("Purge Worker: ", worker.Identity, ", At Index: ", worker.index)
heap.Remove(&(pq.WorkerQueue), worker.index)
delete(pq.id2item, worker.Identity)
}
log.Println("Available Workers: ", green(fmt.Sprintf("%d", len(pq.WorkerQueue))))
}
示例11: HandleRequest
//
// 将消息发送到Backend上去
//
func (s *BackService) HandleRequest(req *Request) (err error) {
backendConn := s.NextBackendConn()
s.lastRequestTime.Set(time.Now().Unix())
if backendConn == nil {
// 没有后端服务
if s.verbose {
log.Println(Red("No BackSocket Found for service:"), s.serviceName)
}
// 从errMsg来构建异常
errMsg := GetWorkerNotFoundData(req, "BackService")
req.Response.Data = errMsg
return nil
} else {
if s.verbose {
log.Println("SendMessage With: ", backendConn.Addr(), "For Service: ", s.serviceName)
}
backendConn.PushBack(req)
return nil
}
}
示例12: getFrontendAddr
func (conf *Config) getFrontendAddr() string {
var frontendAddr = ""
// 如果没有指定FrontHost, 则自动根据 IpPrefix来进行筛选,
// 例如: IpPrefix: 10., 那么最终内网IP: 10.4.10.2之类的被选中
if conf.FrontHost == "" {
log.Println("FrontHost: ", conf.FrontHost, ", Prefix: ", conf.IpPrefix)
if conf.IpPrefix != "" {
conf.FrontHost = GetIpWithPrefix(conf.IpPrefix)
}
}
if conf.FrontPort != "" && conf.FrontHost != "" {
frontendAddr = fmt.Sprintf("tcp://%s:%s", conf.FrontHost, conf.FrontPort)
}
return frontendAddr
}
示例13: GetRpcProxyData
//
// 读取RPC Proxy的数据:
// 绑定的前端的ip/port, 例如: {"rpc_front": "tcp://127.0.0.1:5550"}
//
func (top *Topology) GetRpcProxyData() (proxyInfo map[string]interface{}, e error) {
path := top.FullPath("/rpc_proxy")
data, _, err := top.ZkConn.Get(path)
log.Println("Data: ", data, ", err: ", err)
if err != nil {
return nil, err
}
proxyInfo = make(map[string]interface{})
err = json.Unmarshal(data, &proxyInfo)
if err != nil {
return nil, err
} else {
return proxyInfo, nil
}
}
示例14: UpdateEndpointAddrs
//
// 将不在: addrSet中的endPoint标记为下线
//
func (p *BackSockets) UpdateEndpointAddrs(addrSet map[string]bool) {
p.Lock()
defer p.Unlock()
var addr string
for addr, _ = range addrSet {
p.addEndpoint(addr)
}
now := time.Now().Format("@2006-01-02 15:04:05")
for i := 0; i < p.Active; i++ {
if _, ok := addrSet[p.Sockets[i].Addr]; !ok {
log.Println(utils.Red("MarkEndpointsOffline#Mark Backend Offline: "), p.Sockets[i].Addr, now)
p.markOffline(p.Sockets[i])
i--
}
}
}
示例15: getFrontendAddr
//
// 通过参数依赖,保证getFrontendAddr的调用位置(必须等待Host, IpPrefix, Port读取完毕之后)
//
func (conf *Config) getFrontendAddr(frontHost, ipPrefix, frontPort string) string {
if conf.FrontSock != "" {
return conf.FrontSock
}
var frontendAddr = ""
// 如果没有指定FrontHost, 则自动根据 IpPrefix来进行筛选,
// 例如: IpPrefix: 10., 那么最终内网IP: 10.4.10.2之类的被选中
if frontHost == "" {
log.Println("FrontHost: ", frontHost, ", Prefix: ", ipPrefix)
if ipPrefix != "" {
frontHost = GetIpWithPrefix(ipPrefix)
}
}
if frontPort != "" && frontHost != "" {
frontendAddr = fmt.Sprintf("%s:%s", frontHost, frontPort)
}
return frontendAddr
}