本文整理匯總了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
}