本文整理汇总了Golang中github.com/Kissaki/log4go.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleTCPConn
// hanleTCPConn handle a long live tcp connection.
func handleTCPConn(conn net.Conn, rc chan *bufio.Reader) {
addr := conn.RemoteAddr().String()
log.Debug("<%s> handleTcpConn routine start", addr)
rd := newBufioReader(rc, conn)
if args, err := parseCmd(rd); err == nil {
// return buffer bufio.Reader
putBufioReader(rc, rd)
switch args[0] {
case "sub":
SubscribeTCPHandle(conn, args[1:])
break
default:
conn.Write(ParamReply)
log.Warn("<%s> unknown cmd \"%s\"", addr, args[0])
break
}
} else {
// return buffer bufio.Reader
putBufioReader(rc, rd)
log.Error("<%s> parseCmd() error(%v)", addr, err)
}
// close the connection
if err := conn.Close(); err != nil {
log.Error("<%s> conn.Close() error(%v)", addr, err)
}
log.Debug("<%s> handleTcpConn routine stop", addr)
return
}
示例2: clean
// DelMulti implements the Storage DelMulti method.
func (s *RedisStorage) clean() {
for {
info := <-s.delCH
conn := s.getConn(info.Key)
if conn == nil {
log.Warn("get redis connection nil")
continue
}
for _, mid := range info.MIds {
if err := conn.Send("ZREMRANGEBYSCORE", info.Key, mid, mid); err != nil {
log.Error("conn.Send(\"ZREMRANGEBYSCORE\", \"%s\", %d, %d) error(%v)", info.Key, mid, mid, err)
conn.Close()
continue
}
}
if err := conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
conn.Close()
continue
}
for _, _ = range info.MIds {
_, err := conn.Receive()
if err != nil {
log.Error("conn.Receive() error(%v)", err)
conn.Close()
continue
}
}
conn.Close()
}
}
示例3: HandleWrite
// HandleWrite start a goroutine get msg from chan, then send to the conn.
func (c *Connection) HandleWrite(key string) {
go func() {
var (
n int
err error
)
log.Debug("user_key: \"%s\" HandleWrite goroutine start", key)
for {
msg, ok := <-c.Buf
if !ok {
log.Debug("user_key: \"%s\" HandleWrite goroutine stop", key)
return
}
if c.Proto == WebsocketProto {
// raw
n, err = c.Conn.Write(msg)
} else if c.Proto == TCPProto {
// redis protocol
msg = []byte(fmt.Sprintf("$%d\r\n%s\r\n", len(msg), string(msg)))
n, err = c.Conn.Write(msg)
} else {
log.Error("unknown connection protocol: %d", c.Proto)
panic(ErrConnProto)
}
// update stat
if err != nil {
log.Error("user_key: \"%s\" conn.Write() error(%v)", key, err)
MsgStat.IncrFailed(1)
} else {
log.Debug("user_key: \"%s\" write \r\n========%s(%d)========", key, string(msg), n)
MsgStat.IncrSucceed(1)
}
}
}()
}
示例4: watchCometNode
// watchNode watch a named node for leader selection when failover
func watchCometNode(conn *zk.Conn, node, fpath string, retry, ping time.Duration, ch chan *CometNodeEvent) {
fpath = path.Join(fpath, node)
for {
nodes, watch, err := myzk.GetNodesW(conn, fpath)
if err == myzk.ErrNodeNotExist {
log.Warn("zk don't have node \"%s\"", fpath)
break
} else if err == myzk.ErrNoChild {
log.Warn("zk don't have any children in \"%s\", retry in %d second", fpath, waitNodeDelay)
time.Sleep(waitNodeDelaySecond)
continue
} else if err != nil {
log.Error("zk path: \"%s\" getNodes error(%v), retry in %d second", fpath, err, waitNodeDelay)
time.Sleep(waitNodeDelaySecond)
continue
}
// leader selection
sort.Strings(nodes)
if info, err := registerCometNode(conn, nodes[0], fpath, retry, ping, true); err != nil {
log.Error("zk path: \"%s\" registerCometNode error(%v)", fpath, err)
time.Sleep(waitNodeDelaySecond)
continue
} else {
// update node info
ch <- &CometNodeEvent{Event: eventNodeUpdate, Key: node, Value: info}
}
// blocking receive event
event := <-watch
log.Info("zk path: \"%s\" receive a event: (%v)", fpath, event)
}
// WARN, if no persistence node and comet rpc not config
log.Warn("zk path: \"%s\" never watch again till recreate", fpath)
}
示例5: NewMySQLStorage
// NewMySQLStorage initialize mysql pool and consistency hash ring.
func NewMySQLStorage() *MySQLStorage {
dbPool := make(map[string]*sql.DB)
ring := ketama.NewRing(ketamaBase)
for n, source := range Conf.MySQLSource {
nw := strings.Split(n, mysqlSourceSpliter)
if len(nw) != 2 {
err := errors.New("node config error, it's nodeN:W")
log.Error("strings.Split(\"%s\", :) failed (%v)", n, err)
panic(err)
}
w, err := strconv.Atoi(nw[1])
if err != nil {
log.Error("strconv.Atoi(\"%s\") failed (%v)", nw[1], err)
panic(err)
}
db, err := sql.Open("mysql", source)
if err != nil {
log.Error("sql.Open(\"mysql\", %s) failed (%v)", source, err)
panic(err)
}
dbPool[nw[0]] = db
ring.AddNode(nw[0], w)
}
ring.Bake()
s := &MySQLStorage{pool: dbPool, ring: ring}
go s.clean()
return s
}
示例6: PushMsg
// PushMsg implements the Channel PushMsg method.
func (c *SeqChannel) PushMsg(key string, m *myrpc.Message, expire uint) (err error) {
client := myrpc.MessageRPC.Get()
if client == nil {
return ErrMessageRPC
}
c.mutex.Lock()
// private message need persistence
// if message expired no need persistence, only send online message
// rewrite message id
//m.MsgId = c.timeID.ID()
m.MsgId = id.Get()
if m.GroupId != myrpc.PublicGroupId && expire > 0 {
args := &myrpc.MessageSavePrivateArgs{Key: key, Msg: m.Msg, MsgId: m.MsgId, Expire: expire}
ret := 0
if err = client.Call(myrpc.MessageServiceSavePrivate, args, &ret); err != nil {
c.mutex.Unlock()
log.Error("%s(\"%s\", \"%v\", &ret) error(%v)", myrpc.MessageServiceSavePrivate, key, args, err)
return
}
}
// push message
if err = c.writeMsg(key, m); err != nil {
c.mutex.Unlock()
log.Error("c.WriteMsg(\"%s\", m) error(%v)", key, err)
return
}
c.mutex.Unlock()
return
}
示例7: GetPrivate
// GetPrivate implements the Storage GetPrivate method.
func (s *MySQLStorage) GetPrivate(key string, mid int64) ([]*myrpc.Message, error) {
db := s.getConn(key)
if db == nil {
return nil, ErrNoMySQLConn
}
now := time.Now().Unix()
rows, err := db.Query(getPrivateMsgSQL, key, mid)
if err != nil {
log.Error("db.Query(\"%s\",\"%s\",%d,now) failed (%v)", getPrivateMsgSQL, key, mid, err)
return nil, err
}
msgs := []*myrpc.Message{}
for rows.Next() {
expire := int64(0)
cmid := int64(0)
msg := []byte{}
if err := rows.Scan(&cmid, &expire, &msg); err != nil {
log.Error("rows.Scan() failed (%v)", err)
return nil, err
}
if now > expire {
log.Warn("user_key: \"%s\" mid: %d expired", key, cmid)
continue
}
msgs = append(msgs, &myrpc.Message{MsgId: cmid, GroupId: myrpc.PrivateGroupId, Msg: json.RawMessage(msg)})
}
return msgs, nil
}
示例8: RegisterTemp
// RegisterTmp create a ephemeral node, and watch it, if node droped then send a SIGQUIT to self.
func RegisterTemp(conn *zk.Conn, fpath string, data []byte) error {
tpath, err := conn.Create(path.Join(fpath)+"/", data, zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
if err != nil {
log.Error("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)", fpath, string(data), err)
return err
}
log.Debug("create a zookeeper node:%s", tpath)
// watch self
go func() {
for {
log.Info("zk path: \"%s\" set a watch", tpath)
exist, _, watch, err := conn.ExistsW(tpath)
if err != nil {
log.Error("zk.ExistsW(\"%s\") error(%v)", tpath, err)
log.Warn("zk path: \"%s\" set watch failed, kill itself", tpath)
killSelf()
return
}
if !exist {
log.Warn("zk path: \"%s\" not exist, kill itself", tpath)
killSelf()
return
}
event := <-watch
log.Info("zk path: \"%s\" receive a event %v", tpath, event)
}
}()
return nil
}
示例9: parseCmd
// parseCmd parse the tcp request command.
func parseCmd(rd *bufio.Reader) ([]string, error) {
// get argument number
argNum, err := parseCmdSize(rd, '*')
if err != nil {
log.Error("tcp:cmd format error when find '*' (%v)", err)
return nil, err
}
if argNum < minCmdNum || argNum > maxCmdNum {
log.Error("tcp:cmd argument number length error")
return nil, ErrProtocol
}
args := make([]string, 0, argNum)
for i := 0; i < argNum; i++ {
// get argument length
cmdLen, err := parseCmdSize(rd, '$')
if err != nil {
log.Error("tcp:parseCmdSize(rd, '$') error(%v)", err)
return nil, err
}
// get argument data
d, err := parseCmdData(rd, cmdLen)
if err != nil {
log.Error("tcp:parseCmdData() error(%v)", err)
return nil, err
}
// append args
args = append(args, string(d))
}
return args, nil
}
示例10: watchMessageRoot
// watchMessageRoot watch the message root path.
func watchMessageRoot(conn *zk.Conn, fpath string, ch chan *MessageNodeEvent) error {
for {
nodes, watch, err := myzk.GetNodesW(conn, fpath)
if err == myzk.ErrNodeNotExist {
log.Warn("zk don't have node \"%s\", retry in %d second", fpath, waitNodeDelay)
time.Sleep(waitNodeDelaySecond)
continue
} else if err == myzk.ErrNoChild {
log.Warn("zk don't have any children in \"%s\", retry in %d second", fpath, waitNodeDelay)
// all child died, kick all the nodes
for _, client := range MessageRPC.Clients {
log.Debug("node: \"%s\" send del node event", client.Addr)
ch <- &MessageNodeEvent{Event: eventNodeDel, Key: &WeightRpc{Addr: client.Addr, Weight: client.Weight}}
}
time.Sleep(waitNodeDelaySecond)
continue
} else if err != nil {
log.Error("getNodes error(%v), retry in %d second", err, waitNodeDelay)
time.Sleep(waitNodeDelaySecond)
continue
}
nodesMap := map[string]bool{}
// handle new add nodes
for _, node := range nodes {
data, _, err := conn.Get(path.Join(fpath, node))
if err != nil {
log.Error("zk.Get(\"%s\") error(%v)", path.Join(fpath, node), err)
continue
}
// parse message node info
nodeInfo := &MessageNodeInfo{}
if err := json.Unmarshal(data, nodeInfo); err != nil {
log.Error("json.Unmarshal(\"%s\", nodeInfo) error(%v)", string(data), err)
continue
}
for _, addr := range nodeInfo.Rpc {
// if not exists in old map then trigger a add event
if _, ok := MessageRPC.Clients[addr]; !ok {
ch <- &MessageNodeEvent{Event: eventNodeAdd, Key: &WeightRpc{Addr: addr, Weight: nodeInfo.Weight}}
}
nodesMap[addr] = true
}
}
// handle delete nodes
for _, client := range MessageRPC.Clients {
if _, ok := nodesMap[client.Addr]; !ok {
ch <- &MessageNodeEvent{Event: eventNodeDel, Key: client}
}
}
// blocking wait node changed
event := <-watch
log.Info("zk path: \"%s\" receive a event %v", fpath, event)
}
}
示例11: GetOfflineMsg0
// GetOfflineMsg get offline mesage http handler.
func GetOfflineMsg0(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
key := params.Get("key")
midStr := params.Get("mid")
callback := params.Get("callback")
res := map[string]interface{}{"ret": OK, "msg": "ok"}
defer retWrite(w, r, res, callback, time.Now())
if key == "" || midStr == "" {
res["ret"] = ParamErr
return
}
mid, err := strconv.ParseInt(midStr, 10, 64)
if err != nil {
res["ret"] = ParamErr
log.Error("strconv.ParseInt(\"%s\", 10, 64) error(%v)", midStr, err)
return
}
// RPC get offline messages
reply := &myrpc.MessageGetResp{}
args := &myrpc.MessageGetPrivateArgs{MsgId: mid, Key: key}
client := myrpc.MessageRPC.Get()
if client == nil {
res["ret"] = InternalErr
return
}
if err := client.Call(myrpc.MessageServiceGetPrivate, args, reply); err != nil {
log.Error("myrpc.MessageRPC.Call(\"%s\", \"%v\", reply) error(%v)", myrpc.MessageServiceGetPrivate, args, err)
res["ret"] = InternalErr
return
}
omsgs := []string{}
opmsgs := []string{}
for _, msg := range reply.Msgs {
omsg, err := msg.OldBytes()
if err != nil {
res["ret"] = InternalErr
return
}
omsgs = append(omsgs, string(omsg))
}
if len(omsgs) == 0 {
return
}
res["data"] = map[string]interface{}{"msgs": omsgs, "pmsgs": opmsgs}
return
}
示例12: registerCometNode
// registerCometNode get infomation of comet node
func registerCometNode(conn *zk.Conn, node, fpath string, retry, ping time.Duration, startPing bool) (info *CometNodeInfo, err error) {
// get current node info from zookeeper
fpath = path.Join(fpath, node)
data, _, err := conn.Get(fpath)
if err != nil {
log.Error("zk.Get(\"%s\") error(%v)", fpath, err)
return
}
info = &CometNodeInfo{}
if err = json.Unmarshal(data, info); err != nil {
log.Error("json.Unmarshal(\"%s\", nodeData) error(%v)", string(data), err)
return
}
if len(info.RpcAddr) == 0 {
log.Error("zk nodes: \"%s\" don't have rpc addr", fpath)
err = ErrCometRPC
return
}
// get old node info for finding the old rpc connection
oldInfo := cometNodeInfoMap[node]
// init comet rpc
clients := make(map[string]*WeightRpc, len(info.RpcAddr))
for _, addr := range info.RpcAddr {
var (
r *rpc.Client
)
if oldInfo != nil && oldInfo.Rpc != nil {
if wr, ok := oldInfo.Rpc.Clients[addr]; ok && wr.Client != nil {
// reuse the rpc connection must let old client = nil, avoid reclose rpc.
oldInfo.Rpc.Clients[addr].Client = nil
r = wr.Client
}
}
if r == nil {
if r, err = rpc.Dial("tcp", addr); err != nil {
log.Error("rpc.Dial(\"%s\") error(%v)", addr, err)
return
}
log.Debug("node:%s addr:%s rpc reconnect", node, addr)
}
clients[addr] = &WeightRpc{Weight: 1, Addr: addr, Client: r}
}
// comet rpc use rand load balance
lb, err := NewRandLB(clients, cometService, retry, ping, startPing)
if err != nil {
log.Error("NewRandLR() error(%v)", err)
return
}
info.Rpc = lb
log.Info("zk path: \"%s\" register nodes: \"%s\"", fpath, node)
return
}
示例13: httpListen
func httpListen(mux *http.ServeMux, bind string) {
server := &http.Server{Handler: mux, ReadTimeout: Conf.HttpServerTimeout, WriteTimeout: Conf.HttpServerTimeout}
server.SetKeepAlivesEnabled(false)
l, err := net.Listen("tcp", bind)
if err != nil {
log.Error("net.Listen(\"tcp\", \"%s\") error(%v)", bind, err)
panic(err)
}
if err := server.Serve(l); err != nil {
log.Error("server.Serve() error(%v)", err)
panic(err)
}
}
示例14: rpcListen
func rpcListen(bind string) {
l, err := net.Listen("tcp", bind)
if err != nil {
log.Error("net.Listen(\"tcp\", \"%s\") error(%v)", bind, err)
panic(err)
}
defer func() {
if err := l.Close(); err != nil {
log.Error("listener.Close() error(%v)", err)
}
}()
rpc.Accept(l)
}
示例15: PushPrivate
// PushPrivate handle for push private message.
func PushPrivate(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method Not Allowed", 405)
return
}
body := ""
res := map[string]interface{}{"ret": OK}
defer retPWrite(w, r, res, &body, time.Now())
// param
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
res["ret"] = InternalErr
log.Error("ioutil.ReadAll() failed (%v)", err)
return
}
body = string(bodyBytes)
params := r.URL.Query()
key := params.Get("key")
expire, err := strconv.ParseUint(params.Get("expire"), 10, 32)
if err != nil {
res["ret"] = ParamErr
log.Error("strconv.ParseUint(\"%s\", 10, 32) error(%v)", params.Get("expire"), err)
return
}
node := myrpc.GetComet(key)
if node == nil || node.Rpc == nil {
res["ret"] = NotFoundServer
return
}
client := node.Rpc.Get()
if client == nil {
res["ret"] = NotFoundServer
return
}
rm := json.RawMessage(bodyBytes)
msg, err := rm.MarshalJSON()
if err != nil {
res["ret"] = ParamErr
log.Error("json.RawMessage(\"%s\").MarshalJSON() error(%v)", body, err)
return
}
args := &myrpc.CometPushPrivateArgs{Msg: json.RawMessage(msg), Expire: uint(expire), Key: key}
ret := 0
if err := client.Call(myrpc.CometServicePushPrivate, args, &ret); err != nil {
log.Error("client.Call(\"%s\", \"%s\", &ret) error(%v)", myrpc.CometServicePushPrivate, args.Key, err)
res["ret"] = InternalErr
return
}
return
}