本文整理汇总了Golang中github.com/sorintlab/stolon/Godeps/_workspace/src/golang.org/x/net/context.Background函数的典型用法代码示例。如果您正苦于以下问题:Golang Background函数的具体用法?Golang Background怎么用?Golang Background使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Background函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetKeeperInfo
func (tk *TestKeeper) GetKeeperInfo(timeout time.Duration) (*cluster.KeeperInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/info", net.JoinHostPort(tk.listenAddress, tk.port)), nil)
if err != nil {
return nil, err
}
var data cluster.KeeperInfo
err = httpDo(ctx, req, nil, func(resp *http.Response, err error) error {
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http error code: %d, error: %s", resp.StatusCode, resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return &data, nil
}
示例2: SetKeeperDiscoveryInfo
func (e *EtcdManager) SetKeeperDiscoveryInfo(id string, ms *cluster.KeeperDiscoveryInfo) (*etcd.Response, error) {
msj, err := json.Marshal(ms)
if err != nil {
return nil, err
}
return e.kAPI.Set(context.Background(), filepath.Join(e.etcdPath, keepersDiscoveryInfoDir, id), string(msj), nil)
}
示例3: AtomicDelete
// AtomicDelete deletes a value at "key" if the key
// has not been modified in the meantime, throws an
// error if this is the case
func (s *Etcd) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
if previous == nil {
return false, store.ErrPreviousNotSpecified
}
delOpts := &etcd.DeleteOptions{}
if previous != nil {
delOpts.PrevIndex = previous.LastIndex
if previous.Value != nil {
delOpts.PrevValue = string(previous.Value)
}
}
_, err := s.client.Delete(context.Background(), s.normalize(key), delOpts)
if err != nil {
if etcdError, ok := err.(etcd.Error); ok {
// Key Not Found
if etcdError.Code == etcd.ErrorCodeKeyNotFound {
return false, store.ErrKeyNotFound
}
// Compare failed
if etcdError.Code == etcd.ErrorCodeTestFailed {
return false, store.ErrKeyModified
}
}
return false, err
}
return true, nil
}
示例4: Start
func (s *Sentinel) Start() {
endCh := make(chan struct{})
endApiCh := make(chan error)
router := s.NewRouter()
go func() {
endApiCh <- http.ListenAndServe(fmt.Sprintf("%s:%s", s.listenAddress, s.port), router)
}()
ctx, cancel := context.WithCancel(context.Background())
timerCh := time.NewTimer(0).C
for true {
select {
case <-s.stop:
log.Debugf("stopping stolon sentinel")
cancel()
s.end <- true
return
case <-timerCh:
go func() {
s.clusterSentinelCheck(ctx)
endCh <- struct{}{}
}()
case <-endCh:
timerCh = time.NewTimer(s.clusterConfig.SleepInterval).C
case err := <-endApiCh:
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
close(s.stop)
}
}
}
示例5: GetRoleFromDB
func (p *Manager) GetRoleFromDB() (common.Role, error) {
db, err := sql.Open("postgres", p.connString)
if err != nil {
return 0, err
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), p.requestTimeout)
rows, err := Query(ctx, db, "SELECT pg_is_in_recovery from pg_is_in_recovery()")
cancel()
if err != nil {
return 0, err
}
defer rows.Close()
for rows.Next() {
var isInRecovery bool
if err := rows.Scan(&isInRecovery); err != nil {
return 0, err
}
if isInRecovery {
return common.StandbyRole, nil
}
return common.MasterRole, nil
}
return 0, fmt.Errorf("cannot get pg role from db: no rows returned")
}
示例6: GetReplicatinSlots
func (p *Manager) GetReplicatinSlots() ([]string, error) {
db, err := sql.Open("postgres", p.connString)
if err != nil {
return nil, err
}
defer db.Close()
replSlots := []string{}
ctx, cancel := context.WithTimeout(context.Background(), p.requestTimeout)
rows, err := Query(ctx, db, "SELECT slot_name from pg_replication_slots")
cancel()
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var slotName string
if err := rows.Scan(&slotName); err != nil {
return nil, err
}
replSlots = append(replSlots, slotName)
}
return replSlots, nil
}
示例7: List
// List child nodes of a given directory
func (s *Etcd) List(directory string) ([]*store.KVPair, error) {
getOpts := &etcd.GetOptions{
Quorum: true,
Recursive: true,
Sort: true,
}
resp, err := s.client.Get(context.Background(), s.normalize(directory), getOpts)
if err != nil {
if keyNotFound(err) {
return nil, store.ErrKeyNotFound
}
return nil, err
}
kv := []*store.KVPair{}
for _, n := range resp.Node.Nodes {
kv = append(kv, &store.KVPair{
Key: n.Key,
Value: []byte(n.Value),
LastIndex: n.ModifiedIndex,
})
}
return kv, nil
}
示例8: SetLeaderSentinelInfo
func (e *EtcdManager) SetLeaderSentinelInfo(si *cluster.SentinelInfo, ttl time.Duration) (*etcd.Response, error) {
sij, err := json.Marshal(si)
if err != nil {
return nil, err
}
opts := &etcd.SetOptions{TTL: ttl}
return e.kAPI.Set(context.Background(), filepath.Join(e.etcdPath, leaderSentinelInfoFile), string(sij), opts)
}
示例9: SetProxyInfo
func (e *EtcdManager) SetProxyInfo(pi *cluster.ProxyInfo, ttl time.Duration) (*etcd.Response, error) {
pij, err := json.Marshal(pi)
if err != nil {
return nil, err
}
opts := &etcd.SetOptions{TTL: ttl}
return e.kAPI.Set(context.Background(), filepath.Join(e.etcdPath, proxiesInfoDir, pi.ID), string(pij), opts)
}
示例10: SetClusterConfig
func (e *EtcdManager) SetClusterConfig(cfg *cluster.Config) (*etcd.Response, error) {
cfgj, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
path := filepath.Join(e.etcdPath, configFile)
opts := &etcd.SetOptions{}
return e.kAPI.Set(context.Background(), path, string(cfgj), opts)
}
示例11: getLeaderEndpoint
func (c *httpClusterClient) getLeaderEndpoint() (string, error) {
mAPI := NewMembersAPI(c)
leader, err := mAPI.Leader(context.Background())
if err != nil {
return "", err
}
return leader.ClientURLs[0], nil // TODO: how to handle multiple client URLs?
}
示例12: GetEtcdNode
func (te *TestEtcd) GetEtcdNode(timeout time.Duration, path string) (*etcd.Node, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
res, err := te.kAPI.Get(ctx, path, &etcd.GetOptions{Quorum: true})
if err != nil {
return nil, err
}
return res.Node, nil
}
示例13: pgStateHandler
func (p *PostgresKeeper) pgStateHandler(w http.ResponseWriter, req *http.Request) {
pgState := &cluster.PostgresState{}
p.cvMutex.Lock()
defer p.cvMutex.Unlock()
initialized, err := p.pgm.IsInitialized()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !initialized {
pgState.Initialized = false
} else {
var err error
ctx, cancel := context.WithTimeout(context.Background(), p.clusterConfig.RequestTimeout)
pgState, err = pg.GetPGState(ctx, p.getOurReplConnString())
cancel()
if err != nil {
log.Errorf("error getting pg state: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
pgState.Initialized = true
// if timeline <= 1 then no timeline history file exists.
pgState.TimelinesHistory = cluster.PostgresTimeLinesHistory{}
if pgState.TimelineID > 1 {
ctx, cancel = context.WithTimeout(context.Background(), p.clusterConfig.RequestTimeout)
tlsh, err := pg.GetTimelinesHistory(ctx, pgState.TimelineID, p.getOurReplConnString())
cancel()
if err != nil {
log.Errorf("error getting timeline history: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
pgState.TimelinesHistory = tlsh
}
}
if err := json.NewEncoder(w).Encode(&pgState); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
示例14: DeleteTree
// DeleteTree deletes a range of keys under a given directory
func (s *Etcd) DeleteTree(directory string) error {
delOpts := &etcd.DeleteOptions{
Recursive: true,
}
_, err := s.client.Delete(context.Background(), s.normalize(directory), delOpts)
if keyNotFound(err) {
return store.ErrKeyNotFound
}
return err
}
示例15: Delete
// Delete a value at "key"
func (s *Etcd) Delete(key string) error {
opts := &etcd.DeleteOptions{
Recursive: false,
}
_, err := s.client.Delete(context.Background(), s.normalize(key), opts)
if keyNotFound(err) {
return store.ErrKeyNotFound
}
return err
}