From 354f1a3c401881f62b892b8daec55364a4e2ce45 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Fri, 15 Nov 2013 10:17:39 +0000 Subject: [PATCH] fix syntax errors --- api.go | 2 ++ masterelection/masterelection.go | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/api.go b/api.go index 5e75f16b..5ad33a3e 100644 --- a/api.go +++ b/api.go @@ -17,6 +17,8 @@ var ( MasterElectionPath = "/icecast/cluster/master" MountPrefix = "/icecast/mounts/" NodePrefix = "/icecast/nodes/" + + IcecastPort = 8000 ) // A mountpoint for a stream. diff --git a/masterelection/masterelection.go b/masterelection/masterelection.go index d7844d39..96015f6c 100644 --- a/masterelection/masterelection.go +++ b/masterelection/masterelection.go @@ -28,7 +28,6 @@ type MasterElection struct { stopped bool Addr string - MasterAddr string Path string TTL uint64 @@ -55,6 +54,14 @@ func (m *MasterElection) IsMaster() bool { return m.State == STATE_MASTER } +func (m *MasterElection) GetMasterAddr() string { + responses, err := m.client.Get(m.Path) + if err != nil || len(responses) != 1 { + return "" + } + return responses[0].Value +} + func (m *MasterElection) setState(state int) { if m.State == state { return @@ -100,27 +107,19 @@ func (m *MasterElection) Run() { if m.State == STATE_MASTER { prevValue = m.Addr } - resp, ok, err := m.client.TestAndSet(m.Path, prevValue, m.Addr, m.TTL) - // if err != nil { - // log.Printf("%s: error from etcd: %s", m.Path, err) - // time.Sleep(20 * time.Millisecond) - // continue - // } - - if ok { + if _, ok, _ := m.client.TestAndSet(m.Path, prevValue, m.Addr, m.TTL); ok { // Howdy, we're the master now. Wait a while // and renew our TTL. m.setState(STATE_MASTER) - m.MasterAddr = m.Addr time.Sleep(halfttl) } else { - // We're not the master. Watch for a DELETE - // (in theory, but we're not actually - // verifying the action type, just waiting for - // the first event...) + // We're not the master. We could wait for a + // DELETE but I'm not sure if that's what you + // get on expiry, so we just wait for the + // first event which will be another SET from + // the current master. Oh well. m.setState(STATE_SLAVE) - m.MasterAddr = resp.PrevValue - _, err := m.client.Watch(m.Path, resp.Index, nil, nil) + _, err := m.client.Watch(m.Path, 0, nil, nil) if err != nil { log.Printf("%s: watch error: %s", m.Path, err) } -- GitLab