From d6c38ba1c0d84a60d3498099858b739cbeb30acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 15 Aug 2023 12:56:49 +0800 Subject: [PATCH] update etcd --- define.go | 9 ++++ delete.go | 8 +-- delete_test.go | 22 -------- go.mod | 16 +----- go.sum | 35 +------------ lease.go | 24 ++++----- lease_test.go | 68 ------------------------- string.go | 18 +++---- string_test.go | 56 --------------------- watch.go | 20 ++++---- watch_test.go | 134 ------------------------------------------------- 11 files changed, 46 insertions(+), 364 deletions(-) delete mode 100644 delete_test.go delete mode 100644 lease_test.go delete mode 100644 string_test.go delete mode 100644 watch_test.go diff --git a/define.go b/define.go index d2b3f59..a8b232e 100644 --- a/define.go +++ b/define.go @@ -66,3 +66,12 @@ type LeaseKeepAliveData struct { LeaseDetail *clientv3.LeaseKeepAliveResponse `json:"lease_detail"` // 续约数据 Data map[string]interface{} `json:"data"` // 携带的数据 } + +// WrapperClient 包装的客户端类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:48 2023/8/15 +type WrapperClient struct { + client *clientv3.Client +} diff --git a/delete.go b/delete.go index 0a62445..8dbc91a 100644 --- a/delete.go +++ b/delete.go @@ -20,7 +20,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 2:02 下午 2021/11/24 -func DeleteWithOption(ctx context.Context, key string, timeout time.Duration, optionList ...clientv3.OpOption) (*clientv3.DeleteResponse, error) { +func (wc *WrapperClient) DeleteWithOption(ctx context.Context, key string, timeout time.Duration, optionList ...clientv3.OpOption) (*clientv3.DeleteResponse, error) { if nil == ctx { ctx = context.Background() } @@ -37,7 +37,7 @@ func DeleteWithOption(ctx context.Context, key string, timeout time.Duration, op ctx, cancelFunc = context.WithTimeout(ctx, timeout) defer cancelFunc() - if deleteResponse, err = Client.Delete(ctx, key, optionList...); nil != err { + if deleteResponse, err = wc.client.Delete(ctx, key, optionList...); nil != err { return nil, errors.New("delete key fail : " + err.Error()) } @@ -49,6 +49,6 @@ func DeleteWithOption(ctx context.Context, key string, timeout time.Duration, op // Author : go_developer@163.com<白茶清欢> // // Date : 2:12 下午 2021/11/24 -func DeleteWithKeyPrefix(ctx context.Context, keyPrefix string, timeout time.Duration) (*clientv3.DeleteResponse, error) { - return DeleteWithOption(ctx, keyPrefix, timeout, clientv3.WithPrefix()) +func (wc *WrapperClient) DeleteWithKeyPrefix(ctx context.Context, keyPrefix string, timeout time.Duration) (*clientv3.DeleteResponse, error) { + return wc.DeleteWithOption(ctx, keyPrefix, timeout, clientv3.WithPrefix()) } diff --git a/delete_test.go b/delete_test.go deleted file mode 100644 index dbcbf49..0000000 --- a/delete_test.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package etcd... -// -// Description : etcd... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2021-11-24 2:11 下午 -package etcd - -import ( - "fmt" - "testing" -) - -// TestDeleteWithKeyPrefix ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2:11 下午 2021/11/24 -func TestDeleteWithKeyPrefix(t *testing.T) { - fmt.Println(DeleteWithKeyPrefix(nil, "/test", 0)) -} diff --git a/go.mod b/go.mod index 1516592..a921824 100644 --- a/go.mod +++ b/go.mod @@ -8,25 +8,17 @@ replace ( ) require ( - git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444 + github.com/coreos/etcd v3.3.27+incompatible go.etcd.io/etcd v3.3.27+incompatible ) require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b // indirect - git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230811085725-1a7414bb7590 // indirect - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230815040629-5c584bcb7691 // indirect - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230811142316-14bc4c2c9de3 // indirect - github.com/BurntSushi/toml v1.3.2 // indirect - github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect github.com/coreos/bbolt v1.3.4 // indirect - github.com/coreos/etcd v3.3.27+incompatible // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect github.com/coreos/pkg v0.0.0-20230601102743-20bbbf26f4d8 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/go-ini/ini v1.67.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect @@ -36,17 +28,12 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/mozillazg/go-pinyin v0.20.0 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.16.0 // indirect github.com/soheilhy/cmux v0.1.5 // indirect - github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 // indirect github.com/xiang90/probing v0.0.0-20221125231312-a49e3df8f510 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.25.0 // indirect - golang.org/x/crypto v0.12.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/text v0.12.0 // indirect @@ -56,6 +43,5 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a // indirect google.golang.org/grpc v1.57.0 // indirect google.golang.org/protobuf v1.31.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/go.sum b/go.sum index 5c70add..c09f39f 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,5 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b h1:C7KftnLh7dOqzNRs5dn/9yqMDvuqMn5RCglvV6bY758= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230811085725-1a7414bb7590 h1:zN5JKHvzCH5q+X6W6fZw1hZ18FTlmZdv+k5T2j+6/GQ= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230811085725-1a7414bb7590/go.mod h1:l9S40lsDnTd/VAZjh1kmfYvz0B9z+7oT86pMQ/KurWo= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230815040629-5c584bcb7691 h1:Mdk0bAao3Plp7ck7WdnQE5WJkHjmHRw6N+jE4u/1jbQ= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20230815040629-5c584bcb7691/go.mod h1:UxkrpJIdDdgGkLsfmYVkUjGEFwbRkxbplWqUitpfca0= -git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444 h1:JVp575weLUX4sfhgjjxotJPxfHio7Ua8KHH3LMRRs2E= -git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444/go.mod h1:IqS3vAMyt1fVCWS7RqGeUw1EFnL/ruUMha45G2T+YNM= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230811142316-14bc4c2c9de3 h1:nEvOvdfbgxnqh7Xdk2OO8SedrUu+U9+sFXGCzkWwqmE= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230811142316-14bc4c2c9de3/go.mod h1:kvjAbtGTo14gKCS0X4rxnb2sPkskHOUy2NXcx34t6Mw= -github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= -github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= -github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -37,8 +23,6 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= -github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -58,8 +42,8 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= @@ -80,16 +64,10 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= -github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= @@ -104,8 +82,6 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= -github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= -github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -127,8 +103,6 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= -go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= -go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= @@ -138,7 +112,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= -golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -156,8 +129,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -199,12 +170,10 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20230814215434-ca7cfce7776a h1:+pp9kgkk8qyqFeQyXoDBFI4U+YYB15cmRkQ3pi6v/h4= google.golang.org/genproto v0.0.0-20230814215434-ca7cfce7776a/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4= @@ -216,8 +185,6 @@ google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/lease.go b/lease.go index 70649b8..765d7de 100644 --- a/lease.go +++ b/lease.go @@ -20,7 +20,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 6:06 下午 2021/11/23 -func LeaseOnce(ctx context.Context, key string, val string, ttl int64) error { +func (wc *WrapperClient) LeaseOnce(ctx context.Context, key string, val string, ttl int64) error { if ttl <= 0 { return errors.New("lease time must be more than 0") } @@ -37,15 +37,15 @@ func LeaseOnce(ctx context.Context, key string, val string, ttl int64) error { ctx, cancelFunc = context.WithCancel(ctx) defer cancelFunc() // 创建一个5秒的租约 - if resp, err = Client.Grant(ctx, ttl); err != nil { + if resp, err = wc.client.Grant(ctx, ttl); err != nil { return errors.New("lease grant error : " + err.Error()) } // ttl 秒钟之后, 这个key就会被移除 - if _, err = Client.Put(ctx, key, val, clientv3.WithLease(resp.ID)); err != nil { + if _, err = wc.client.Put(ctx, key, val, clientv3.WithLease(resp.ID)); err != nil { return errors.New("lease key put fail : " + err.Error()) } - _, err = Client.KeepAliveOnce(ctx, resp.ID) + _, err = wc.client.KeepAliveOnce(ctx, resp.ID) return err } @@ -54,7 +54,7 @@ func LeaseOnce(ctx context.Context, key string, val string, ttl int64) error { // Author : go_developer@163.com<白茶清欢> // // Date : 7:40 下午 2021/11/23 -func LeaseKeepAliveForever(ctx context.Context, key string, val string, ttl int64, keepAliveHandler LeaseKeepALiveHandler) error { +func (wc *WrapperClient) LeaseKeepAliveForever(ctx context.Context, key string, val string, ttl int64, keepAliveHandler LeaseKeepALiveHandler) error { if ttl <= 0 { return errors.New("lease time must be more than 0") } @@ -67,16 +67,16 @@ func LeaseKeepAliveForever(ctx context.Context, key string, val string, ttl int6 err error ) // 创建一个5秒的租约 - if resp, err = Client.Grant(ctx, ttl); err != nil { + if resp, err = wc.client.Grant(ctx, ttl); err != nil { return errors.New("lease grant error : " + err.Error()) } // ttl 秒钟之后, 这个key就会被移除 - if _, err = Client.Put(context.TODO(), key, val, clientv3.WithLease(resp.ID)); err != nil { + if _, err = wc.client.Put(context.TODO(), key, val, clientv3.WithLease(resp.ID)); err != nil { return errors.New("lease key put fail : " + err.Error()) } // the key will be kept forever - if respChan, err = Client.KeepAlive(ctx, resp.ID); nil != err { + if respChan, err = wc.client.KeepAlive(ctx, resp.ID); nil != err { return errors.New("lease keep alive fail : " + err.Error()) } leaseData := &LeaseKeepAliveData{ @@ -107,7 +107,7 @@ func LeaseKeepAliveForever(ctx context.Context, key string, val string, ttl int6 // Author : go_developer@163.com<白茶清欢> // // Date : 8:05 下午 2021/11/23 -func LeaseKeepAliveWithDuration(ctx context.Context, key string, val string, ttl int64, keepAliveHandler LeaseKeepALiveHandler, cancelLeaseChan chan *LeaseKeepAliveData, maxCnt int64) (*LeaseKeepAliveData, error) { +func (wc *WrapperClient) LeaseKeepAliveWithDuration(ctx context.Context, key string, val string, ttl int64, keepAliveHandler LeaseKeepALiveHandler, cancelLeaseChan chan *LeaseKeepAliveData, maxCnt int64) (*LeaseKeepAliveData, error) { if ttl <= 0 { return nil, errors.New("lease time must be more than 0") } @@ -138,17 +138,17 @@ func LeaseKeepAliveWithDuration(ctx context.Context, key string, val string, ttl HasFinish: false, } // 创建一个 ttl 秒的租约 - if resp, err = Client.Grant(ctx, ttl); err != nil { + if resp, err = wc.client.Grant(ctx, ttl); err != nil { return nil, errors.New("lease grant error : " + err.Error()) } leaseData.StartTime = time.Now().Unix() // ttl 秒钟之后, 这个key就会被移除 - if _, err = Client.Put(context.TODO(), key, val, clientv3.WithLease(resp.ID)); err != nil { + if _, err = wc.client.Put(context.TODO(), key, val, clientv3.WithLease(resp.ID)); err != nil { return nil, errors.New("lease key put fail : " + err.Error()) } // the key will be kept forever - if respChan, err = Client.KeepAlive(ctx, resp.ID); nil != err { + if respChan, err = wc.client.KeepAlive(ctx, resp.ID); nil != err { return nil, errors.New("lease keep alive fail : " + err.Error()) } diff --git a/lease_test.go b/lease_test.go deleted file mode 100644 index d90da03..0000000 --- a/lease_test.go +++ /dev/null @@ -1,68 +0,0 @@ -// Package etcd... -// -// Description : etcd... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2021-11-24 11:40 上午 -package etcd - -import ( - "fmt" - "testing" - "time" -) - -// TestLeaseOnce ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 7:37 下午 2021/11/23 -func TestLeaseOnce(t *testing.T) { - key := "lock" - fmt.Println(LeaseOnce(nil, key, "lock", 10)) - for i := 0; i < 15; i++ { - fmt.Println(Get(nil, key, 1)) - time.Sleep(time.Second) - } -} - -// TestLeaseKeepAliveForever ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 7:54 下午 2021/11/23 -func TestLeaseKeepAliveForever(t *testing.T) { - key := "lock" - keepAliveHandler := func(data *LeaseKeepAliveData) { - fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) - } - go func() { - fmt.Println(LeaseKeepAliveForever(nil, key, "lock", 10, keepAliveHandler)) - }() - for i := 0; i < 15; i++ { - r, e := Get(nil, key, 1) - fmt.Println("读取", r, e) - time.Sleep(time.Second) - } -} - -// TestLeaseKeepAliveWithDuration ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:56 下午 2021/11/23 -func TestLeaseKeepAliveWithDuration(t *testing.T) { - key := "lock" - keepAliveHandler := func(data *LeaseKeepAliveData) { - fmt.Println(key, data.LeaseDetail.ID, data.LeaseDetail.TTL, data.LeaseCnt) - } - go func() { - fmt.Println(LeaseKeepAliveWithDuration(nil, key, "lock", 1, keepAliveHandler, nil, 5)) - }() - for i := 0; i < 15; i++ { - r, e := Get(nil, key, 1) - fmt.Println("读取", r, e) - time.Sleep(time.Second) - } -} diff --git a/string.go b/string.go index 327c377..8bc3ec3 100644 --- a/string.go +++ b/string.go @@ -11,8 +11,8 @@ import ( "context" "time" + "github.com/coreos/etcd/mvcc/mvccpb" "go.etcd.io/etcd/clientv3" - "go.etcd.io/etcd/mvcc/mvccpb" ) // Put put数据 @@ -20,7 +20,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 12:09 下午 2021/11/23 -func Put(ctx context.Context, key string, val string, operateTimeout time.Duration) error { +func (wc *WrapperClient) Put(ctx context.Context, key string, val string, operateTimeout time.Duration) error { if operateTimeout <= 0 { operateTimeout = DefaultPutTimeout } @@ -32,7 +32,7 @@ func Put(ctx context.Context, key string, val string, operateTimeout time.Durati ctx = context.Background() } ctx, cancel = context.WithTimeout(ctx, operateTimeout) - _, err = Client.Put(ctx, key, val) + _, err = wc.client.Put(ctx, key, val) cancel() return err } @@ -42,7 +42,7 @@ func Put(ctx context.Context, key string, val string, operateTimeout time.Durati // Author : go_developer@163.com<白茶清欢> // // Date : 11:17 上午 2021/11/24 -func GetWithOption(ctx context.Context, key string, operateTimeout time.Duration, optionList ...clientv3.OpOption) ( +func (wc *WrapperClient) GetWithOption(ctx context.Context, key string, operateTimeout time.Duration, optionList ...clientv3.OpOption) ( []*mvccpb.KeyValue, error, ) { @@ -59,7 +59,7 @@ func GetWithOption(ctx context.Context, key string, operateTimeout time.Duration } ctx, cancel = context.WithTimeout(context.Background(), operateTimeout) defer cancel() - if result, err = Client.Get(ctx, key, optionList...); err != nil { + if result, err = wc.client.Get(ctx, key, optionList...); err != nil { return nil, err } return result.Kvs, nil @@ -70,11 +70,11 @@ func GetWithOption(ctx context.Context, key string, operateTimeout time.Duration // Author : go_developer@163.com<白茶清欢> // // Date : 12:09 下午 2021/11/23 -func Get(ctx context.Context, key string, operateTimeout time.Duration) ( +func (wc *WrapperClient) Get(ctx context.Context, key string, operateTimeout time.Duration) ( []*mvccpb.KeyValue, error, ) { - return GetWithOption(ctx, key, operateTimeout) + return wc.GetWithOption(ctx, key, operateTimeout) } // GetWithPrefix 使用key前缀查找 @@ -82,7 +82,7 @@ func Get(ctx context.Context, key string, operateTimeout time.Duration) ( // Author : go_developer@163.com<白茶清欢> // // Date : 10:58 上午 2021/11/24 -func GetWithPrefix(ctx context.Context, keyPrefix string, operateTimeout time.Duration) ( +func (wc *WrapperClient) GetWithPrefix(ctx context.Context, keyPrefix string, operateTimeout time.Duration) ( map[string]*mvccpb.KeyValue, error, ) { @@ -90,7 +90,7 @@ func GetWithPrefix(ctx context.Context, keyPrefix string, operateTimeout time.Du result []*mvccpb.KeyValue err error ) - if result, err = GetWithOption(ctx, keyPrefix, operateTimeout, clientv3.WithPrefix()); nil != err { + if result, err = wc.GetWithOption(ctx, keyPrefix, operateTimeout, clientv3.WithPrefix()); nil != err { return make(map[string]*mvccpb.KeyValue), err } formatResult := make(map[string]*mvccpb.KeyValue) diff --git a/string_test.go b/string_test.go deleted file mode 100644 index 5b4ee32..0000000 --- a/string_test.go +++ /dev/null @@ -1,56 +0,0 @@ -// Package etcd... -// -// Description : etcd... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2021-11-23 12:27 下午 -package etcd - -import ( - "fmt" - "git.zhangdeman.cn/zhangdeman/wrapper" - "testing" - - "go.etcd.io/etcd/clientv3" -) - -func init() { - err := InitEtcdClient(clientv3.Config{ - Endpoints: []string{"localhost:2379"}, - }) - if nil != err { - panic(err.Error()) - } -} - -// TestPut ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:27 下午 2021/11/23 -func TestPut(t *testing.T) { - fmt.Println(Put(nil, "name", "zhangdeman", 0)) -} - -// TestGet ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:30 下午 2021/11/23 -func TestGet(t *testing.T) { - fmt.Println(Get(nil, "name", 0)) -} - -// TestGetWithPrefix 根据key前缀读取数据 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:01 上午 2021/11/24 -func TestGetWithPrefix(t *testing.T) { - prefix := "/test/dir/" - for i := 0; i < 10; i++ { - _ = Put(nil, fmt.Sprintf("%s%d", prefix, i), wrapper.StringFromRandom(8, "").Value(), 0) - } - fmt.Println(GetWithPrefix(nil, prefix, 0)) -} diff --git a/watch.go b/watch.go index 22ecc6a..95f9af4 100644 --- a/watch.go +++ b/watch.go @@ -20,7 +20,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 2:29 下午 2021/11/26 -func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc WatcherHandler, optionList ...clientv3.OpOption) { +func (wc *WrapperClient) WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc WatcherHandler, optionList ...clientv3.OpOption) { if nil == callbackFunc { // 变化之后,没有任何逻辑处理,视为不需要监听变化 return @@ -29,7 +29,7 @@ func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc Watch ctx = context.Background() } - rch := Client.Watch(ctx, watchKey, optionList...) // <-chan WatchResponse + rch := wc.client.Watch(ctx, watchKey, optionList...) // <-chan WatchResponse for watchResp := range rch { for _, ev := range watchResp.Events { callbackFunc(ev) @@ -42,8 +42,8 @@ func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc Watch // Author : go_developer@163.com<白茶清欢> // // Date : 2:58 下午 2021/11/23 -func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) { - WatchKeyWithOption(ctx, watchKey, callbackFunc) +func (wc *WrapperClient) WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) { + wc.WatchKeyWithOption(ctx, watchKey, callbackFunc) } // WatchWithKeyPrefix ... @@ -51,8 +51,8 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) // Author : go_developer@163.com<白茶清欢> // // Date : 2:30 下午 2021/11/26 -func WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc WatcherHandler) { - WatchKeyWithOption(ctx, watchKey, callbackFunc, clientv3.WithPrefix()) +func (wc *WrapperClient) WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc WatcherHandler) { + wc.WatchKeyWithOption(ctx, watchKey, callbackFunc, clientv3.WithPrefix()) } // WatchKeyWithCancel 可以随时取消的 @@ -60,7 +60,7 @@ func WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc Watch // Author : go_developer@163.com<白茶清欢> // // Date : 3:16 下午 2021/11/23 -func WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc WatcherHandler, cancelChan chan interface{}, cancelDealFunc CancelWatcherHandler) { +func (wc *WrapperClient) WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc WatcherHandler, cancelChan chan interface{}, cancelDealFunc CancelWatcherHandler) { if nil == callbackFunc { // 变化之后,没有任何逻辑处理,视为不需要监听变化 return @@ -74,7 +74,7 @@ func WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc Watch ctx, cancelFunc = context.WithCancel(ctx) defer cancelFunc() - rch := Client.Watch(context.Background(), watchKey) // <-chan WatchResponse + rch := wc.client.Watch(context.Background(), watchKey) // <-chan WatchResponse hasFinish := false for { @@ -104,7 +104,7 @@ func WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc Watch // Author : go_developer@163.com<白茶清欢> // // Date : 4:35 下午 2021/11/23 -func WatchKeyOnce(ctx context.Context, watchKey string, callbackFunc WatcherHandler, timeout time.Duration, timeoutHandler TimeoutWatcherHandler) { +func (wc *WrapperClient) WatchKeyOnce(ctx context.Context, watchKey string, callbackFunc WatcherHandler, timeout time.Duration, timeoutHandler TimeoutWatcherHandler) { if nil == callbackFunc { // 变化之后,没有任何逻辑处理,视为不需要监听变化 return @@ -123,7 +123,7 @@ func WatchKeyOnce(ctx context.Context, watchKey string, callbackFunc WatcherHand ) ctx, cancelFunc = context.WithCancel(ctx) defer cancelFunc() - rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse + rch := wc.client.Watch(ctx, watchKey) // <-chan WatchResponse select { case <-time.After(timeout): if nil != timeoutHandler { diff --git a/watch_test.go b/watch_test.go deleted file mode 100644 index 18dd513..0000000 --- a/watch_test.go +++ /dev/null @@ -1,134 +0,0 @@ -// Package etcd... -// -// Description : etcd... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2021-11-24 11:40 上午 -package etcd - -import ( - "fmt" - "testing" - "time" - - "go.etcd.io/etcd/clientv3" -) - -// TestWatchKey ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 3:06 下午 2021/11/23 -func TestWatchKey(t *testing.T) { - key := "name" - dealFunc := func(data *clientv3.Event) { - fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision) - } - go func() { - for i := 0; i < 30; i++ { - _ = Put(nil, key, fmt.Sprintf("test-%d", i), 0) - time.Sleep(time.Second) - } - }() - WatchKey(nil, key, dealFunc) -} - -// TestWatchKeyWithCancel ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 3:57 下午 2021/11/23 -func TestWatchKeyWithCancel(t *testing.T) { - key := "name" - dealFunc := func(data *clientv3.Event) { - fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision) - } - cancelFunc := func(key string, data interface{}) { - fmt.Println("取消监听 : ", key, data) - } - cancelChan := make(chan interface{}, 1) - go func() { - for i := 0; i < 30; i++ { - _ = Put(nil, key, fmt.Sprintf("test-%d", i), 0) - time.Sleep(time.Second) - } - time.Sleep(10 * time.Second) - cancelChan <- "Hello World" - }() - WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc) -} - -// TestWatchKeyWithCancelByChangeCallback ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 4:27 下午 2021/11/23 -func TestWatchKeyWithCancelByChangeCallback(t *testing.T) { - key := "name" - cancelChan := make(chan interface{}, 1) - - dealFunc := func(data *clientv3.Event) { - fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision) - if string(data.Kv.Value) == "test-29" { - cancelChan <- "Hello World!" - } - } - - cancelFunc := func(key string, data interface{}) { - fmt.Println("取消监听 : ", key, data) - } - - go func() { - for i := 0; i < 30; i++ { - _ = Put(nil, key, fmt.Sprintf("test-%d", i), 0) - time.Sleep(time.Second) - } - }() - WatchKeyWithCancel(nil, key, dealFunc, cancelChan, cancelFunc) -} - -// TestWatchKeyOnce ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 4:50 下午 2021/11/23 -func TestWatchKeyOnce(t *testing.T) { - key := "name" - dealFunc := func(data *clientv3.Event) { - fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision) - } - - timeoutFunc := func(key string, timeout time.Duration) { - fmt.Println("监听超时", key, timeout) - } - - timeout := 10 * time.Second - go func() { - for i := 0; i < 30; i++ { - _ = Put(nil, key, fmt.Sprintf("test-%d", i), 0) - time.Sleep(time.Second) - } - }() - WatchKeyOnce(nil, key, dealFunc, timeout, timeoutFunc) -} - -// TestWatchKeyOnceForTimeout ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 4:53 下午 2021/11/23 -func TestWatchKeyOnceForTimeout(t *testing.T) { - key := "name" - dealFunc := func(data *clientv3.Event) { - fmt.Println(string(data.Kv.Key), string(data.Kv.Value), data.Kv.Version, data.Kv.CreateRevision, data.Kv.ModRevision) - } - - timeoutFunc := func(key string, timeout time.Duration) { - fmt.Println("监听超时", key, timeout) - } - - timeout := time.Second - - WatchKeyOnce(nil, key, dealFunc, timeout, timeoutFunc) -}