-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sarthak | Every worker now generates a unique RequestId
- Loading branch information
1 parent
f4e15d5
commit 17ee13c
Showing
6 changed files
with
76 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package workers | ||
|
||
import "sync/atomic" | ||
|
||
// RequestId generates a unique request id for each request. | ||
type RequestId struct { | ||
next atomic.Uint64 | ||
} | ||
|
||
// NewRequestId creates a new instance of RequestId. | ||
func NewRequestId() *RequestId { | ||
return &RequestId{} | ||
} | ||
|
||
// Next creates new request id | ||
func (requestId *RequestId) Next() uint64 { | ||
return requestId.next.Add(1) | ||
} |
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,46 @@ | ||
package workers | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"sort" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestNewRequestId(t *testing.T) { | ||
requestId := NewRequestId() | ||
assert.Equal(t, uint64(1), requestId.Next()) | ||
} | ||
|
||
func TestNewRequestIdConcurrently(t *testing.T) { | ||
var requestIds []uint64 | ||
var lock sync.Mutex | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(100) | ||
|
||
requestId := NewRequestId() | ||
for goroutineId := 1; goroutineId <= 100; goroutineId++ { | ||
go func() { | ||
defer func() { | ||
lock.Unlock() | ||
wg.Done() | ||
}() | ||
|
||
lock.Lock() | ||
requestIds = append(requestIds, requestId.Next()) | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
sort.Slice(requestIds, func(i, j int) bool { | ||
return requestIds[i] < requestIds[j] | ||
}) | ||
|
||
var expectedRequestIds []uint64 | ||
for id := uint64(1); id <= 100; id++ { | ||
expectedRequestIds = append(expectedRequestIds, id) | ||
} | ||
|
||
assert.Equal(t, expectedRequestIds, requestIds) | ||
} |
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