Esempio n. 1
0
func (l *Ledis) ReplicateFromReader(rb io.Reader) error {
	b := new(replBatch)

	b.wb = l.ldb.NewWriteBatch()
	b.l = l

	f := func(head *BinLogHead, event []byte) error {
		if b.lastHead == nil {
			b.lastHead = head
		} else if !b.lastHead.InSameBatch(head) {
			if err := b.Commit(); err != nil {
				log.Fatal("replication error %s, skip to next", err.Error())
				return ErrSkipEvent
			}
			b.lastHead = head
		}

		err := l.replicateEvent(b, event)
		if err != nil {
			log.Fatal("replication error %s, skip to next", err.Error())
			return ErrSkipEvent
		}
		return nil
	}

	err := ReadEventFromReader(rb, f)
	if err != nil {
		b.Rollback()
		return err
	}
	return b.Commit()
}
Esempio n. 2
0
func (c *respClient) run() {
	defer func() {
		if e := recover(); e != nil {
			buf := make([]byte, 4096)
			n := runtime.Stack(buf, false)
			buf = buf[0:n]

			log.Fatal("client run panic %s:%v", buf, e)
		}

		if c.conn != nil {
			c.conn.Close()
		}

		if c.tx != nil {
			c.tx.Rollback()
			c.tx = nil
		}
	}()

	for {
		reqData, err := c.readRequest()
		if err != nil {
			return
		}

		c.handleRequest(reqData)
	}
}
Esempio n. 3
0
func (l *Ledis) ReplicateFromReader(rb io.Reader) error {
	f := func(createTime uint32, event []byte) error {
		err := l.ReplicateEvent(event)
		if err != nil {
			log.Fatal("replication error %s, skip to next", err.Error())
			return ErrSkipEvent
		}
		return nil
	}

	return ReadEventFromReader(rb, f)
}
Esempio n. 4
0
func (c *client) run() {
	defer func() {
		if e := recover(); e != nil {
			buf := make([]byte, 4096)
			n := runtime.Stack(buf, false)
			buf = buf[0:n]

			log.Fatal("client run panic %s:%v", buf, e)
		}

		c.c.Close()
	}()

	for {
		req, err := c.readRequest()
		if err != nil {
			return
		}

		c.handleRequest(req)
	}
}
Esempio n. 5
0
func (c *conn) onRead() {
	defer func() {
		if err := recover(); err != nil {
			buf := make([]byte, 1024)
			buf = buf[:runtime.Stack(buf, false)]
			log.Fatal("crash %v:%s", err, buf)
		}
	}()

	for {
		p, err := c.decoder.Decode()
		if err != nil {
			if err != io.EOF {
				log.Info("on read error %v", err)
			}
			return
		}

		switch p.Method {
		case proto.Publish:
			err = c.handlePublish(p)
		case proto.Bind:
			err = c.handleBind(p)
		case proto.Unbind:
			err = c.handleUnbind(p)
		case proto.Ack:
			err = c.handleAck(p)
		case proto.Heartbeat:
			c.lastUpdate = time.Now().Unix()
		default:
			log.Info("invalid proto method %d", p.Method)
			return
		}

		if err != nil {
			c.writeError(err)
		}
	}
}