示例#1
0
// GetRaw runs the provided postgres query and args against this sqlquery's db.
func (q SqlQuery) GetRaw(ctx context.Context, query string, args []interface{}, dest interface{}) error {
	db := sqlx.NewDb(q.DB, "postgres")
	log.WithField(ctx, "sql", query).Info("query sql")
	log.WithField(ctx, "args", args).Debug("query args")

	return db.Get(dest, query, args...)
}
示例#2
0
文件: pump.go 项目: jojopoper/horizon
// run is the workhorse of the stream pump system.  It facilitates the triggering
// of open streams by closing a new channel every time the input pump sends.
func run() {
	for {
		select {
		case at, more := <-pump:
			log.WithField(ctx, "time", at).Debug("sse pump")

			prev := nextTick
			nextTick = make(chan struct{})
			// trigger all listeners by closing the nextTick channel
			close(prev)

			if !more {
				return
			}
		case <-ctx.Done():
			pump = nil
			return
		}
	}
}
示例#3
0
// RecoverMiddleware helps the server recover from panics.  It ensures that
// no request can fully bring down the horizon server, and it also logs the
// panics to the logging subsystem.
func RecoverMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		ctx := gctx.FromC(*c)

		defer func() {
			if err := recover(); err != nil {
				stack := make([]byte, 4096) // 4k of stack should be sufficient to see the source
				n := runtime.Stack(stack, false)

				log.
					WithField(ctx, "stacktrace", string(stack[:n])).
					Errorf("panic: %+v", err)

				//TODO: include stack trace if in debug mode
				problem.Render(gctx.FromC(*c), w, problem.ServerError)
			}
		}()

		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}