func PrintPanicStack(extras ...interface{}) { if x := recover(); x != nil { log.Error(x) i := 0 funcName, file, line, ok := runtime.Caller(i) for ok { log.Errorf("frame %v:[func:%v,file:%v,line:%v]\n", i, runtime.FuncForPC(funcName).Name(), file, line) i++ funcName, file, line, ok = runtime.Caller(i) } for k := range extras { log.Errorf("EXRAS#%v DATA:%v\n", k, spew.Sdump(extras[k])) } } }
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) { db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(BOLTDB_BUCKET)) for k := range changes { ep := s.read_ep(k) if ep == nil { log.Errorf("cannot find endpoint %v", k) continue } // serialization and save bin, err := msgpack.Marshal(ep.Read()) if err != nil { log.Critical("cannot marshal:", err) continue } err = b.Put([]byte(fmt.Sprint(k)), bin) if err != nil { log.Critical(err) continue } } return nil }) }
func (s *server) Reg(ctx context.Context, p *Chat_Id) (*Chat_Nil, error) { s.Lock() defer s.Unlock() ep := s.eps[p.Id] if ep != nil { log.Errorf("id already exists:%v", p.Id) return nil, ERROR_ALREADY_EXISTS } s.eps[p.Id] = NewEndPoint() s.pending <- p.Id return OK, nil }
// add a service func (p *service_pool) add_service(key, value string) { p.Lock() defer p.Unlock() service_name := filepath.Dir(key) if p.services[service_name] == nil { p.services[service_name] = &service{} log.Tracef("new service type: %v", service_name) } service := p.services[service_name] if conn, err := grpc.Dial(value, grpc.WithTimeout(DEFAULT_DIAL_TIMEOUT)); err == nil { service.clients = append(service.clients, client{key, conn}) log.Tracef("service added: %v -- %v", key, value) } else { log.Errorf("did not connect: %v -- %v err: %v", key, value, err) } }
// subscribe to an endpoint & receive server streams func (s *server) Subscribe(p *Chat_Id, stream ChatService_SubscribeServer) error { // read endpoint ep := s.read_ep(p.Id) if ep == nil { log.Errorf("cannot find endpoint %v", p) return ERROR_NOT_EXISTS } // send history chat messages msgs := ep.Read() for k := range msgs { if err := stream.Send(&msgs[k]); err != nil { return nil } } // create subscriber e := make(chan error, 1) var once sync.Once f := NewSubscriber(func(msg *Chat_Message) { if err := stream.Send(msg); err != nil { once.Do(func() { // protect for channel blocking e <- err }) } }) // subscribe to the endpoint log.Tracef("subscribe to:%v", p.Id) ep.ps.Sub(f) defer func() { ep.ps.Leave(f) log.Tracef("leave from:%v", p.Id) }() // client send cancel to stop receiving, see service_test.go for example select { case <-stream.Context().Done(): case <-e: log.Error(e) } return nil }
// add a service func (p *service_pool) add_service(key, value string) { p.Lock() defer p.Unlock() service_name := filepath.Dir(key) // name check if p.enable_name_check && !p.service_names[service_name] { log.Warningf("service not in names: %v, ignored", service_name) return } if p.services[service_name] == nil { p.services[service_name] = &service{} log.Tracef("new service type: %v", service_name) } service := p.services[service_name] if conn, err := grpc.Dial(value, grpc.WithTimeout(DEFAULT_DIAL_TIMEOUT), grpc.WithInsecure()); err == nil { service.clients = append(service.clients, client{key, conn}) log.Tracef("service added: %v -- %v", key, value) } else { log.Errorf("did not connect: %v -- %v err: %v", key, value, err) } }
// client protocol handle proxy func proxy_user_request(sess *Session, p []byte) []byte { start := time.Now() defer utils.PrintPanicStack(sess, p) // 解密 if sess.Flag&SESS_ENCRYPT != 0 { sess.Decoder.XORKeyStream(p, p) } // 封装为reader reader := packet.Reader(p) // 读客户端数据包序列号(1,2,3...) // 可避免重放攻击-REPLAY-ATTACK seq_id, err := reader.ReadU32() if err != nil { log.Error("read client timestamp failed:", err) sess.Flag |= SESS_KICKED_OUT return nil } // 数据包序列号验证 if seq_id != sess.PacketCount { log.Errorf("illegal packet sequence id:%v should be:%v size:%v", seq_id, sess.PacketCount, len(p)-6) sess.Flag |= SESS_KICKED_OUT return nil } // 读协议号 b, err := reader.ReadS16() if err != nil { log.Error("read protocol number failed.") sess.Flag |= SESS_KICKED_OUT return nil } // 根据协议号断做服务划分 var ret []byte if b > MAX_PROTO_NUM { if err := forward(sess, p[4:]); err != nil { log.Errorf("service id:%v execute failed, error:%v", b, err) sess.Flag |= SESS_KICKED_OUT return nil } } else { if h := client_handler.Handlers[b]; h != nil { ret = h(sess, reader) } else { log.Errorf("service id:%v not bind", b) sess.Flag |= SESS_KICKED_OUT return nil } } // 统计处理时间 elasped := time.Now().Sub(start) if b != 0 { // 排除心跳包日志 log.Trace("[REQ]", client_handler.RCode[b]) _statter.Timing(1.0, fmt.Sprintf("%v%v", STATSD_PREFIX, client_handler.RCode[b]), elasped) } return ret }