Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
safe write to network resource ops when consuming block data
Browse files Browse the repository at this point in the history
  • Loading branch information
itzmeanjan committed Jan 11, 2021
1 parent d058c85 commit 3a87b18
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/pubsub/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,42 @@ func (b *BlockConsumer) Send(msg string) bool {
user := db.GetUserFromAPIKey(b.DB, b.Request.APIKey)
if user == nil {

// -- Critical section of code begins
//
// Attempting to write to a network resource,
// shared among multiple go routines
b.Lock.Lock()

if err := b.Connection.WriteJSON(&SubscriptionResponse{
Code: 0,
Message: "Bad API Key",
}); err != nil {
log.Printf("[!] Failed to deliver bad API key message to client : %s\n", err.Error())
}

b.Lock.Unlock()
// -- ends here
return false

}

if !user.Enabled {

// -- Critical section of code begins
//
// Attempting to write to a network resource,
// shared among multiple go routines
b.Lock.Lock()

if err := b.Connection.WriteJSON(&SubscriptionResponse{
Code: 0,
Message: "Bad API Key",
}); err != nil {
log.Printf("[!] Failed to deliver bad API key message to client : %s\n", err.Error())
}

b.Lock.Unlock()
// -- ends here
return false

}
Expand All @@ -113,13 +129,21 @@ func (b *BlockConsumer) Send(msg string) bool {
// if client has crossed it's allowed data delivery limit
if !db.IsUnderRateLimit(b.DB, b.UserAddress.Hex()) {

// -- Critical section of code begins
//
// Attempting to write to a network resource,
// shared among multiple go routines
b.Lock.Lock()

if err := b.Connection.WriteJSON(&SubscriptionResponse{
Code: 0,
Message: "Crossed Allowed Rate Limit",
}); err != nil {
log.Printf("[!] Failed to deliver rate limit crossed message to client : %s\n", err.Error())
}

b.Lock.Unlock()
// -- ends here
return false

}
Expand Down Expand Up @@ -149,6 +173,13 @@ func (b *BlockConsumer) Send(msg string) bool {
// connection ( connection might be already closed though )
func (b *BlockConsumer) SendData(data interface{}) bool {

// -- Critical section of code begins
//
// Attempting to write to a network resource,
// shared among multiple go routines
b.Lock.Lock()
defer b.Lock.Unlock()

if err := b.Connection.WriteJSON(data); err != nil {
log.Printf("[!] Failed to deliver `block` data to client : %s\n", err.Error())
return false
Expand Down Expand Up @@ -177,6 +208,13 @@ func (b *BlockConsumer) Unsubscribe() {
Message: fmt.Sprintf("Unsubscribed from `%s`", b.Request.Topic()),
}

// -- Critical section of code begins
//
// Attempting to write to a network resource,
// shared among multiple go routines
b.Lock.Lock()
defer b.Lock.Unlock()

if err := b.Connection.WriteJSON(resp); err != nil {
log.Printf("[!] Failed to deliver `%s` unsubscription confirmation to client : %s\n", b.Request.Topic(), err.Error())
}
Expand Down

0 comments on commit 3a87b18

Please sign in to comment.