Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use context everywhere #23

Merged
merged 5 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/rawkv/rawkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
package main

import (
"context"
"fmt"

"github.com/tikv/client-go/config"
"github.com/tikv/client-go/rawkv"
)

func main() {
cli, err := rawkv.NewClient([]string{"127.0.0.1:2379"}, config.Default())
cli, err := rawkv.NewClient(context.TODO(), []string{"127.0.0.1:2379"}, config.Default())
if err != nil {
panic(err)
}
Expand All @@ -33,28 +34,28 @@ func main() {
val := []byte("PingCAP")

// put key into tikv
err = cli.Put(key, val)
err = cli.Put(context.TODO(), key, val)
if err != nil {
panic(err)
}
fmt.Printf("Successfully put %s:%s to tikv\n", key, val)

// get key from tikv
val, err = cli.Get(key)
val, err = cli.Get(context.TODO(), key)
if err != nil {
panic(err)
}
fmt.Printf("found val: %s for key: %s\n", val, key)

// delete key from tikv
err = cli.Delete(key)
err = cli.Delete(context.TODO(), key)
if err != nil {
panic(err)
}
fmt.Printf("key: %s deleted\n", key)

// get key again from tikv
val, err = cli.Get(key)
val, err = cli.Get(context.TODO(), key)
if err != nil {
panic(err)
}
Expand Down
16 changes: 8 additions & 8 deletions examples/txnkv/txnkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ var (
// Init initializes information.
func initStore() {
var err error
client, err = txnkv.NewClient([]string{*pdAddr}, config.Default())
client, err = txnkv.NewClient(context.TODO(), []string{*pdAddr}, config.Default())
if err != nil {
panic(err)
}
}

// key1 val1 key2 val2 ...
func puts(args ...[]byte) error {
tx, err := client.Begin()
tx, err := client.Begin(context.TODO())
if err != nil {
return err
}
Expand All @@ -65,19 +65,19 @@ func puts(args ...[]byte) error {
}

func get(k []byte) (KV, error) {
tx, err := client.Begin()
tx, err := client.Begin(context.TODO())
if err != nil {
return KV{}, err
}
v, err := tx.Get(k)
v, err := tx.Get(context.TODO(), k)
if err != nil {
return KV{}, err
}
return KV{K: k, V: v}, nil
}

func dels(keys ...[]byte) error {
tx, err := client.Begin()
tx, err := client.Begin(context.TODO())
if err != nil {
return err
}
Expand All @@ -91,11 +91,11 @@ func dels(keys ...[]byte) error {
}

func scan(keyPrefix []byte, limit int) ([]KV, error) {
tx, err := client.Begin()
tx, err := client.Begin(context.TODO())
if err != nil {
return nil, err
}
it, err := tx.Iter(key.Key(keyPrefix), nil)
it, err := tx.Iter(context.TODO(), key.Key(keyPrefix), nil)
if err != nil {
return nil, err
}
Expand All @@ -104,7 +104,7 @@ func scan(keyPrefix []byte, limit int) ([]KV, error) {
for it.Valid() && limit > 0 {
ret = append(ret, KV{K: it.Key()[:], V: it.Value()[:]})
limit--
it.Next()
it.Next(context.TODO())
}
return ret, nil
}
Expand Down
18 changes: 18 additions & 0 deletions proxy/httpproxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package httpproxy

import (
"context"
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/tikv/client-go/proxy"
Expand Down Expand Up @@ -69,3 +71,19 @@ func NewHTTPProxyHandler() http.Handler {

return router
}

var defaultTimeout = 20 * time.Second

func reqContext(vars map[string]string) (context.Context, context.CancelFunc) {
ctx := context.Background()
if id := vars["id"]; id != "" {
ctx = context.WithValue(ctx, proxy.UUIDKey, proxy.UUID(id))
}

d, err := time.ParseDuration(vars["timeout"])
if err != nil {
d = defaultTimeout
}

return context.WithTimeout(ctx, d)
}
51 changes: 27 additions & 24 deletions proxy/httpproxy/rawkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package httpproxy

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -47,94 +48,94 @@ type RawResponse struct {
Values [][]byte `json:"values,omitempty"` // for batchGet
}

func (h rawkvHandler) New(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
id, err := h.p.New(r.PDAddrs, config.Default())
func (h rawkvHandler) New(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
id, err := h.p.New(ctx, r.PDAddrs, config.Default())
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{ID: string(id)}, http.StatusCreated, nil
}

func (h rawkvHandler) Close(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
if err := h.p.Close(proxy.UUID(vars["id"])); err != nil {
func (h rawkvHandler) Close(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
if err := h.p.Close(ctx); err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) Get(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
val, err := h.p.Get(proxy.UUID(vars["id"]), r.Key)
func (h rawkvHandler) Get(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
val, err := h.p.Get(ctx, r.Key)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{Value: val}, http.StatusOK, nil
}

func (h rawkvHandler) BatchGet(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
vals, err := h.p.BatchGet(proxy.UUID(vars["id"]), r.Keys)
func (h rawkvHandler) BatchGet(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
vals, err := h.p.BatchGet(ctx, r.Keys)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{Values: vals}, http.StatusOK, nil
}

func (h rawkvHandler) Put(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
err := h.p.Put(proxy.UUID(vars["id"]), r.Key, r.Value)
func (h rawkvHandler) Put(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
err := h.p.Put(ctx, r.Key, r.Value)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) BatchPut(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
err := h.p.BatchPut(proxy.UUID(vars["id"]), r.Keys, r.Values)
func (h rawkvHandler) BatchPut(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
err := h.p.BatchPut(ctx, r.Keys, r.Values)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) Delete(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
err := h.p.Delete(proxy.UUID(vars["id"]), r.Key)
func (h rawkvHandler) Delete(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
err := h.p.Delete(ctx, r.Key)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) BatchDelete(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
err := h.p.BatchDelete(proxy.UUID(vars["id"]), r.Keys)
func (h rawkvHandler) BatchDelete(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
err := h.p.BatchDelete(ctx, r.Keys)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) DeleteRange(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
err := h.p.DeleteRange(proxy.UUID(vars["id"]), r.StartKey, r.EndKey)
func (h rawkvHandler) DeleteRange(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
err := h.p.DeleteRange(ctx, r.StartKey, r.EndKey)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{}, http.StatusOK, nil
}

func (h rawkvHandler) Scan(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
keys, values, err := h.p.Scan(proxy.UUID(vars["id"]), r.StartKey, r.EndKey, r.Limit)
func (h rawkvHandler) Scan(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
keys, values, err := h.p.Scan(ctx, r.StartKey, r.EndKey, r.Limit)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{Keys: keys, Values: values}, http.StatusOK, nil
}

func (h rawkvHandler) ReverseScan(vars map[string]string, r *RawRequest) (*RawResponse, int, error) {
keys, values, err := h.p.ReverseScan(proxy.UUID(vars["id"]), r.StartKey, r.EndKey, r.Limit)
func (h rawkvHandler) ReverseScan(ctx context.Context, r *RawRequest) (*RawResponse, int, error) {
keys, values, err := h.p.ReverseScan(ctx, r.StartKey, r.EndKey, r.Limit)
if err != nil {
return nil, http.StatusInternalServerError, err
}
return &RawResponse{Keys: keys, Values: values}, http.StatusOK, nil
}

func (h rawkvHandler) handlerFunc(f func(map[string]string, *RawRequest) (*RawResponse, int, error)) func(http.ResponseWriter, *http.Request) {
func (h rawkvHandler) handlerFunc(f func(context.Context, *RawRequest) (*RawResponse, int, error)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -146,7 +147,9 @@ func (h rawkvHandler) handlerFunc(f func(map[string]string, *RawRequest) (*RawRe
sendError(w, err, http.StatusBadRequest)
return
}
res, status, err := f(mux.Vars(r), &req)
ctx, cancel := reqContext(mux.Vars(r))
res, status, err := f(ctx, &req)
cancel()
if err != nil {
sendError(w, err, status)
return
Expand Down
Loading