LinkinStars commented on code in PR #1453: URL: https://github.com/apache/answer/pull/1453#discussion_r2612801738
########## internal/base/queue/queue.go: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package queue + +import ( + "context" + "sync" + + "github.com/segmentfault/pacman/log" +) + +// Queue is a generic message queue service that processes messages asynchronously. +// It is thread-safe and supports graceful shutdown. +type Queue[T any] struct { + name string + queue chan T + handler func(ctx context.Context, msg T) error + mu sync.RWMutex + closed bool + wg sync.WaitGroup +} + +// New creates a new queue with the given name and buffer size. +func New[T any](name string, bufferSize int) *Queue[T] { + q := &Queue[T]{ + name: name, + queue: make(chan T, bufferSize), + } + q.startWorker() + return q +} + +// Send enqueues a message to be processed asynchronously. +// It will block if the queue is full. +func (q *Queue[T]) Send(ctx context.Context, msg T) { + q.mu.RLock() + closed := q.closed + q.mu.RUnlock() + + if closed { + log.Warnf("[%s] queue is closed, dropping message", q.name) + return + } + + select { + case q.queue <- msg: + log.Debugf("[%s] enqueued message: %+v", q.name, msg) + case <-ctx.Done(): + log.Warnf("[%s] context cancelled while sending message", q.name) + } +} Review Comment: This function may have a small issue. After determining the closed state, it immediately unlocks the channel and sends a message. If the channel is closed after unlocking, sending the message may result in a “send on closed channel” error. ########## internal/service/activityqueue/activity_queue.go: ########## @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package activityqueue + +import ( + "github.com/apache/answer/internal/base/queue" + "github.com/apache/answer/internal/schema" +) + +type Service = *queue.Queue[*schema.ActivityMsg] + +func NewService() Service { + return queue.New[*schema.ActivityMsg]("activity", 128) +} Review Comment: We believe defining an interface would be more user-friendly. Such as ```go type Service interface { Send(ctx context.Context, msg *schema.ActivityMsg) RegisterHandler(handler func(ctx context.Context, msg *schema.ActivityMsg) error) Close() } func NewService() Service { return queue.New[*schema.ActivityMsg]("activity", 128) } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
