plugin wx getSrv sync
This commit is contained in:
parent
8e2fac56ba
commit
95f3f9918e
|
@ -7,9 +7,31 @@ import (
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var instance *cashcoupons.CouponApiService
|
||||||
|
var once sync.Once
|
||||||
|
var onceError error
|
||||||
|
var onceErrorMutex sync.Mutex
|
||||||
|
|
||||||
func getSrv(ctx context.Context, config *Config) (*cashcoupons.CouponApiService, error) {
|
func getSrv(ctx context.Context, config *Config) (*cashcoupons.CouponApiService, error) {
|
||||||
|
onceErrorMutex.Lock()
|
||||||
|
defer onceErrorMutex.Unlock()
|
||||||
|
|
||||||
|
if onceError != nil {
|
||||||
|
instance = nil
|
||||||
|
onceError = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
once.Do(func() {
|
||||||
|
instance, onceError = newInstance(ctx, config)
|
||||||
|
})
|
||||||
|
|
||||||
|
return instance, onceError
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInstance(ctx context.Context, config *Config) (*cashcoupons.CouponApiService, error) {
|
||||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(config.PrivateKeyPath)
|
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(config.PrivateKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("load merchant private key error:%v", err)
|
return nil, fmt.Errorf("load merchant private key error:%v", err)
|
||||||
|
|
|
@ -63,7 +63,7 @@ func TestQuery(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v \n", got)
|
fmt.Printf("%+v \n", got)
|
||||||
//assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
|
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue