@@ -112,6 +112,7 @@ var (
112
112
resultTouched = []byte ("TOUCHED\r \n " )
113
113
114
114
resultClientErrorPrefix = []byte ("CLIENT_ERROR " )
115
+ versionPrefix = []byte ("VERSION" )
115
116
)
116
117
117
118
// New returns a memcache client using the provided server(s)
@@ -398,6 +399,30 @@ func (c *Client) flushAllFromAddr(addr net.Addr) error {
398
399
})
399
400
}
400
401
402
+ // ping sends the version command to the given addr
403
+ func (c * Client ) ping (addr net.Addr ) error {
404
+ return c .withAddrRw (addr , func (rw * bufio.ReadWriter ) error {
405
+ if _ , err := fmt .Fprintf (rw , "version\r \n " ); err != nil {
406
+ return err
407
+ }
408
+ if err := rw .Flush (); err != nil {
409
+ return err
410
+ }
411
+ line , err := rw .ReadSlice ('\n' )
412
+ if err != nil {
413
+ return err
414
+ }
415
+
416
+ switch {
417
+ case bytes .HasPrefix (line , versionPrefix ):
418
+ break
419
+ default :
420
+ return fmt .Errorf ("memcache: unexpected response line from ping: %q" , string (line ))
421
+ }
422
+ return nil
423
+ })
424
+ }
425
+
401
426
func (c * Client ) touchFromAddr (addr net.Addr , keys []string , expiration int32 ) error {
402
427
return c .withAddrRw (addr , func (rw * bufio.ReadWriter ) error {
403
428
for _ , key := range keys {
@@ -644,6 +669,12 @@ func (c *Client) DeleteAll() error {
644
669
})
645
670
}
646
671
672
+ // Ping checks all instances if they are alive. Returns error if any
673
+ // of them is down.
674
+ func (c * Client ) Ping () error {
675
+ return c .selector .Each (c .ping )
676
+ }
677
+
647
678
// Increment atomically increments key by delta. The return value is
648
679
// the new value after being incremented or an error. If the value
649
680
// didn't exist in memcached the error is ErrCacheMiss. The value in
0 commit comments