瀏覽代碼

client: document using a custom context

Brian Waldon 11 年之前
父節點
當前提交
6fd105d554
共有 1 個文件被更改,包括 17 次插入0 次删除
  1. 17 0
      client/doc.go

+ 17 - 0
client/doc.go

@@ -44,5 +44,22 @@ Create a KeysAPI using the Client, then use it to interact with etcd:
 	// delete the newly created key only if the value is still "bar"
 	kAPI.Delete(context.Background(), "/foo", &DeleteOptions{PrevValue: "bar"})
 
+Use a custom context to set timeouts on your operations:
+
+	import "time"
+
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
+
+	// set a new key, ignoring it's previous state
+	_, err := kAPI.Set(ctx, "/ping", "pong", nil)
+	if err != nil {
+		if err == context.DeadlineExceeded {
+			// request took longer than 5s
+		} else {
+			// handle error
+		}
+	}
+
 */
 package client