-
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.
Revert "Revert "Revert "feat: 🎸 paymentサーバーをキューでなく確率で落とすように"""
- Loading branch information
1 parent
d8025ca
commit e9fd740
Showing
8 changed files
with
147 additions
and
74 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
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,78 @@ | ||
package payment | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/isucon/isucon14/bench/internal/concurrent" | ||
"golang.org/x/sync/semaphore" | ||
) | ||
|
||
type paymentQueue struct { | ||
ctx context.Context | ||
semaphore *semaphore.Weighted | ||
verifier Verifier | ||
processTime time.Duration | ||
acceptedPayments *concurrent.SimpleMap[string, *concurrent.SimpleSlice[*Payment]] | ||
errChan chan error | ||
} | ||
|
||
func newPaymentQueue(queueSize int, verifier Verifier, processTime time.Duration, errChan chan error) *paymentQueue { | ||
return &paymentQueue{ | ||
ctx: context.Background(), | ||
semaphore: semaphore.NewWeighted(int64(queueSize)), | ||
verifier: verifier, | ||
processTime: processTime, | ||
acceptedPayments: concurrent.NewSimpleMap[string, *concurrent.SimpleSlice[*Payment]](), | ||
errChan: errChan, | ||
} | ||
} | ||
|
||
func (q *paymentQueue) execute(p *Payment) { | ||
time.Sleep(q.processTime) | ||
p.Status = q.verifier.Verify(p) | ||
if p.Status.Err != nil { | ||
q.errChan <- p.Status.Err | ||
} | ||
close(p.processChan) | ||
} | ||
|
||
func (q *paymentQueue) tryProcess(p *Payment) bool { | ||
if !q.semaphore.TryAcquire(1) { | ||
return false | ||
} | ||
q.appendAcceptedPayments(p) | ||
|
||
go func() { | ||
defer q.semaphore.Release(1) | ||
q.execute(p) | ||
}() | ||
return true | ||
} | ||
|
||
func (q *paymentQueue) process(p *Payment) { | ||
// 遅かれ早かれ処理するため、受け入れた決済として保持しておく | ||
q.appendAcceptedPayments(p) | ||
|
||
q.semaphore.Acquire(q.ctx, 1) | ||
defer q.semaphore.Release(1) | ||
|
||
q.execute(p) | ||
} | ||
|
||
func (q *paymentQueue) appendAcceptedPayments(p *Payment) { | ||
payments, _ := q.acceptedPayments.GetOrSetDefault(p.Token, func() *concurrent.SimpleSlice[*Payment] { return concurrent.NewSimpleSlice[*Payment]() }) | ||
payments.Append(p) | ||
} | ||
|
||
func (q *paymentQueue) getAllAcceptedPayments(token string) []*Payment { | ||
payments, ok := q.acceptedPayments.Get(token) | ||
if !ok { | ||
return []*Payment{} | ||
} | ||
return payments.ToSlice() | ||
} | ||
|
||
func (q *paymentQueue) close() { | ||
q.ctx.Done() | ||
} |
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 was deleted.
Oops, something went wrong.