-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
b10845d
commit 039b7fb
Showing
9 changed files
with
181 additions
and
130 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
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,75 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/dipdup-net/metadata/cmd/metadata/models" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// ContractService - | ||
type ContractService struct { | ||
repo models.ContractRepository | ||
handler func(ctx context.Context, contract models.ContractMetadata) error | ||
workers chan struct{} | ||
wg sync.WaitGroup | ||
} | ||
|
||
// NewContractService - | ||
func NewContractService(repo models.ContractRepository, handler func(context.Context, models.ContractMetadata) error) *ContractService { | ||
return &ContractService{ | ||
repo: repo, | ||
handler: handler, | ||
workers: make(chan struct{}, 10), | ||
} | ||
} | ||
|
||
// Start - | ||
func (s *ContractService) Start(ctx context.Context) { | ||
s.wg.Add(1) | ||
go s.manager(ctx) | ||
} | ||
|
||
// Close - | ||
func (s *ContractService) Close() error { | ||
s.wg.Wait() | ||
|
||
close(s.workers) | ||
return nil | ||
} | ||
|
||
func (s *ContractService) manager(ctx context.Context) { | ||
defer s.wg.Done() | ||
|
||
if s.handler == nil { | ||
log.Warn("processor contract metadata service: service without handler") | ||
return | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
unresolved, err := s.repo.GetContractMetadata(models.StatusNew, 15, 0) | ||
if err != nil { | ||
log.Error(err) | ||
continue | ||
} | ||
for i := range unresolved { | ||
s.workers <- struct{}{} | ||
s.wg.Add(1) | ||
go func(contract models.ContractMetadata) { | ||
defer func() { | ||
<-s.workers | ||
s.wg.Done() | ||
}() | ||
if err := s.handler(ctx, contract); err != nil { | ||
log.Error(err) | ||
} | ||
}(unresolved[i]) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,75 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/dipdup-net/metadata/cmd/metadata/models" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// TokenService - | ||
type TokenService struct { | ||
repo models.TokenRepository | ||
handler func(ctx context.Context, contract models.TokenMetadata) error | ||
workers chan struct{} | ||
wg sync.WaitGroup | ||
} | ||
|
||
// NewContractService - | ||
func NewTokenService(repo models.TokenRepository, handler func(context.Context, models.TokenMetadata) error) *TokenService { | ||
return &TokenService{ | ||
repo: repo, | ||
handler: handler, | ||
workers: make(chan struct{}, 10), | ||
} | ||
} | ||
|
||
// Start - | ||
func (s *TokenService) Start(ctx context.Context) { | ||
s.wg.Add(1) | ||
go s.manager(ctx) | ||
} | ||
|
||
// Close - | ||
func (s *TokenService) Close() error { | ||
s.wg.Wait() | ||
|
||
close(s.workers) | ||
return nil | ||
} | ||
|
||
func (s *TokenService) manager(ctx context.Context) { | ||
defer s.wg.Done() | ||
|
||
if s.handler == nil { | ||
log.Warn("processor token metadata service: service without handler") | ||
return | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
unresolved, err := s.repo.GetTokenMetadata(models.StatusNew, 15, 0) | ||
if err != nil { | ||
log.Error(err) | ||
continue | ||
} | ||
for i := range unresolved { | ||
s.workers <- struct{}{} | ||
s.wg.Add(1) | ||
go func(contract models.TokenMetadata) { | ||
defer func() { | ||
<-s.workers | ||
s.wg.Done() | ||
}() | ||
if err := s.handler(ctx, contract); err != nil { | ||
log.Error(err) | ||
} | ||
}(unresolved[i]) | ||
} | ||
} | ||
} | ||
} |
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