File tree Expand file tree Collapse file tree 2 files changed +20
-10
lines changed
Expand file tree Collapse file tree 2 files changed +20
-10
lines changed Original file line number Diff line number Diff line change @@ -155,8 +155,22 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
155155 batch = append (batch , buf ... )
156156 }
157157
158+ err = g .send (batch )
159+
160+ // try to reconnect and retry to send
161+ if err != nil {
162+ log .Println ("E! Graphite: Reconnecting and retrying: " )
163+ g .Connect ()
164+ err = g .send (batch )
165+ }
166+
167+ return err
168+ }
169+
170+ func (g * Graphite ) send (batch []byte ) error {
158171 // This will get set to nil if a successful write occurs
159- err = errors .New ("Could not write to any Graphite server in cluster\n " )
172+ err := errors .New ("Could not write to any Graphite server in cluster\n " )
173+
160174 // Send data to a random server
161175 p := rand .Perm (len (g .conns ))
162176 for _ , n := range p {
@@ -167,18 +181,16 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
167181 if _ , e := g .conns [n ].Write (batch ); e != nil {
168182 // Error
169183 log .Println ("E! Graphite Error: " + e .Error ())
184+ // Close explicitely
185+ g .conns [n ].Close ()
170186 // Let's try the next one
171187 } else {
172188 // Success
173189 err = nil
174190 break
175191 }
176192 }
177- // try to reconnect
178- if err != nil {
179- log .Println ("E! Reconnecting: " )
180- g .Connect ()
181- }
193+
182194 return err
183195}
184196
Original file line number Diff line number Diff line change @@ -81,18 +81,16 @@ func TestGraphiteOK(t *testing.T) {
8181 err2 := g .Write (metrics )
8282 require .NoError (t , err2 )
8383
84- // Waiting TCPserver
84+ // Waiting TCPserver, should reconnect and resend
8585 wg .Wait ()
8686 t .Log ("Finished Waiting for first data" )
8787 var wg2 sync.WaitGroup
8888 // Start TCP server
8989 wg2 .Add (1 )
9090 TCPServer2 (t , & wg2 )
9191 //Write but expect an error, but reconnect
92- g .Write (metrics2 )
9392 err3 := g .Write (metrics2 )
94- t .Log ("Finished writing second data, it should have failed" )
95- //Actually write the new metrics
93+ t .Log ("Finished writing second data, it should have reconnected automatically" )
9694
9795 require .NoError (t , err3 )
9896 t .Log ("Finished writing third data" )
You can’t perform that action at this time.
0 commit comments