Skip to content

Commit

Permalink
Merge pull request #1283 from anyproto/go-3541-async-fetch-global-name
Browse files Browse the repository at this point in the history
GO-3541 Async fetchGlobalName
  • Loading branch information
KirillSto authored May 31, 2024
2 parents 9efb6cd + ae5228f commit bede287
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 14 additions & 5 deletions core/identity/ownidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *ownProfileSubscription) run(ctx context.Context) (err error) {
s.handleOwnProfileDetails(records[0].Details)
}

s.fetchGlobalName()
go s.fetchGlobalName(s.componentCtx, s.namingService)

go func() {
for {
Expand Down Expand Up @@ -205,8 +205,12 @@ func (s *ownProfileSubscription) handleOwnProfileDetails(profileDetails *types.S
s.enqueuePush()
}

func (s *ownProfileSubscription) fetchGlobalName() {
response, err := s.namingService.GetNameByAnyId(s.componentCtx, &nameserviceproto.NameByAnyIdRequest{AnyAddress: s.myIdentity})
func (s *ownProfileSubscription) fetchGlobalName(ctx context.Context, ns nameserviceclient.AnyNsClientService) {
if ns == nil {
log.Error("error fetching global name of our own identity from Naming Service as the service is not initialized")
return
}
response, err := ns.GetNameByAnyId(ctx, &nameserviceproto.NameByAnyIdRequest{AnyAddress: s.myIdentity})
if err != nil || response == nil {
log.Error("error fetching global name of our own identity from Naming Service", zap.Error(err))
return
Expand All @@ -215,11 +219,16 @@ func (s *ownProfileSubscription) fetchGlobalName() {
log.Debug("globalName was not found for our own identity in Naming Service")
return
}
s.handleGlobalNameUpdate(response.Name)
s.updateGlobalName(response.Name)
}

func (s *ownProfileSubscription) updateGlobalName(globalName string) {
s.globalNameUpdatedCh <- globalName
select {
case <-s.componentCtx.Done():
return
case s.globalNameUpdatedCh <- globalName:
return
}
}

func (s *ownProfileSubscription) handleGlobalNameUpdate(globalName string) {
Expand Down
6 changes: 6 additions & 0 deletions core/identity/ownidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func TestOwnProfileSubscription(t *testing.T) {
err := fx.run(context.Background())
require.NoError(t, err)

time.Sleep(testBatchTimeout / 4)

fx.objectStoreFixture.AddObjects(t, []objectstore.TestObject{
{
bundle.RelationKeyId: pbtypes.String(testProfileObjectId),
Expand Down Expand Up @@ -201,6 +203,8 @@ func TestOwnProfileSubscription(t *testing.T) {
err := fx.run(context.Background())
require.NoError(t, err)

time.Sleep(testBatchTimeout / 4)

fx.updateGlobalName(newName)

time.Sleep(2 * testBatchTimeout)
Expand Down Expand Up @@ -254,6 +258,8 @@ func TestOwnProfileSubscription(t *testing.T) {
err := fx.run(context.Background())
require.NoError(t, err)

time.Sleep(testBatchTimeout / 4)

fx.objectStoreFixture.AddObjects(t, []objectstore.TestObject{
{
bundle.RelationKeyId: pbtypes.String(testProfileObjectId),
Expand Down

0 comments on commit bede287

Please sign in to comment.