Ejemplo n.º 1
0
Archivo: conn.go Proyecto: cuiwm/reborn
func (c *conn) handleRequest(h *Handler) (redis.Resp, error) {
	if c.timeout > 0 {
		deadline := time.Now().Add(c.timeout)
		if err := c.nc.SetReadDeadline(deadline); err != nil {
			return nil, errors.Trace(err)
		}
	}
	request, err := redis.DecodeRequest(c.r)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if request.Type() == redis.TypePing {
		return nil, nil
	}

	h.counters.commands.Add(1)
	response, err := c.dispatch(h, request)
	if err != nil {
		h.counters.commandsFailed.Add(1)
		b, _ := redis.EncodeToBytes(request)
		log.Warningf("handle commands failed, conn = %s, request = '%s', err = %s", c, base64.StdEncoding.EncodeToString(b), err)
	}

	return response, nil
}
Ejemplo n.º 2
0
Archivo: repl.go Proyecto: CowLeo/qdb
func (h *Handler) replicationFeedSlaves(f *store.Forward) error {
	h.repl.Lock()
	defer h.repl.Unlock()

	r := &h.repl
	if r.backlogBuf == nil && len(r.slaves) == 0 {
		return nil
	}

	if r.backlogBuf == nil {
		if err := h.createReplicationBacklog(); err != nil {
			return errors.Trace(err)
		}
	}

	if r.lastSelectDB.Get() != int64(f.DB) {
		selectCmd, _ := redis.EncodeToBytes(redis.NewRequest("SELECT", f.DB))

		// write SELECT into backlog
		if err := h.feedReplicationBacklog(selectCmd); err != nil {
			return errors.Trace(err)
		}

		r.lastSelectDB.Set(int64(f.DB))
	}

	// encode Forward with RESP format, then write into backlog
	if buf, err := h.respEncodeStoreForward(f); err != nil {
		return errors.Trace(err)
	} else if err = h.feedReplicationBacklog(buf); err != nil {
		return errors.Trace(err)
	}

	// notice slaves replication backlog has new data, need to sync
	if err := h.replicationNoticeSlavesSyncing(); err != nil {
		return errors.Trace(err)
	}

	return nil
}