// Start 启动Http服务,开始端口监听和服务处理 func (self *HttpService) Start() bool { self.Lock() defer self.Unlock() c.Infof("Listen at %s", self.Conf.Addr) ln, err := net.Listen("tcp", self.Conf.Addr) if err != nil { c.Errorf("Listen at %s fail,error:%v", self.Conf.Addr, err) return false } tcpListener := tcpKeepAliveListener{ln.(*net.TCPListener)} if self.Conf.MaxConns > 0 { self.listener = netutil.LimitListener(tcpListener, self.Conf.MaxConns) } else { self.listener = tcpListener } self.graceHandler.waitGroup.Add(1) go func() { defer self.graceHandler.waitGroup.Done() err := self.server.Serve(self.listener) if err != nil { c.Errorf("server.Serve return with error:%v", err) } }() return true }
//在事务中执行 func (op *DBOper) DoInTrans(operation DBOperTxFunc) (rt interface{}, err error) { if err := op.BeginTx(); err != nil { return nil, err } var succ = false //结束事务 defer func() { if !succ { op.SetRollbackOnly(true) } transErr := op.finishTrans() if transErr != nil { c.Errorf("Finish transaction erro:%v", transErr) rt = nil err = transErr } }() rt, err = operation(op.tx) if err != nil { c.Errorf("Operation fail:%v", err) succ = false } else { succ = true } return }
// Stop 停止Http服务,关闭端口监听和服务处理 func (self *HttpService) Stop() bool { self.Lock() defer self.Unlock() if self.listener != nil { if err := self.listener.Close(); err != nil { c.Errorf("Close listener error:%v", err) } } //等待所有的服务 c.Infof("Waiting shutdown") self.graceHandler.waitGroup.Wait() c.Infof("Finish shutdown") self.listener = nil self.graceHandler = nil self.server = nil self.serveMux = nil return true }