github.com/lmb/consul@v1.4.1/lib/stop_context.go (about)

     1  package lib
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // StopChannelContext implements the context.Context interface
     9  // You provide the channel to select on to determine whether
    10  // the context should be cancelled and other code such
    11  // as the rate.Limiter will automatically use the channel
    12  // appropriately
    13  type StopChannelContext struct {
    14  	StopCh <-chan struct{}
    15  }
    16  
    17  func (c *StopChannelContext) Deadline() (deadline time.Time, ok bool) {
    18  	ok = false
    19  	return
    20  }
    21  
    22  func (c *StopChannelContext) Done() <-chan struct{} {
    23  	return c.StopCh
    24  }
    25  
    26  func (c *StopChannelContext) Err() error {
    27  	select {
    28  	case <-c.StopCh:
    29  		return context.Canceled
    30  	default:
    31  		return nil
    32  	}
    33  }
    34  
    35  func (c *StopChannelContext) Value(key interface{}) interface{} {
    36  	return nil
    37  }