func (c *conn) sendCommand(cmd string, args ...interface{}) error { deadline := time.Now().Add(time.Second * 5) if err := c.nc.SetWriteDeadline(deadline); err != nil { return errors.Trace(err) } return c.writeRESP(redis.NewRequest(cmd, args...)) }
func (pc *testPoolConn) doCmd(c *C, cmd string, args ...interface{}) redis.Resp { r := bufio.NewReaderSize(pc.Conn, 32) w := bufio.NewWriterSize(pc.Conn, 32) req := redis.NewRequest(cmd, args...) err := redis.Encode(w, req) c.Assert(err, IsNil) err = w.Flush() c.Assert(err, IsNil) resp, err := redis.Decode(r) c.Assert(err, IsNil) return resp }
// send PSYNC command and return the first reply line from master func (c *conn) prePSync(masterRunID string, syncOffset int64) (string, error) { deadline := time.Now().Add(time.Second * 5) if err := c.nc.SetDeadline(deadline); err != nil { return "", errors.Trace(err) } if err := c.writeRESP(redis.NewRequest("PSYNC", masterRunID, syncOffset)); err != nil { return "", errors.Trace(err) } rsp, err := c.readLine() if err != nil { return "", errors.Trace(err) } return string(rsp), nil }
func (c *conn) ping() error { deadline := time.Now().Add(time.Second * 5) if err := c.nc.SetDeadline(deadline); err != nil { return errors.Trace(err) } if err := c.writeRESP(redis.NewRequest("PING")); err != nil { return errors.Trace(err) } if rsp, err := c.readLine(); err != nil { return errors.Trace(err) } else if strings.ToLower(string(rsp)) != "+pong" { return errors.Errorf("invalid response of command ping: %s", rsp) } return nil }
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 }