Skip to content

Commit

Permalink
fix bugs of pkg/notification
Browse files Browse the repository at this point in the history
  • Loading branch information
yxxchange committed Aug 11, 2023
1 parent 7648bb5 commit 9db9fdb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (n *notice) getUserNamespace(instance *TaskInstance, opt FilterFunc) []stri
namespaces = append(namespaces, namespace.Name)
}
}
return nil
return namespaces
}

func filter(ns string) bool {
Expand Down
2 changes: 1 addition & 1 deletion controllers/pkg/notification/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import (
"testing"
)

func TestEncodeTime(t *testing.T) {
func TestEncodeTime(_ *testing.T) {
fmt.Println(encodeTime())
}
6 changes: 2 additions & 4 deletions controllers/pkg/notification/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ Return values:
The function also generates a unique ID for each NotificationEvent. If the generation of the ID fails, it uses the current Unix timestamp as the ID.
*/
func NewNotificationEvent(title string, message string,
kind Kind, from string, level v1.Type) Event {

func NewNotificationEvent(title string, message string, kind Kind, from string, level v1.Type) Event {
id, err := randStringBytes(idLength)
if err != nil || id == "" {
id = strings.ToLower(strconv.Itoa(int(time.Now().Unix())))
Expand All @@ -170,7 +168,7 @@ func NewNotificationEvent(title string, message string,
}

func RandStrings(n int) string {
str, err := randStringBytes(idLength)
str, err := randStringBytes(n)
if err != nil || str == "" {
str = encodeTime()
}
Expand Down
15 changes: 6 additions & 9 deletions controllers/pkg/notification/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const maxBatchSize = 100
const maxChannelSize = 500

// the best practice of notification api is the following:
// 1. use a notification builder to build a notification event queue,such as:
// Queue := NotificationQueue{}
Expand Down Expand Up @@ -59,7 +56,7 @@ const maxChannelSize = 500
// 4. decoupling of message generation and delivery, along with error tolerance mechanisms
// 5. allow users to focus solely on important tasks.

type NotificationManager struct {
type Manager struct {
ctx context.Context
client client.Client
logger logr.Logger
Expand All @@ -69,8 +66,8 @@ type NotificationManager struct {
}

func NewNotificationManager(ctx context.Context, client client.Client,
logger logr.Logger, batchSize, channelSize int) *NotificationManager {
return &NotificationManager{
logger logr.Logger, batchSize, channelSize int) *Manager {
return &Manager{
ctx: ctx,
client: client,
logger: logger,
Expand All @@ -81,7 +78,7 @@ func NewNotificationManager(ctx context.Context, client client.Client,

// Run of the NotificationManager runs the notification manager.
// It writes the notifications in batches
func (nm *NotificationManager) Run() {
func (nm *Manager) Run() {
pool := NewPool(nm.batchSize)
pool.Run(nm.channelSize)
for _, notification := range nm.queue {
Expand All @@ -96,14 +93,14 @@ func (nm *NotificationManager) Run() {
pool.Wait()
}

func (nm *NotificationManager) Load(receivers *Receiver, events []Event) *NotificationManager {
func (nm *Manager) Load(receivers *Receiver, events []Event) *Manager {
for _, event := range events {
nm.loadNotification(receivers.receivers, event)
}
return nm
}

func (nm *NotificationManager) loadNotification(receivers []string, event Event) {
func (nm *Manager) loadNotification(receivers []string, event Event) {
for _, receiver := range receivers {
nm.queue = append(nm.queue, newNotification(receiver, event))
}
Expand Down

0 comments on commit 9db9fdb

Please sign in to comment.