Using Redis to do task queues (Golang)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. In the previous article with pure go in the native simulation of the next distributed queue of things. This complements the Redis queue section.
The following three questions need to be addressed in the Redis queue:

1. Queue Building
Use Redis's rpush/lpop to solve

2. Parameter Passing/parsing
The client resolves the restore by depositing the JOSN parameter on the redis,server side.

3. Connection pooling

Redigo support for Redis connection pooling

The following code is a concrete solution to the implementation:

Redis does background task queue//author:xiong Chuan liang//date:2015-3-25package mainimport ("bytes" "Encoding/json" "Errors" "FMT" " Time "" Github.com/garyburd/redigo/redis ") func main () {r, Err: = Newredispool (" "," ") if err! = Nil {fmt. PRINTLN (Err) return}//the job into queue R. Enqueue ()//Remove two jobr.getjob () r.getjob ()}type redispool struct {pool *redis in turn. Pool}func newredispool (server, password string) (*redispool, error) {if Server = = "" {Server = ": 6379"}pool: = &redis. pool{maxidle:3,idletimeout:240 * time. Second,dial:func () (Redis. Conn, error) {c, err: = Redis. Dial ("TCP", server) if err! = Nil {return nil, err}if password! = "" {if _, err: = C.do ("AUTH", password); Err! = Nil {c.close () return nil, Err}}return C, Err},testonborrow:func (c Redis. Conn, T time. Time) Error {_, Err: = C.do ("PING") return Err},}return &redispool{pool}, Nil}type Job struct {Class string ' JSO  N: "Class" ' Args []interface{} ' json: ' args ' '}//impersonate client func (R *redispool) Enqueue () error {c: = R.pool.get () defer c.close () j : = &job{}j.class = "mail" J.args = append (J.args, "xcl_168@aliyun.com", "" "," Body ", 2, True) J2: = &job{}j2. Class = "Log" J2. Args = Append (J2. Args, "Ccc.log", "Ddd.log", []int{222, 333}) for _, V: = range []*job{j, j2} {b, err: = json. Marshal (v) if err! = Nil {return err}_, err = c.do ("Rpush", "queue", B) if err! = Nil {return err}}fmt. Println ("[Enqueue ()] succeed!") Return nil}//Simulation Job Serverfunc (R *redispool) getjob () error {count, err: = R.queuedjobcount () if err! = Nil | | count = = 0 { return errors. New ("no Job.")} Fmt. Println ("[Getjob ()] Jobs count:", count) C: = R.pool.get () defer c.close () for I: = 0; I < int (count); i++ {reply, err: = C.do ("Lpop", "queue") if err! = Nil {return Err}var j Jobdecoder: = json. Newdecoder (bytes. Newreader (reply. ( []byte)]) If err: = decoder. Decode (&AMP;J); Err! = Nil {return err}fmt. Println ("[Getjob ()]", J.class, ":", J.args)}return Nil}func (R *redispool) Queuedjobcount () (int, error) {c: = R.POOL.G ET () defer c.close () lenqueue, err: = C.do ("Llen", "queue") if err! = Nil {return 0, err}count, OK: = Lenqueue. (Int64) If!ok {return 0, errors. New ("Type conversion error!")} return int (count), nil}/* run result: [Enqueue ()] succeed! [Getjob ()] Jobs count:2[getjob ()] Mail: [xcl_168@aliyun.com body 2 true][getjob ()] Log: [Ccc.log ddd.log [222 333]][root@xc Los src]#*/
You can see that the go has been able to get parameters. Are some of the most basic things.

Mail:xcl_168@aliyun.com

Blog:http://blog.csdn.net





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.