本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context.Context.Done方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Done方法的具體用法?Golang Context.Done怎麽用?Golang Context.Done使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/flynn/flynn/Godeps/_workspace/src/golang.org/x/net/context.Context
的用法示例。
在下文中一共展示了Context.Done方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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():
}
}()
uconn, err := transport.Connect(ctx)
if err != nil {
p.logf("router: proxy error: %v", err)
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():
}
}()
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)
}
示例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: 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
}
}
}
示例5: 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
}
示例6: 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
}
示例7: 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
}
}
}
示例8: 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
}
}
}
示例9: 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
}
示例10: 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)
}
示例11: AppLog
func (c *controllerAPI) AppLog(ctx context.Context, w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(ctx)
opts := logaggc.LogOpts{
Follow: req.FormValue("follow") == "true",
JobID: req.FormValue("job_id"),
}
if vals, ok := req.Form["process_type"]; ok && len(vals) > 0 {
opts.ProcessType = &vals[len(vals)-1]
}
if strLines := req.FormValue("lines"); strLines != "" {
lines, err := strconv.Atoi(req.FormValue("lines"))
if err != nil {
respondWithError(w, err)
return
}
opts.Lines = &lines
}
rc, err := c.logaggc.GetLog(c.getApp(ctx).ID, &opts)
if err != nil {
respondWithError(w, err)
return
}
if cn, ok := w.(http.CloseNotifier); ok {
go func() {
select {
case <-cn.CloseNotify():
rc.Close()
case <-ctx.Done():
}
}()
}
defer cancel()
defer rc.Close()
if !strings.Contains(req.Header.Get("Accept"), "text/event-stream") {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
// Send headers right away if following
if wf, ok := w.(http.Flusher); ok && opts.Follow {
wf.Flush()
}
fw := httphelper.FlushWriter{Writer: w, Enabled: opts.Follow}
io.Copy(fw, rc)
return
}
ch := make(chan *sseLogChunk)
l, _ := ctxhelper.LoggerFromContext(ctx)
s := sse.NewStream(w, ch, l)
defer s.Close()
s.Serve()
msgc := make(chan *json.RawMessage)
go func() {
defer close(msgc)
dec := json.NewDecoder(rc)
for {
var m json.RawMessage
if err := dec.Decode(&m); err != nil {
if err != io.EOF {
l.Error("decoding logagg stream", err)
}
return
}
msgc <- &m
}
}()
for {
select {
case m := <-msgc:
if m == nil {
ch <- &sseLogChunk{Event: "eof"}
return
}
// write to sse
select {
case ch <- &sseLogChunk{Event: "message", Data: *m}:
case <-s.Done:
return
case <-ctx.Done():
return
}
case <-s.Done:
return
case <-ctx.Done():
return
}
}
}
示例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([]filter, 0)
if strJobID := req.FormValue("job_id"); strJobID != "" {
filters = append(filters, filterJobID{[]byte(strJobID)})
}
if processTypeVals, ok := req.Form["process_type"]; ok && len(processTypeVals) > 0 {
val := processTypeVals[len(processTypeVals)-1]
filters = append(filters, filterProcessType{[]byte(val)})
}
w.WriteHeader(200)
var msgc <-chan *rfc5424.Message
if follow {
msgc = a.agg.ReadLastNAndSubscribe(channelID, lines, filters, ctx.Done())
go flushLoop(w.(http.Flusher), 50*time.Millisecond, ctx.Done())
} else {
msgc = a.agg.ReadLastN(channelID, lines, filters, ctx.Done())
}
enc := json.NewEncoder(w)
for {
select {
case syslogMsg := <-msgc:
if syslogMsg == nil { // channel is closed / done
return
}
if err := enc.Encode(NewMessageFromSyslog(syslogMsg)); err != nil {
log15.Error("error writing msg", "err", err)
return
}
case <-ctx.Done():
return
}
}
}