Skip to content

Commit

Permalink
refactor: refactor function signatures and imports in notify and rout…
Browse files Browse the repository at this point in the history
…er packages

- Remove the import of `github.com/appleboy/gorush/logx` in `notify/feedback.go`
- Add the import of `github.com/appleboy/gorush/logx` in `notify/feedback.go`
- Change the signature of the `SendNotification` function in `notify/notification.go`
- Change the signature of the `Run` function in `notify/notification.go`
- Change the signature of the `handleNotification` function in `router/server.go`
- Change the signature of the `TestMain` function in `router/server_test.go`
- Change the signature of the `Send` method in `rpc/server.go`

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Oct 1, 2023
1 parent d4af7bc commit d7a0cb5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion notify/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"context"
"errors"
"github.com/appleboy/gorush/logx"
"net"
"net/http"
"strings"
"time"

"github.com/appleboy/gorush/logx"
)

var feedbackClient *http.Client
Expand Down
14 changes: 9 additions & 5 deletions notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,16 @@ func CheckPushConf(cfg *config.ConfYaml) error {
return nil
}

// SendNotification send notification
func SendNotification(req qcore.QueuedMessage, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
// SendNotification provide send notification.
func SendNotification(
ctx context.Context,
req qcore.QueuedMessage,
cfg *config.ConfYaml,
) (resp *ResponsePush, err error) {
v, ok := req.(*PushNotification)
if !ok {
if err = json.Unmarshal(req.Bytes(), &v); err != nil {
return
return nil, err
}
}

Expand All @@ -262,13 +266,13 @@ func SendNotification(req qcore.QueuedMessage, cfg *config.ConfYaml) (resp *Resp
}
}

return
return resp, err
}

// Run send notification
var Run = func(cfg *config.ConfYaml) func(ctx context.Context, msg qcore.QueuedMessage) error {
return func(ctx context.Context, msg qcore.QueuedMessage) error {
_, err := SendNotification(msg, cfg)
_, err := SendNotification(ctx, msg, cfg)
return err
}
}
2 changes: 1 addition & 1 deletion router/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func handleNotification(
func(msg *notify.PushNotification, cfg *config.ConfYaml) {
if err := q.QueueTask(func(ctx context.Context) error {
defer wg.Done()
resp, err := notify.SendNotification(msg, cfg)
resp, err := notify.SendNotification(ctx, msg, cfg)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion router/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestMain(m *testing.M) {
q = queue.NewPool(
int(cfg.Core.WorkerNum),
queue.WithFn(func(ctx context.Context, msg qcore.QueuedMessage) error {
_, err := notify.SendNotification(msg, cfg)
_, err := notify.SendNotification(ctx, msg, cfg)
return err
}),
queue.WithLogger(logx.QueueLogger()),
Expand Down
2 changes: 1 addition & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
}

go func() {
_, err := notify.SendNotification(&notification, s.cfg)
_, err := notify.SendNotification(ctx, &notification, s.cfg)
if err != nil {
logx.LogError.Error(err)
}
Expand Down

0 comments on commit d7a0cb5

Please sign in to comment.