本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ServeConn
// ServeConn takes an inbound conn and proxies it to a backend.
func (p *ReverseProxy) ServeConn(ctx context.Context, dconn net.Conn) {
transport := p.transport
if transport == nil {
panic("router: nil transport for proxy")
}
defer dconn.Close()
clientGone := dconn.(http.CloseNotifier).CloseNotify()
ctx, cancel := context.WithCancel(ctx)
defer cancel() // finish cancellation goroutine
go func() {
select {
case <-clientGone:
cancel() // client went away, cancel request
case <-ctx.Done():
}
}()
l := p.Logger.New("client_addr", dconn.RemoteAddr(), "host_addr", dconn.LocalAddr(), "proxy", "tcp")
uconn, err := transport.Connect(ctx, l)
if err != nil {
return
}
defer uconn.Close()
joinConns(uconn, dconn)
}
示例2: ServeConn
// ServeConn takes an inbound conn and proxies it to a backend.
func (p *ReverseProxy) ServeConn(ctx context.Context, dconn net.Conn) {
transport := p.transport
if transport == nil {
panic("router: nil transport for proxy")
}
defer dconn.Close()
clientGone := dconn.(http.CloseNotifier).CloseNotify()
ctx, cancel := context.WithCancel(ctx)
defer cancel() // finish cancellation goroutine
go func() {
select {
case <-clientGone:
cancel() // client went away, cancel request
case <-ctx.Done():
}
}()
uconn, err := transport.Connect(ctx)
if err != nil {
p.logf("router: proxy error: %v", err)
return
}
defer uconn.Close()
joinConns(uconn, dconn)
}
示例3: GetLog
func (a *aggregatorAPI) GetLog(ctx context.Context, w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(ctx)
if cn, ok := w.(http.CloseNotifier); ok {
ch := cn.CloseNotify()
go func() {
select {
case <-ch:
cancel()
case <-ctx.Done():
}
}()
}
defer cancel()
params, _ := ctxhelper.ParamsFromContext(ctx)
follow := false
if strFollow := req.FormValue("follow"); strFollow == "true" {
follow = true
}
var (
backlog bool
lines int
err error
)
if strLines := req.FormValue("lines"); strLines != "" {
if lines, err = strconv.Atoi(strLines); err != nil {
httphelper.ValidationError(w, "lines", err.Error())
return
}
if lines < 0 || lines > 10000 {
httphelper.ValidationError(w, "lines", "lines must be an integer between 0 and 10000")
return
}
backlog = lines > 0
}
filters := make(filterSlice, 0)
if jobID := req.FormValue("job_id"); jobID != "" {
filters = append(filters, filterJobID(jobID))
}
if processTypeVals, ok := req.Form["process_type"]; ok && len(processTypeVals) > 0 {
val := processTypeVals[len(processTypeVals)-1]
filters = append(filters, filterProcessType(val))
}
iter := &Iterator{
id: params.ByName("channel_id"),
follow: follow,
backlog: backlog,
lines: lines,
filter: filters,
donec: ctx.Done(),
}
writeMessages(ctx, w, iter.Scan(a.agg))
}
示例4: retrieveToken
// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is returned along
// with an error..
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) {
tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v)
if err != nil {
return nil, err
}
token := tokenFromInternal(tk)
if notifierFunc, ok := ctx.Value(TokenRefreshNotifier).(TokenRefreshNotifierFunc); ok {
notifierFunc(token)
}
return token, nil
}
示例5: Sync
func (d *pgDataStore) Sync(ctx context.Context, h SyncHandler, startc chan<- struct{}) error {
ctx, cancel := context.WithCancel(ctx)
idc, errc, err := d.startListener(ctx)
if err != nil {
return err
}
initialRoutes, err := d.List()
if err != nil {
cancel()
return err
}
toRemove := h.Current()
for _, route := range initialRoutes {
if _, ok := toRemove[route.ID]; ok {
delete(toRemove, route.ID)
}
if err := h.Set(route); err != nil {
return err
}
}
// send remove for any routes that are no longer in the database
for id := range toRemove {
if err := h.Remove(id); err != nil {
return err
}
}
close(startc)
for {
select {
case id := <-idc:
if err := d.handleUpdate(h, id); err != nil {
cancel()
return err
}
case err = <-errc:
return err
case <-ctx.Done():
// wait for startListener to finish (it will either
// close idc or send an error on errc)
select {
case <-idc:
case <-errc:
}
return nil
}
}
}
示例6: dialTCP
func dialTCP(ctx context.Context, addrs []string) (net.Conn, string, error) {
donec := ctx.Done()
for _, addr := range addrs {
select {
case <-donec:
return nil, "", errCanceled
default:
}
if conn, err := dialer.Dial("tcp", addr); err == nil {
return conn, addr, nil
}
}
return nil, "", errNoBackends
}
示例7: ContextClient
func ContextClient(ctx context.Context) (*http.Client, error) {
for _, fn := range contextClientFuncs {
c, err := fn(ctx)
if err != nil {
return nil, err
}
if c != nil {
return c, nil
}
}
if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
return hc, nil
}
return http.DefaultClient, nil
}
示例8: dialTCP
func dialTCP(ctx context.Context, l log15.Logger, addrs []string) (net.Conn, string, error) {
donec := ctx.Done()
for i, addr := range addrs {
select {
case <-donec:
return nil, "", errCanceled
default:
}
conn, err := dialer.Dial("tcp", addr)
if err == nil {
return conn, addr, nil
}
l.Error("retriable dial error", "backend", addr, "err", err, "attempt", i)
}
return nil, "", errNoBackends
}
示例9: Sync
func (d *pgDataStore) Sync(ctx context.Context, h SyncHandler, startc chan<- struct{}) error {
ctx, cancel := context.WithCancel(ctx)
idc, errc, err := d.startListener(ctx)
if err != nil {
return err
}
initialRoutes, err := d.List()
if err != nil {
cancel()
return err
}
for _, route := range initialRoutes {
if err := h.Set(route); err != nil {
return err
}
}
close(startc)
for {
select {
case id := <-idc:
if err := d.handleUpdate(h, id); err != nil {
cancel()
return err
}
case err = <-errc:
return err
case <-ctx.Done():
// wait for startListener to finish (it will either
// close idc or send an error on errc)
select {
case <-idc:
case <-errc:
}
return nil
}
}
}
示例10: writeMessages
func writeMessages(ctx context.Context, w http.ResponseWriter, msgc <-chan *rfc5424.Message) {
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
enc := json.NewEncoder(w)
for {
select {
case syslogMsg, ok := <-msgc:
if !ok { // channel is closed / done
return
}
if err := enc.Encode(NewMessageFromSyslog(syslogMsg)); err != nil {
log15.Error("error writing msg", "err", err)
return
}
case <-ticker.C:
w.(http.Flusher).Flush()
case <-ctx.Done():
return
}
}
}
示例11: startListener
func (d *pgDataStore) startListener(ctx context.Context) (<-chan string, <-chan error, error) {
idc := make(chan string)
errc := make(chan error)
conn, err := d.pgx.Acquire()
if err != nil {
return nil, nil, err
}
if err = conn.Listen(d.tableName); err != nil {
d.pgx.Release(conn)
return nil, nil, err
}
go func() {
defer unlistenAndRelease(d.pgx, conn, d.tableName)
defer close(idc)
for {
select {
case <-ctx.Done():
return
default:
}
notification, err := conn.WaitForNotification(time.Second)
if err == pgx.ErrNotificationTimeout {
continue
}
if err != nil {
errc <- err
return
}
idc <- notification.Payload
}
}()
return idc, errc, nil
}
示例12: GetLog
func (a *aggregatorAPI) GetLog(ctx context.Context, w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(ctx)
if cn, ok := w.(http.CloseNotifier); ok {
go func() {
select {
case <-cn.CloseNotify():
cancel()
case <-ctx.Done():
}
}()
}
defer cancel()
params, _ := ctxhelper.ParamsFromContext(ctx)
channelID := params.ByName("channel_id")
follow := false
if strFollow := req.FormValue("follow"); strFollow == "true" {
follow = true
}
lines := -1 // default to all lines
if strLines := req.FormValue("lines"); strLines != "" {
var err error
lines, err = strconv.Atoi(strLines)
if err != nil || lines < 0 || lines > 10000 {
httphelper.ValidationError(w, "lines", "lines must be an integer between 0 and 10000")
return
}
}
filters := make(filterSlice, 0)
if jobID := req.FormValue("job_id"); jobID != "" {
filters = append(filters, filterJobID(jobID))
}
if processTypeVals, ok := req.Form["process_type"]; ok && len(processTypeVals) > 0 {
val := processTypeVals[len(processTypeVals)-1]
filters = append(filters, filterProcessType(val))
}
w.WriteHeader(200)
var msgc <-chan *rfc5424.Message
if follow {
msgc = a.agg.ReadLastNAndSubscribe(channelID, lines, filters, ctx.Done())
} else {
msgc = a.agg.ReadLastN(channelID, lines, filters, ctx.Done())
}
writeMessages(ctx, w, msgc)
}
示例13: RequestIDFromContext
// RequestIDFromContext extracts a request ID from a context.
func RequestIDFromContext(ctx context.Context) (id string, ok bool) {
id, ok = ctx.Value(ctxKeyReqID).(string)
return
}
示例14: getApp
func (c *controllerAPI) getApp(ctx context.Context) *ct.App {
return ctx.Value("app").(*ct.App)
}
示例15: SessionFromContext
func (api *API) SessionFromContext(ctx context.Context) *sessions.Session {
return ctx.Value(ctxSessionKey).(*sessions.Session)
}