コード例 #1
0
func ExampleWithTimeout() {
	// Pass a context with a timeout to tell a blocking function that it
	// should abandon its work after the timeout elapses.
	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
	select {
	case <-time.After(200 * time.Millisecond):
		fmt.Println("overslept")
	case <-ctx.Done():
		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
	}
	// Output:
	// context deadline exceeded
}
コード例 #2
0
func (w *Weavebox) makeHTTPRouterHandle(h Handler) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, params httprouter.Params) {
		if w.context == nil {
			w.context = context.Background()
		}
		ctx := &Context{
			Context:  w.context,
			vars:     params,
			response: rw,
			request:  r,
			weavebox: w,
		}
		for _, handler := range w.middleware {
			if err := handler(ctx); err != nil {
				w.ErrorHandler(ctx, err)
				return
			}
		}
		if err := h(ctx); err != nil {
			w.ErrorHandler(ctx, err)
			return
		}
	}
}