Created
October 24, 2015 17:00
-
-
Save lysu/3f7e9687132bc46d3222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Example_reuse_socket_connection() { | |
// Put two data: t1-data1, t1-data2 into mytube1. | |
// and one data: t2-staff into mytube2 | |
master, err := beanstalk.Dial("tcp", "127.0.0.1:11300") | |
if err != nil { | |
panic(err) | |
} | |
master.Tube.Name = "mytube1" | |
_, err = master.Put([]byte("t1-data1"), 1, 0, 5*time.Minute) | |
if err != nil { | |
panic(err) | |
} | |
_, err = master.Put([]byte("t1-data2"), 1, 0, 5*time.Minute) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Put two data into mytube1\n") | |
master.Tube.Name = "mytube2" // this is ok, so pool conn.Conn is ok, but pool socket.. | |
_, err = master.Put([]byte("t2-staff"), 1, 0, 5*time.Minute) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Put two data into mytube2\n") | |
// Create socket connection will be reused. | |
reusedConn, _ := net.Dial("tcp", "127.0.0.1:11300") | |
// Consume first t1-data1 from mytube1 and success delete it. | |
worker1Bq := beanstalk.NewConn(reusedConn) | |
worker1Bq.Tube.Name = "mytube1" | |
worker1Bq.TubeSet = *beanstalk.NewTubeSet(worker1Bq, "mytube1") | |
id, data, err := worker1Bq.Reserve(5 * time.Second) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Consume mytube1 will get 'data1', real got is %s\n", string(data)) | |
// Reuse previous connection, and try to consume --- mytube2 | |
// We hope this will get t2-staff...but we found it will consume mytube1 data | |
worker2Bq := beanstalk.NewConn(reusedConn) | |
worker2Bq.Tube.Name = "mytube2" | |
worker2Bq.TubeSet = *beanstalk.NewTubeSet(worker2Bq, "mytube2") | |
id, data, err = worker2Bq.Reserve(5 * time.Second) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Consume mytube2 should take t2-staff, but we got %s", string(data)) | |
err = worker1Bq.Delete(id) | |
if err != nil { | |
panic(err) | |
} | |
if "t2-staff" != string(data) { | |
panic(fmt.Sprintf("worker 2 got data should not got")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment