undo split hello and auth commands, to reduce roundtrips

This commit is contained in:
Joel Meador 2016-06-27 16:54:56 -04:00
parent bb35a5aecc
commit 3d0f5d204c
2 changed files with 9 additions and 37 deletions

View File

@ -31,6 +31,7 @@ const (
DefaultHost = "collector.instrumentalapp.com"
HelloMessage = "hello version go/telegraf/1.1\n"
AuthFormat = "authenticate %s\n"
HandshakeFormat = HelloMessage + AuthFormat
)
var (
@ -59,12 +60,6 @@ func (i *Instrumental) Connect() error {
return err
}
err = i.hello(connection)
if err != nil {
i.conn = nil
return err
}
err = i.authenticate(connection)
if err != nil {
i.conn = nil
@ -175,39 +170,19 @@ func (i *Instrumental) SampleConfig() string {
return sampleConfig
}
func (i *Instrumental) hello(conn net.Conn) error {
_, err := fmt.Fprintf(conn, HelloMessage)
if err != nil {
return err
}
response := make([]byte, 512)
if _, err = conn.Read(response); err != nil {
fmt.Println("hello err", err)
return err
}
if string(response)[:3] != "ok\n" {
return fmt.Errorf("Hello failed: %s", response)
}
return nil
}
func (i *Instrumental) authenticate(conn net.Conn) error {
_, err := fmt.Fprintf(conn, AuthFormat, i.ApiToken)
_, err := fmt.Fprintf(conn, HandshakeFormat, i.ApiToken)
if err != nil {
return err
}
// The response here will either be an "ok" or an error message.
// The response here will either be two "ok"s or an error message.
responses := make([]byte, 512)
if _, err = conn.Read(responses); err != nil {
fmt.Println("err was not nil: ", err)
return err
}
if string(responses)[:3] != "ok\n" {
if string(responses)[:6] != "ok\nok\n" {
return fmt.Errorf("Authentication failed: %s", responses)
}

View File

@ -79,10 +79,9 @@ func TCPServer(t *testing.T, wg *sync.WaitGroup) {
hello, _ := tp.ReadLine()
assert.Equal(t, "hello version go/telegraf/1.1", hello)
conn.Write([]byte("ok\n"))
auth, _ := tp.ReadLine()
assert.Equal(t, "authenticate abc123token", auth)
conn.Write([]byte("ok\n"))
conn.Write([]byte("ok\nok\n"))
data1, _ := tp.ReadLine()
assert.Equal(t, "gauge my.prefix.192_168_0_1.mymeasurement.myfield 3.14 1289430000", data1)
@ -96,11 +95,9 @@ func TCPServer(t *testing.T, wg *sync.WaitGroup) {
hello, _ = tp.ReadLine()
assert.Equal(t, "hello version go/telegraf/1.1", hello)
conn.Write([]byte("ok\n"))
auth, _ = tp.ReadLine()
assert.Equal(t, "authenticate abc123token", auth)
conn.Write([]byte("ok\n"))
conn.Write([]byte("ok\nok\n"))
data3, _ := tp.ReadLine()
assert.Equal(t, "increment my.prefix.192_168_0_1.my_histogram 3.14 1289430000", data3)