Skip to content

Commit

Permalink
update reorder example
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeng committed May 3, 2020
1 parent 362273c commit f5e8a96
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions example/recorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"net/http"
"time"

"github.com/didi/sharingan"
)
Expand All @@ -21,11 +20,15 @@ func indexHandle(w http.ResponseWriter, r *http.Request) {

// Tips:正常http请求不需要设置,只有使用go异步执行时需要
func goHandle(w http.ResponseWriter, r *http.Request) {
doneChan := make(chan bool)

go func(delegatedID int64) {
sharingan.SetDelegatedFromGoRoutineID(delegatedID)
defer sharingan.SetDelegatedFromGoRoutineID(0)
http.Get("http://127.0.0.1:8888")

doneChan <- true
}(sharingan.GetCurrentGoRoutineID())

time.Sleep(time.Second)
<-doneChan
}
19 changes: 11 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package sharingan_test

import (
"fmt"
"net/http"

"github.com/didi/sharingan"
)

func Example() {
sharingan.SetDelegatedFromGoRoutineID(1)
doneChan := make(chan bool)

// goroutineID = 0 「without any tag,default」
// goroutineID = 1 「with recorder or replayer」
goroutineID := sharingan.GetCurrentGoRoutineID()
go func(delegatedID int64) {
sharingan.SetDelegatedFromGoRoutineID(delegatedID)
defer sharingan.SetDelegatedFromGoRoutineID(0)

//Output:
//goroutineID = 0
fmt.Printf("goroutineID = %d\n", goroutineID)
http.Get("http://127.0.0.1:8888")

doneChan <- true
}(sharingan.GetCurrentGoRoutineID())

<-doneChan
}

0 comments on commit f5e8a96

Please sign in to comment.