diff --git a/bindings/alicloud/dingtalk/webhook/webhook.go b/bindings/alicloud/dingtalk/webhook/webhook.go index e435aa3c16..ad42320481 100644 --- a/bindings/alicloud/dingtalk/webhook/webhook.go +++ b/bindings/alicloud/dingtalk/webhook/webhook.go @@ -22,7 +22,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -54,7 +53,7 @@ type outgoingWebhook struct { handler bindings.Handler } -var webhooks = struct { // nolint: gochecknoglobals +var webhooks = struct { //nolint: gochecknoglobals sync.RWMutex m map[string]*outgoingWebhook }{m: make(map[string]*outgoingWebhook)} @@ -176,6 +175,8 @@ func (t *DingTalkWebhook) sendMessage(ctx context.Context, req *bindings.InvokeR return fmt.Errorf("dingtalk webhook error: post failed. %w", err) } defer func() { + // Drain before closing + _, _ = io.Copy(io.Discard, resp.Body) _ = resp.Body.Close() }() @@ -183,13 +184,8 @@ func (t *DingTalkWebhook) sendMessage(ctx context.Context, req *bindings.InvokeR return fmt.Errorf("dingtalk webhook error: post failed. status:%d", resp.StatusCode) } - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - return fmt.Errorf("dingtalk webhook error: read body failed. %w", err) - } - var rst webhookResult - err = json.Unmarshal(data, &rst) + err = json.NewDecoder(resp.Body).Decode(&rst) if err != nil { return fmt.Errorf("dingtalk webhook error: unmarshal body failed. %w", err) } @@ -206,7 +202,7 @@ func getPostURL(urlPath, secret string) (string, error) { return urlPath, nil } - timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10) + timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10) sign, err := sign(secret, timestamp) if err != nil { return urlPath, err diff --git a/bindings/alicloud/dingtalk/webhook/webhook_test.go b/bindings/alicloud/dingtalk/webhook/webhook_test.go index 3d0ff85177..24c8ac494a 100644 --- a/bindings/alicloud/dingtalk/webhook/webhook_test.go +++ b/bindings/alicloud/dingtalk/webhook/webhook_test.go @@ -60,7 +60,7 @@ func TestPublishMsg(t *testing.T) { //nolint:paralleltest require.NoError(t, err) req := &bindings.InvokeRequest{Data: []byte(msg), Operation: bindings.CreateOperation, Metadata: map[string]string{}} - _, err = d.Invoke(context.TODO(), req) + _, err = d.Invoke(context.Background(), req) require.NoError(t, err) } @@ -95,7 +95,7 @@ func TestBindingReadAndInvoke(t *testing.T) { //nolint:paralleltest require.NoError(t, err) req := &bindings.InvokeRequest{Data: []byte(msg), Operation: bindings.GetOperation, Metadata: map[string]string{}} - _, err = d.Invoke(context.TODO(), req) + _, err = d.Invoke(context.Background(), req) require.NoError(t, err) select {