@@ -116,7 +116,7 @@ type Client struct {
116116 connectTimeout C.int
117117 pollTimeout C.int
118118 retryTimeout C.int
119- maxRetries C.int // maximum amount of retries. maxRetries <= 0 means unlimited. default is -1.
119+ maxRetries C.int // maximum amount of retries. maxRetries <= 0 means unlimited. default is -1.
120120
121121 lk sync.Mutex // protects following fields
122122 freeConns []* conn
@@ -649,6 +649,7 @@ func (cn *conn) configHashFunction(val int) {
649649}
650650
651651// ConfigTimeout Keys:
652+ //
652653// PollTimeout
653654// ConnectTimeout
654655// RetryTimeout
@@ -670,14 +671,14 @@ func (client *Client) ConfigTimeout(cCfgKey C.config_options_t, timeout time.Dur
670671// GetServerAddressByKey will return the address of the memcached
671672// server where a key is stored (assume all memcached servers are
672673// accessiable and wonot establish any connections. )
673- func (client * Client ) GetServerAddressByKey (key string ) string {
674+ func (client * Client ) GetServerAddressByKey (ctx context. Context , key string ) string {
674675 rawKey := client .addPrefix (key )
675676
676677 cKey := C .CString (rawKey )
677678 defer C .free (unsafe .Pointer (cKey ))
678679 cKeyLen := C .size_t (len (rawKey ))
679680
680- cn , err := client .conn (context . Background () )
681+ cn , err := client .conn (ctx )
681682 if err != nil {
682683 return ""
683684 }
@@ -692,13 +693,13 @@ func (client *Client) GetServerAddressByKey(key string) string {
692693// server where a key is stored. (Will try to connect to
693694// corresponding memcached server and may failover accordingly. )
694695// if no server is avaiable, an empty string will be returned.
695- func (client * Client ) GetRealtimeServerAddressByKey (key string ) string {
696+ func (client * Client ) GetRealtimeServerAddressByKey (ctx context. Context , key string ) string {
696697 rawKey := client .addPrefix (key )
697698
698699 cKey := C .CString (rawKey )
699700 defer C .free (unsafe .Pointer (cKey ))
700701 cKeyLen := C .size_t (len (rawKey ))
701- cn , err := client .conn (context . Background () )
702+ cn , err := client .conn (ctx )
702703 if err != nil {
703704 return ""
704705 }
@@ -732,7 +733,7 @@ func (client *Client) addPrefix(key string) string {
732733 return strings .Join ([]string {client .prefix , key }, "" )
733734}
734735
735- func (client * Client ) store (cmd string , item * Item ) error {
736+ func (client * Client ) store (ctx context. Context , cmd string , item * Item ) error {
736737 key := client .addPrefix (item .Key )
737738
738739 cKey := C .CString (key )
@@ -750,7 +751,7 @@ func (client *Client) store(cmd string, item *Item) error {
750751
751752 var errCode C.err_code_t
752753
753- cn , err := client .conn (context . Background () )
754+ cn , err := client .conn (ctx )
754755 if err != nil {
755756 return err
756757 }
@@ -816,32 +817,32 @@ func (client *Client) store(cmd string, item *Item) error {
816817}
817818
818819// Add is a storage command, return without error only when the key is empty
819- func (client * Client ) Add (item * Item ) error {
820- return client .store ("add" , item )
820+ func (client * Client ) Add (ctx context. Context , item * Item ) error {
821+ return client .store (ctx , "add" , item )
821822}
822823
823824// Replace is a storage command, return without error only when the key is not empty
824- func (client * Client ) Replace (item * Item ) error {
825- return client .store ("replace" , item )
825+ func (client * Client ) Replace (ctx context. Context , item * Item ) error {
826+ return client .store (ctx , "replace" , item )
826827}
827828
828829// Prepend value to an existed key
829- func (client * Client ) Prepend (item * Item ) error {
830- return client .store ("prepend" , item )
830+ func (client * Client ) Prepend (ctx context. Context , item * Item ) error {
831+ return client .store (ctx , "prepend" , item )
831832}
832833
833834// Append value to an existed key
834- func (client * Client ) Append (item * Item ) error {
835- return client .store ("append" , item )
835+ func (client * Client ) Append (ctx context. Context , item * Item ) error {
836+ return client .store (ctx , "append" , item )
836837}
837838
838839// Set value to a key
839- func (client * Client ) Set (item * Item ) error {
840- return client .store ("set" , item )
840+ func (client * Client ) Set (ctx context. Context , item * Item ) error {
841+ return client .store (ctx , "set" , item )
841842}
842843
843844// SetMulti will set multi values at once
844- func (client * Client ) SetMulti (items []* Item ) (failedKeys []string , err error ) {
845+ func (client * Client ) SetMulti (ctx context. Context , items []* Item ) (failedKeys []string , err error ) {
845846 nItems := len (items )
846847 cKeys := make ([]* C.char , nItems )
847848 cKeyLens := make ([]C.size_t , nItems )
@@ -877,7 +878,7 @@ func (client *Client) SetMulti(items []*Item) (failedKeys []string, err error) {
877878 var results * * C.message_result_t
878879 var n C.size_t
879880
880- cn , err := client .conn (context . Background () )
881+ cn , err := client .conn (ctx )
881882 if err != nil {
882883 return []string {}, err
883884 }
@@ -934,12 +935,12 @@ func (client *Client) SetMulti(items []*Item) (failedKeys []string, err error) {
934935}
935936
936937// Cas is short for Compare And Swap
937- func (client * Client ) Cas (item * Item ) error {
938- return client .store ("cas" , item )
938+ func (client * Client ) Cas (ctx context. Context , item * Item ) error {
939+ return client .store (ctx , "cas" , item )
939940}
940941
941942// Delete a key
942- func (client * Client ) Delete (key string ) error {
943+ func (client * Client ) Delete (ctx context. Context , key string ) error {
943944 rawKey := client .addPrefix (key )
944945
945946 cKey := C .CString (rawKey )
@@ -950,7 +951,7 @@ func (client *Client) Delete(key string) error {
950951 var rst * * C.message_result_t
951952 var n C.size_t
952953
953- cn , err := client .conn (context . Background () )
954+ cn , err := client .conn (ctx )
954955 if err != nil {
955956 return err
956957 }
@@ -981,7 +982,7 @@ func (client *Client) Delete(key string) error {
981982}
982983
983984// DeleteMulti will delete multi keys at once
984- func (client * Client ) DeleteMulti (keys []string ) (failedKeys []string , err error ) {
985+ func (client * Client ) DeleteMulti (ctx context. Context , keys []string ) (failedKeys []string , err error ) {
985986 var rawKeys []string
986987 if len (client .prefix ) == 0 {
987988 rawKeys = keys
@@ -1009,7 +1010,7 @@ func (client *Client) DeleteMulti(keys []string) (failedKeys []string, err error
10091010 cKeyLen := C .size_t (len (key ))
10101011 cKeyLens [i ] = cKeyLen
10111012 }
1012- cn , err := client .conn (context . Background () )
1013+ cn , err := client .conn (ctx )
10131014 if err != nil {
10141015 return []string {}, err
10151016 }
@@ -1065,8 +1066,8 @@ func (client *Client) DeleteMulti(keys []string) (failedKeys []string, err error
10651066 return
10661067}
10671068
1068- func (client * Client ) getOrGets (cmd string , key string ) (item * Item , err error ) {
1069- cn , err := client .conn (context . Background () )
1069+ func (client * Client ) getOrGets (ctx context. Context , cmd string , key string ) (item * Item , err error ) {
1070+ cn , err := client .conn (ctx )
10701071 if err != nil {
10711072 return nil , err
10721073 }
@@ -1119,17 +1120,17 @@ func (client *Client) getOrGets(cmd string, key string) (item *Item, err error)
11191120}
11201121
11211122// Get is a retrieval command. It will return Item or nil
1122- func (client * Client ) Get (key string ) (* Item , error ) {
1123- return client .getOrGets ("get" , key )
1123+ func (client * Client ) Get (ctx context. Context , key string ) (* Item , error ) {
1124+ return client .getOrGets (ctx , "get" , key )
11241125}
11251126
11261127// Gets is a retrieval command. It will return Item(with casid) or nil
1127- func (client * Client ) Gets (key string ) (* Item , error ) {
1128- return client .getOrGets ("gets" , key )
1128+ func (client * Client ) Gets (ctx context. Context , key string ) (* Item , error ) {
1129+ return client .getOrGets (ctx , "gets" , key )
11291130}
11301131
11311132// GetMulti will return a map of multi values
1132- func (client * Client ) GetMulti (keys []string ) (rv map [string ]* Item , err error ) {
1133+ func (client * Client ) GetMulti (ctx context. Context , keys []string ) (rv map [string ]* Item , err error ) {
11331134 nKeys := len (keys )
11341135 var rawKeys []string
11351136 if len (client .prefix ) == 0 {
@@ -1156,7 +1157,7 @@ func (client *Client) GetMulti(keys []string) (rv map[string]*Item, err error) {
11561157 var rst * * C.retrieval_result_t
11571158 var n C.size_t
11581159
1159- cn , err1 := client .conn (context . Background () )
1160+ cn , err1 := client .conn (ctx )
11601161 if err1 != nil {
11611162 err = err1
11621163 return
@@ -1200,7 +1201,7 @@ func (client *Client) GetMulti(keys []string) (rv map[string]*Item, err error) {
12001201}
12011202
12021203// Touch command
1203- func (client * Client ) Touch (key string , expiration int64 ) error {
1204+ func (client * Client ) Touch (ctx context. Context , key string , expiration int64 ) error {
12041205 rawKey := client .addPrefix (key )
12051206
12061207 cKey := C .CString (rawKey )
@@ -1212,7 +1213,7 @@ func (client *Client) Touch(key string, expiration int64) error {
12121213 var rst * * C.message_result_t
12131214 var n C.size_t
12141215
1215- cn , err := client .conn (context . Background () )
1216+ cn , err := client .conn (ctx )
12161217 if err != nil {
12171218 return err
12181219 }
@@ -1242,7 +1243,7 @@ func (client *Client) Touch(key string, expiration int64) error {
12421243 return networkError (errorMessage (errCode ))
12431244}
12441245
1245- func (client * Client ) incrOrDecr (cmd string , key string , delta uint64 ) (uint64 , error ) {
1246+ func (client * Client ) incrOrDecr (ctx context. Context , cmd string , key string , delta uint64 ) (uint64 , error ) {
12461247 rawKey := client .addPrefix (key )
12471248 cKey := C .CString (rawKey )
12481249 defer C .free (unsafe .Pointer (cKey ))
@@ -1255,7 +1256,7 @@ func (client *Client) incrOrDecr(cmd string, key string, delta uint64) (uint64,
12551256
12561257 var errCode C.err_code_t
12571258
1258- cn , err := client .conn (context . Background () )
1259+ cn , err := client .conn (ctx )
12591260 if err != nil {
12601261 return 0 , err
12611262 }
@@ -1296,22 +1297,22 @@ func (client *Client) incrOrDecr(cmd string, key string, delta uint64) (uint64,
12961297}
12971298
12981299// Incr will increase the value in key by delta
1299- func (client * Client ) Incr (key string , delta uint64 ) (uint64 , error ) {
1300- return client .incrOrDecr ("incr" , key , delta )
1300+ func (client * Client ) Incr (ctx context. Context , key string , delta uint64 ) (uint64 , error ) {
1301+ return client .incrOrDecr (ctx , "incr" , key , delta )
13011302}
13021303
13031304// Decr will decrease the value in key by delta
1304- func (client * Client ) Decr (key string , delta uint64 ) (uint64 , error ) {
1305- return client .incrOrDecr ("decr" , key , delta )
1305+ func (client * Client ) Decr (ctx context. Context , key string , delta uint64 ) (uint64 , error ) {
1306+ return client .incrOrDecr (ctx , "decr" , key , delta )
13061307}
13071308
13081309// Version will return a map reflecting versions of each memcached server
1309- func (client * Client ) Version () (map [string ]string , error ) {
1310+ func (client * Client ) Version (ctx context. Context ) (map [string ]string , error ) {
13101311 var rst * C.broadcast_result_t
13111312 var n C.size_t
13121313 rv := make (map [string ]string )
13131314
1314- cn , err := client .conn (context . Background () )
1315+ cn , err := client .conn (ctx )
13151316 if err != nil {
13161317 return rv , err
13171318 }
@@ -1342,13 +1343,13 @@ func (client *Client) Version() (map[string]string, error) {
13421343}
13431344
13441345// Stats will return a map reflecting stats map of each memcached server
1345- func (client * Client ) Stats () (map [string ](map [string ]string ), error ) {
1346+ func (client * Client ) Stats (ctx context. Context ) (map [string ](map [string ]string ), error ) {
13461347 var rst * C.broadcast_result_t
13471348 var n C.size_t
13481349
13491350 rv := make (map [string ](map [string ]string ))
13501351
1351- cn , err := client .conn (context . Background () )
1352+ cn , err := client .conn (ctx )
13521353 if err != nil {
13531354 return rv , err
13541355 }
@@ -1401,12 +1402,12 @@ func (client *Client) ToggleFlushAllFeature(enabled bool) {
14011402// FlushAll will flush all memcached servers
14021403// You must call ToggleFlushAllFeature(True) first to
14031404// enable this feature.
1404- func (client * Client ) FlushAll () ([]string , error ) {
1405+ func (client * Client ) FlushAll (ctx context. Context ) ([]string , error ) {
14051406 var rst * C.broadcast_result_t
14061407 var n C.size_t
14071408 flushedHosts := []string {}
14081409
1409- cn , err := client .conn (context . Background () )
1410+ cn , err := client .conn (ctx )
14101411 C .client_toggle_flush_all_feature (
14111412 cn ._imp , C .bool (client .flushAllEnabled ),
14121413 )
@@ -1484,6 +1485,7 @@ func (cn *conn) expired(timeout time.Duration) bool {
14841485}
14851486
14861487// FIXME(Harry): We are using quit everywhere when the conn is failed,
1488+ //
14871489// I think we can just close socket instead of send quit, it should save some time.
14881490// So don't mix up Quit and Close, implement a Close function plz.
14891491func (cn * conn ) quit () error {
0 commit comments