2023-08-29 00:02:12 +08:00
|
|
|
package pub_client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
2023-08-29 00:02:12 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2024-01-28 15:43:22 +08:00
|
|
|
|
2023-08-29 00:02:12 +08:00
|
|
|
func (p *TopicPublisher) Publish(key, value []byte) error {
|
2023-12-12 04:05:54 +08:00
|
|
|
hashKey := util.HashToInt32(key) % pub_balancer.MaxPartitionCount
|
2023-08-29 00:02:12 +08:00
|
|
|
if hashKey < 0 {
|
|
|
|
hashKey = -hashKey
|
|
|
|
}
|
2024-01-28 15:43:22 +08:00
|
|
|
inputBuffer, found := p.partition2Buffer.Floor(hashKey+1, hashKey+1)
|
2023-08-29 00:02:12 +08:00
|
|
|
if !found {
|
2024-01-28 15:43:22 +08:00
|
|
|
return fmt.Errorf("no input buffer found for key %d", hashKey)
|
2023-08-29 00:02:12 +08:00
|
|
|
}
|
2024-01-28 15:43:22 +08:00
|
|
|
|
|
|
|
return inputBuffer.Enqueue(&mq_pb.DataMessage{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
})
|
2023-08-29 00:02:12 +08:00
|
|
|
}
|