本文整理汇总了Golang中github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Init
func (n *Network) Init(ctx context.Context, iface *net.Interface, iaddr net.IP, eaddr net.IP) *backend.SubnetDef {
var be backend.Backend
var sn *backend.SubnetDef
steps := []func() error{
func() (err error) {
n.Config, err = n.sm.GetNetworkConfig(ctx, n.Name)
if err != nil {
log.Error("Failed to retrieve network config: ", err)
}
return
},
func() (err error) {
be, err = newBackend(n.sm, n.Name, n.Config)
if err != nil {
log.Error("Failed to create backend: ", err)
} else {
n.be = be
}
return
},
func() (err error) {
sn, err = be.Init(ctx, iface, iaddr, eaddr)
if err != nil {
log.Errorf("Failed to initialize network %v (type %v): %v", n.Name, n.Config.BackendType, err)
}
n.lease = sn.Lease
return
},
func() (err error) {
if n.ipMasq {
flannelNet := n.Config.Network
if err = setupIPMasq(flannelNet); err != nil {
log.Errorf("Failed to set up IP Masquerade for network %v: %v", n.Name, err)
}
}
return
},
}
for _, s := range steps {
for ; ; time.Sleep(time.Second) {
select {
case <-ctx.Done():
return nil
default:
}
err := s()
if err == nil {
break
}
}
}
return sn
}
示例2: Run
func (n *network) Run(ctx context.Context) {
wg := sync.WaitGroup{}
log.Info("Watching for new subnet leases")
evts := make(chan []subnet.Event)
wg.Add(1)
go func() {
subnet.WatchLeases(ctx, n.sm, n.name, n.lease, evts)
wg.Done()
}()
n.rl = make([]netlink.Route, 0, 10)
wg.Add(1)
go func() {
n.routeCheck(ctx)
wg.Done()
}()
defer wg.Wait()
for {
select {
case evtBatch := <-evts:
n.handleSubnetEvents(evtBatch)
case <-ctx.Done():
return
}
}
}
示例3: Run
func (m *UdpBackend) Run(ctx context.Context) {
// one for each goroutine below
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
runCProxy(m.tun, m.conn, m.ctl2, m.tunNet.IP, m.mtu)
wg.Done()
}()
log.Info("Watching for new subnet leases")
evts := make(chan []subnet.Event)
wg.Add(1)
go func() {
subnet.WatchLeases(ctx, m.sm, m.network, m.lease, evts)
wg.Done()
}()
for {
select {
case evtBatch := <-evts:
m.processSubnetEvents(evtBatch)
case <-ctx.Done():
stopProxy(m.ctl)
break
}
}
wg.Wait()
}
示例4: watchSubnets
func (msr *MockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (Event, uint64, error) {
n, ok := msr.networks[network]
if !ok {
return Event{}, msr.index, fmt.Errorf("Network %s not found", network)
}
for {
if since < msr.index {
return Event{}, msr.index, etcd.Error{
Code: etcd.ErrorCodeEventIndexCleared,
Cause: "out of date",
Message: "cursor is out of date",
Index: msr.index,
}
}
select {
case <-ctx.Done():
return Event{}, msr.index, ctx.Err()
case e := <-n.subnetsEvents:
if e.index <= since {
continue
}
return e.evt, msr.index, nil
}
}
}
示例5: watchSubnet
func (msr *MockSubnetRegistry) watchSubnet(ctx context.Context, network string, since uint64, sn ip.IP4Net) (Event, uint64, error) {
msr.mux.Lock()
n, ok := msr.networks[network]
msr.mux.Unlock()
if !ok {
return Event{}, 0, fmt.Errorf("Network %s not found", network)
}
for {
msr.mux.Lock()
index := msr.index
msr.mux.Unlock()
if since < index {
return Event{}, msr.index, etcd.Error{
Code: etcd.ErrorCodeEventIndexCleared,
Cause: "out of date",
Message: "cursor is out of date",
Index: index,
}
}
select {
case <-ctx.Done():
return Event{}, index, ctx.Err()
case e := <-n.subnetEventsChan(sn):
if e.index > since {
return e.evt, index, nil
}
}
}
}
示例6: watchNetworks
func (msr *MockSubnetRegistry) watchNetworks(ctx context.Context, since uint64) (Event, uint64, error) {
msr.mux.Lock()
index := msr.index
msr.mux.Unlock()
for {
if since < index {
return Event{}, 0, etcd.Error{
Code: etcd.ErrorCodeEventIndexCleared,
Cause: "out of date",
Message: "cursor is out of date",
Index: index,
}
}
select {
case <-ctx.Done():
return Event{}, 0, ctx.Err()
case e := <-msr.networkEvents:
if e.index > since {
return e.evt, e.index, nil
}
}
}
}
示例7: RunServer
func RunServer(ctx context.Context, sm subnet.Manager, listenAddr, cafile, certfile, keyfile string) {
// {network} is always required a the API level but to
// keep backward compat, special "_" network is allowed
// that means "no network"
r := mux.NewRouter()
r.HandleFunc("/v1/{network}/config", bindHandler(handleGetNetworkConfig, ctx, sm)).Methods("GET")
r.HandleFunc("/v1/{network}/leases", bindHandler(handleAcquireLease, ctx, sm)).Methods("POST")
r.HandleFunc("/v1/{network}/leases/{subnet}", bindHandler(handleRenewLease, ctx, sm)).Methods("PUT")
r.HandleFunc("/v1/{network}/leases", bindHandler(handleWatchLeases, ctx, sm)).Methods("GET")
r.HandleFunc("/v1/", bindHandler(handleNetworks, ctx, sm)).Methods("GET")
l, err := listener(listenAddr, cafile, certfile, keyfile)
if err != nil {
log.Errorf("Error listening on %v: %v", listenAddr, err)
return
}
c := make(chan error, 1)
go func() {
c <- http.Serve(l, httpLogger(r))
}()
select {
case <-ctx.Done():
l.Close()
<-c
case err := <-c:
log.Errorf("Error serving on %v: %v", listenAddr, err)
}
}
示例8: AcquireLease
func (m *EtcdManager) AcquireLease(ctx context.Context, network string, attrs *LeaseAttrs) (*Lease, error) {
config, err := m.GetNetworkConfig(ctx, network)
if err != nil {
return nil, err
}
for {
l, err := m.acquireLeaseOnce(ctx, network, config, attrs)
switch {
case err == nil:
log.Info("Subnet lease acquired: ", l.Subnet)
return l, nil
case err == context.Canceled, err == context.DeadlineExceeded:
return nil, err
default:
log.Error("Failed to acquire subnet: ", err)
}
select {
case <-time.After(time.Second):
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
示例9: watchSubnets
func (esr *etcdSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
stop := make(chan bool)
respCh := make(chan watchResp)
go func() {
for {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets")
rresp, err := esr.client().RawWatch(key, since, true, nil, stop)
if err != nil {
respCh <- watchResp{nil, err}
return
}
if len(rresp.Body) == 0 {
// etcd timed out, go back but recreate the client as the underlying
// http transport gets hosed (http://code.google.com/p/go/issues/detail?id=8648)
esr.resetClient()
continue
}
resp, err := rresp.Unmarshal()
respCh <- watchResp{resp, err}
}
}()
select {
case <-ctx.Done():
close(stop)
<-respCh // Wait for f to return.
return nil, ctx.Err()
case wr := <-respCh:
return wr.resp, wr.err
}
}
示例10: routeCheck
func (n *network) routeCheck(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(routeCheckRetries * time.Second):
n.checkSubnetExistInRoutes()
}
}
}
示例11: routeCheck
func (rb *HostgwBackend) routeCheck(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(routeCheckRetries * time.Second):
rb.checkSubnetExistInRoutes()
}
}
}
示例12: Run
func (n *Network) Run(ctx context.Context) {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
n.be.Run()
wg.Done()
}()
<-ctx.Done()
n.be.Stop()
wg.Wait()
}
示例13: watchSubnets
func (msr *mockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case r := <-msr.events:
if r.Node.ModifiedIndex <= since {
continue
}
return r, nil
}
}
}
示例14: Do
func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
req := act.HTTPRequest(c.endpoint)
if err := printcURL(req); err != nil {
return nil, nil, err
}
rtchan := make(chan roundTripResponse, 1)
go func() {
resp, err := c.transport.RoundTrip(req)
rtchan <- roundTripResponse{resp: resp, err: err}
close(rtchan)
}()
var resp *http.Response
var err error
select {
case rtresp := <-rtchan:
resp, err = rtresp.resp, rtresp.err
case <-ctx.Done():
// cancel and wait for request to actually exit before continuing
c.transport.CancelRequest(req)
rtresp := <-rtchan
resp = rtresp.resp
err = ctx.Err()
}
// always check for resp nil-ness to deal with possible
// race conditions between channels above
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
return nil, nil, err
}
var body []byte
done := make(chan struct{})
go func() {
body, err = ioutil.ReadAll(resp.Body)
done <- struct{}{}
}()
select {
case <-ctx.Done():
err = resp.Body.Close()
<-done
if err == nil {
err = ctx.Err()
}
case <-done:
}
return resp, body, err
}
示例15: AutoSync
func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration) error {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
err := c.Sync(ctx)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}