當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Request.Args方法代碼示例

本文整理匯總了Golang中github.com/dongzerun/smartproxy/redis.Request.Args方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.Args方法的具體用法?Golang Request.Args怎麽用?Golang Request.Args使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/dongzerun/smartproxy/redis.Request的用法示例。


在下文中一共展示了Request.Args方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: DEL

func (s *Session) DEL(req *redis.Request) {
	var result int64
	// 串行會很慢,可以考濾開goroutine並行執行
	// 但是這個goroutine量一定要控製,不能有多少key就多少goroutine
	p := make(chan int, s.MulOpParallel)
	for i := 0; i < s.MulOpParallel; i++ {
		p <- 1
	}

	defer func() {
		close(p)
	}()

	keys := req.Args()
	wg := sync.WaitGroup{}
	wg.Add(len(keys))

	for _, key := range keys {
		go func(key string) {
			<-p
			// log.Info("In DEL goroutine ", key)
			cmdslice := []string{"DEL", key}
			r := redis.NewRequest(cmdslice)
			resp := s.Proxy.Backend.OnDEL(r)
			result += resp.Val()
			p <- 1
			wg.Done()
		}(key)
	}

	wg.Wait()
	mergeResp := redis.FormatInt(result)
	// log.Info("DEL merger resp ", mergeResp, result)
	s.write2client(mergeResp)
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:35,代碼來源:special.go

示例2: MSET

func (s *Session) MSET(req *redis.Request) {
	pair := req.Args()
	if len(pair)%2 != 0 {
		err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
		s.write2client([]byte(err))
		return
	}

	p := make(chan int, s.MulOpParallel)
	for i := 0; i < s.MulOpParallel; i++ {
		p <- 1
	}

	defer func() {
		close(p)
	}()
	wg := sync.WaitGroup{}
	wg.Add(len(pair) / 2)
	partialErr := 0
	// we just ignore return code, MSET reuturn OK unless anyone set error
	for i := 0; i < len(pair); i += 2 {
		go func(k string, v string) {
			<-p
			// log.Info("In MSET goroutine ", k, v)
			cmdslice := []string{"SET", k, v}
			r := redis.NewRequest(cmdslice)
			resp := s.Proxy.Backend.OnSET(r)
			if resp.Err() != nil && resp.Err() != redis.Nil {
				// log.Warning("MSET error ", cmdslice, resp.Err())
				partialErr += 1
			}
			p <- 1
			wg.Done()
		}(pair[i], pair[i+1])
	}
	wg.Wait()

	if partialErr == 0 {
		s.write2client(OK_BYTES)
	} else {
		d := fmt.Sprintf("- %d MSET failed, partial key/value %d set\r\n", partialErr, len(pair)/2-partialErr)
		s.write2client([]byte(d))
	}
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:44,代碼來源:special.go

示例3: proxyConf

//loglevel  idletime  mulparallel  statsd  slaveok
func (s *Session) proxyConf(req *redis.Request) {
	// proxy config set loglevel info
	// proxy config set idletime 200
	// proxy config set slaveok 1|0
	// proxy config set mulparallel 30
	// proxy config get statsd
	args := req.Args()
	// config get|set
	switch strings.ToLower(args[1]) {
	case "get":
		if len(req.Args()) != 3 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		cfgname := strings.ToLower(args[2])
		reply := s.proxyConfigGetByName(cfgname)
		s.write2client(reply)
		return
	case "set":
		if len(req.Args()) != 4 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		cfgname := strings.ToLower(args[2])
		value := strings.ToLower(args[3])
		reply := s.proxyConfigSetByName(cfgname, value)
		s.write2client(reply)
		return
	default:
		s.write2client([]byte("-wrong proxy config op type\r\n"))
		return
	}
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:36,代碼來源:proxycmd.go

示例4: MGET

func (s *Session) MGET(req *redis.Request) {
	p := make(chan int, s.MulOpParallel)
	for i := 0; i < s.MulOpParallel; i++ {
		p <- 1
	}

	defer func() {
		close(p)
	}()

	keys := req.Args()
	wg := sync.WaitGroup{}
	wg.Add(len(keys))

	// we should ensure the KEY's order
	result := make([][]byte, len(keys))

	for idx, key := range keys {
		go func(key string, idx int) {
			<-p
			// log.Info("In MGET goroutine ", key)
			cmdslice := []string{"GET", key}
			r := redis.NewRequest(cmdslice)
			resp := s.Proxy.Backend.OnGET(r)
			result[idx] = resp.Reply()
			p <- 1
			wg.Done()
		}(key, idx)
	}

	wg.Wait()
	mergeResp := []byte(fmt.Sprintf("*%d\r\n", len(keys)))
	for _, res := range result {
		mergeResp = append(mergeResp, res...)
	}
	// log.Info("MGET merger resp ", string(mergeResp))
	s.write2client(mergeResp)
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:38,代碼來源:special.go

示例5: preCheckCommand

// buf, shouldClose, handled, err
func preCheckCommand(req *redis.Request) ([]byte, bool, bool, error) {
	var reply []byte
	shouldClose := false

	if req.Len() == 0 {
		return reply, false, true, BadCommandError
	}
	cmd := req.Name()
	switch cmd {
	case "PING":
		reply = []byte("+PONG\r\n")
	case "QUIT":
		reply = OK_BYTES
		shouldClose = true
	case "SELECT":
		//支持 select,但是到後台全部都用的 db 0
		//hia hia hia hia 沒辦法。。。
		reply = OK_BYTES
	case "AUTH":
		reply = OK_BYTES
	case "ECHO":
		if len(req.Args()) == 1 {
			echo := fmt.Sprintf("+%s\r\n", req.Args()[0])
			return []byte(echo), false, true, nil
		} else {
			return nil, false, true, WrongArgumentCount
		}
	}

	if len(reply) > 0 {
		return reply, shouldClose, true, nil
	}

	if err := verifyCommand(req); err != nil {
		return nil, shouldClose, true, err
	}

	if len(req.Args()) >= 1 {
		if _, ok := BlackKeyLists[req.Args()[0]]; ok {
			// key blacked
			reply = []byte("-key already be blacked \r\n")
			return reply, shouldClose, true, nil
		}
	}

	return reply, shouldClose, false, nil
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:48,代碼來源:precheck.go

示例6: proxyBlack

func (s *Session) proxyBlack(req *redis.Request) {
	args := strings.ToLower(req.Args()[1])
	// log.Warning(req.Args())
	switch args {
	// proxy black remove keyname
	case "remove":
		if len(req.Args()) != 3 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		// delete(BlackKeyLists, req.Args()[-1])
		key := req.Args()[2]
		if _, exists := BlackKeyLists[key]; exists {
			log.Warning("remove black key ", key)
			delete(BlackKeyLists, key)
			s.write2client(OK_BYTES)
		} else {
			s.write2client([]byte("-remove key not exists\r\n"))
		}

	case "get":
		if len(req.Args()) != 2 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		ks := make([]string, 0)
		for k, _ := range BlackKeyLists {
			ks = append(ks, k)
		}
		d := redis.FormatStringSlice(ks)
		s.write2client(d)
	case "set":
		//proxy black set 3600 keyname1
		if len(req.Args()) != 4 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		t, err := strconv.Atoi(req.Args()[3])
		if err != nil {
			log.Warningf("black key: %s time unavailable %s", req.Args()[2], req.Args()[3])
			err := fmt.Sprintf("-%s\r\n", BlackTimeUnavaliable)
			s.write2client([]byte(err))
			return
		}
		if t > 86400 || t < 0 {
			log.Warningf("black key: %s time unavailable %s", req.Args()[2], req.Args()[3])
			s.write2client([]byte("-black time must between 0 ~ 86400\r\n"))
			return
		}
		BlackKeyLists[req.Args()[2]] = &BlackKey{
			Name:     req.Args()[2],
			Startup:  time.Now(),
			Deadline: time.Now().Add(time.Duration(t) * time.Second),
		}
		s.write2client(OK_BYTES)
		return
	default:
		err := fmt.Sprintf("-%s\r\n", UnknowProxyOpType)
		s.write2client([]byte(err))
		return
	}
	return
}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:66,代碼來源:proxycmd.go

示例7: PROXY

func (s *Session) PROXY(req *redis.Request) {
	op := strings.ToLower(req.Args()[0])
	// log.Warning("PROXY ", req.Args())
	switch op {
	case "info":
		if len(req.Args()) != 1 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		s.proxyInfo(req)
	case "black":
		if len(req.Args()) < 2 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		s.proxyBlack(req)
	case "config":
		// proxy config set name value
		if len(req.Args()) < 3 || len(req.Args()) > 4 {
			err := fmt.Sprintf("-%s\r\n", WrongArgumentCount)
			s.write2client([]byte(err))
			return
		}
		s.proxyConf(req)
	default:
		log.Warning("Unknow proxy op type: ", req.Args())
		err := fmt.Sprintf("-%s\r\n", UnknowProxyOpType)
		s.write2client([]byte(err))
		return
	}

}
開發者ID:shitfSign,項目名稱:smartproxy,代碼行數:34,代碼來源:proxycmd.go


注:本文中的github.com/dongzerun/smartproxy/redis.Request.Args方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。