-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
51 lines (44 loc) · 1.27 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pool_test
import (
"context"
"fmt"
"net/http"
"time"
"github.com/DoNewsCode/core"
pool "github.com/DoNewsCode/core-pool"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/events"
"github.com/gorilla/mux"
)
func Example() {
c := core.Default(core.WithInline("http.addr", ":9999"), core.WithInline("log.level", "none"))
c.Provide(pool.Providers())
c.Invoke(func(p *pool.Pool, dispatcher contract.Dispatcher) {
dispatcher.Subscribe(events.Listen(core.OnHTTPServerStart, func(ctx context.Context, payload interface{}) error {
go func() {
if _, err := http.Get("http://localhost:9999/"); err != nil {
panic(err)
}
}()
return nil
}))
c.AddModule(core.HttpFunc(func(router *mux.Router) {
router.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
p.Go(request.Context(), func(asyncContext context.Context) {
select {
case <-asyncContext.Done():
fmt.Println("async context cancelled")
case <-time.After(time.Second):
fmt.Println("async context will not be cancelled")
}
})
writer.Write(nil)
})
}))
})
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
c.Serve(ctx)
// Output:
// async context will not be cancelled
}