-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package broker | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/ispiroglu/mercurius/internal/logger" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type SubscriberRepository struct { | ||
logger *zap.Logger | ||
StreamPools sync.Map | ||
poolCount atomic.Uint32 | ||
} | ||
|
||
func NewSubscriberRepository() *SubscriberRepository { | ||
return &SubscriberRepository{ | ||
logger: logger.NewLogger(), | ||
StreamPools: sync.Map{}, | ||
} | ||
} | ||
|
||
func (r *SubscriberRepository) Unsubscribe(subscriber *Subscriber) error { | ||
|
||
if _, ok := r.StreamPools.Load(subscriber.Id); !ok { | ||
return status.Error(codes.NotFound, "Cannot found subscriber at repository.") | ||
} | ||
|
||
r.StreamPools.Delete(subscriber.Id) | ||
r.poolCount.Store( | ||
r.poolCount.Load() - 1, | ||
) | ||
return nil | ||
} | ||
|
||
func (r *SubscriberRepository) addSubscriber(ctx context.Context, id string, subName string, topicName string) (*Subscriber, error) { | ||
|
||
// Handle subName conflict? | ||
s := NewSubscriber(ctx, id, subName, topicName) | ||
pool, _ := r.StreamPools.LoadOrStore(subName, newStreamPool(subName)) | ||
|
||
pool.(*StreamPool).AddSubscriber(s) | ||
r.poolCount.Add(1) | ||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package broker | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ispiroglu/mercurius/internal/logger" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type TopicRepository struct { | ||
logger *zap.Logger | ||
Topics sync.Map | ||
} | ||
|
||
func NewTopicRepository() *TopicRepository { | ||
return &TopicRepository{ | ||
logger: logger.NewLogger(), | ||
Topics: sync.Map{}, | ||
} | ||
} | ||
|
||
func (r *TopicRepository) GetTopic(name string) (*Topic, error) { | ||
topic, exist := r.Topics.Load(name) | ||
if !exist { | ||
// r.logger.Warn("Could not found topic", zap.String("Topic", name)) | ||
return nil, status.Error(codes.NotFound, "cannot found the topic called:"+name) | ||
} | ||
|
||
return topic.(*Topic), nil | ||
} | ||
|
||
func (r *TopicRepository) CreateTopic(name string) (*Topic, error) { | ||
if _, err := r.GetTopic(name); err == nil { | ||
// r.logger.Warn("Cannot create the topic that already exists", zap.String("Topic", name)) | ||
return nil, status.Error(codes.AlreadyExists, "there is already a topic named:"+name) | ||
} | ||
|
||
createdTopic := newTopic(name) | ||
r.Topics.Store(name, createdTopic) | ||
|
||
return createdTopic, nil | ||
} | ||
|
||
func (r *TopicRepository) Unsubscribe(subscriber *Subscriber) { | ||
topic, err := r.GetTopic(subscriber.TopicName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := topic.SubscriberRepository.Unsubscribe(subscriber); err != nil { | ||
r.logger.Warn("", zap.Error(err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters