Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

おおまかにロジックを書いた #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/sinmetalcraft/lane

go 1.16
59 changes: 59 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package lane

import (
"context"
"fmt"
"sync"
"time"
)

type Service struct {
lanes map[string]*Buzzer
mu sync.Mutex
}

func NewService(ctx context.Context) (*Service, error) {
return &Service{
lanes: make(map[string]*Buzzer),
}, nil
}

type Buzzer struct {
GoSign chan bool
Expire time.Time
once sync.Once
}

func (s *Service) LaneUp(ctx context.Context, key string, timeout time.Duration) (buzzer *Buzzer, goSign bool) {
s.mu.Lock()
defer s.mu.Unlock()

buzzer, ok := s.lanes[key]
if !ok {
buzzer := &Buzzer{
GoSign: make(chan bool),
Expire: time.Now().Add(timeout),
}
s.lanes[key] = buzzer
return buzzer, true
}

return buzzer, false
}

func (s *Service) Done(ctx context.Context, key string) (err error) {
s.mu.Lock()
defer s.mu.Unlock()

buzzer, ok := s.lanes[key]
if !ok {
return fmt.Errorf("not found in the wait lane")
}

buzzer.once.Do(func() {
close(buzzer.GoSign)
})

delete(s.lanes, key)
return nil
}
64 changes: 64 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package lane_test

import (
"context"
"sync"
"testing"
"time"

"github.com/sinmetalcraft/lane"
)

func TestService_LaneUpAndDone(t *testing.T) {
ctx := context.Background()

s, err := lane.NewService(ctx)
if err != nil {
t.Fatal(err)
}

key := "hoge"
buzzer, goSign := s.LaneUp(ctx, key, 10*time.Second)
if buzzer == nil {
t.Error("buzzer is nil")
}
if e, g := true, goSign; e != g {
t.Errorf("want goSign %t but got %t", e, g)
}

receiveCounterCh := make(chan int)
wg := &sync.WaitGroup{}
const waitLineCount = 10
for i := 0; i < waitLineCount; i++ {
wg.Add(1)
go func(ctx context.Context, i int) {
buzzer, goSign := s.LaneUp(ctx, key, 10*time.Second)
if buzzer == nil {
t.Error("buzzer is nil")
}
if e, g := false, goSign; e != g {
t.Errorf("want goSign %t but got %t\n", e, g)
}
wg.Done() // 待ち行列に並んだことを通達

select {
case <-buzzer.GoSign:
receiveCounterCh <- i
case <-ctx.Done():
t.Error("context canceled")
}
}(ctx, i)
}

wg.Wait() // みんな待ち行列に並んで、待っている状態になった

// 処理が完了したら、待っているみんなに伝える
if err := s.Done(ctx, key); err != nil {
t.Fatal(err)
}

for i := 0; i < waitLineCount; i++ {
v := <-receiveCounterCh
t.Logf("done: %d\n", v)
}
}