func main() { defer exit.Recover() defer logutil.Flush() flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %v:\n", os.Args[0]) flag.PrintDefaults() fmt.Fprintf(os.Stderr, doc) } flag.Parse() args := flag.Args() if len(args) == 0 { flag.Usage() exit.Return(1) } if *zkAddrs != "" { var err error zconn, _, err = zk.DialZkTimeout(*zkAddrs, 5*time.Second, 10*time.Second) if err != nil { log.Errorf("zk connect failed: %v", err.Error()) exit.Return(1) } } cmdName := args[0] args = args[1:] if cmd, ok := cmdMap[cmdName]; ok { subFlags := flag.NewFlagSet(cmdName, flag.ExitOnError) if err := cmd(subFlags, args); err != nil { log.Error(err) exit.Return(1) } } }
func main() { opts.Parse() args := opts.Args if len(args) == 0 { opts.Help() os.Exit(1) } if *zkAddrs != "" { if *zkoccAddr != "" { log.Fatalf("zk.addrs and zk.zkocc-addr are mutually exclusive") } var err error zconn, _, err = zk.DialZkTimeout(*zkAddrs, 5*time.Second, 10*time.Second) if err != nil { log.Fatalf("zk connect failed: %v", err.Error()) } } if *zkoccAddr != "" { var err error zconn, err = zk.DialZkocc(*zkoccAddr, 5*time.Second) if err != nil { log.Fatalf("zkocc connect failed: %v", err.Error()) } } cmdName := args[0] args = args[1:] if cmd, ok := cmdMap[cmdName]; ok { cmd(args) } else { opts.Help() } }
// background routine to initiate a connection sequence // only connect if state == CELL_DISCONNECTED // will change state to CELL_CONNECTING during the connection process // will then change to CELL_CONNECTED (and braodcast the cond) // or to CELL_BACKOFF (and schedule a new reconnection soon) func (zcell *zkCell) connect() { // change our state, we're working on connecting zcell.mutex.Lock() if zcell.state != CELL_DISCONNECTED { // someone else is already connecting zcell.mutex.Unlock() return } zcell.setState(CELL_CONNECTING) zcell.mutex.Unlock() // now connect zconn, session, err := zk.DialZkTimeout(zcell.zkAddr, *baseTimeout, *connectTimeout) if err == nil { zcell.zconn = zconn go zcell.handleSessionEvents(session) } // and change our state zcell.mutex.Lock() if zcell.state != CELL_CONNECTING { panic(fmt.Errorf("Unexpected state: %v", zcell.state)) } if err == nil { relog.Info("zk cell conn: cell %v connected", zcell.cellName) zcell.setState(CELL_CONNECTED) zcell.lastErr = nil } else { relog.Info("zk cell conn: cell %v connection failed: %v", zcell.cellName, err) zcell.setState(CELL_BACKOFF) zcell.lastErr = err go func() { // we're going to try to reconnect at some point // FIXME(alainjobart) backoff algorithm? <-time.NewTimer(time.Duration(*reconnectInterval) * time.Second).C // switch back to DISCONNECTED, and trigger a connect zcell.mutex.Lock() zcell.setState(CELL_DISCONNECTED) zcell.mutex.Unlock() zcell.connect() }() } // we broadcast on the condition to get everybody unstuck, // whether we succeeded to connect or not zcell.ready.Broadcast() zcell.mutex.Unlock() }
func main() { defer logutil.Flush() flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %v:\n", os.Args[0]) flag.PrintDefaults() fmt.Fprintf(os.Stderr, doc) } flag.Parse() args := flag.Args() if len(args) == 0 { flag.Usage() os.Exit(1) } if *zkAddrs != "" { if *zkoccAddr != "" { log.Fatalf("zk.addrs and zk.zkocc-addr are mutually exclusive") } var err error zconn, _, err = zk.DialZkTimeout(*zkAddrs, 5*time.Second, 10*time.Second) if err != nil { log.Fatalf("zk connect failed: %v", err.Error()) } } if *zkoccAddr != "" { var err error zconn, err = zk.DialZkocc(*zkoccAddr, 5*time.Second) if err != nil { log.Fatalf("zkocc connect failed: %v", err.Error()) } } cmdName := args[0] args = args[1:] if cmd, ok := cmdMap[cmdName]; ok { subFlags := flag.NewFlagSet(cmdName, flag.ExitOnError) cmd(subFlags, args) } }