Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.22.5

- name: Build
run: go build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module regproxy2

go 1.17
go 1.22.5

require (
go.mercari.io/go-dnscache v0.0.0-20210517095825-88b046eb94f2
go.uber.org/zap v1.19.1
golang.org/x/sync v0.7.0
)

require (
go.mercari.io/go-dnscache v0.0.0-20210517095825-88b046eb94f2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"strconv"
"sync"
"time"

dnscache "go.mercari.io/go-dnscache"
Expand All @@ -34,6 +35,7 @@ func errResp(resp http.ResponseWriter, e error) {
type RegProxy struct {
client *http.Client
upstreams map[string]*url.URL
writeLock sync.Mutex
handler http.Handler
}

Expand Down Expand Up @@ -130,6 +132,9 @@ func (self *RegProxy) register(resp http.ResponseWriter, req *http.Request) {
badRequest(resp, err.Error())
return
}

self.writeLock.Lock()
defer self.writeLock.Unlock()
log.Printf("Adding upstream %v", q)
self.upstreams[q.Name] = upstream
resp.WriteHeader(204)
Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"golang.org/x/sync/errgroup"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -63,6 +66,32 @@ func TestRegister(t *testing.T) {
}
}

func TestConcurrentRegister(t *testing.T) {
errorGroup, _ := errgroup.WithContext(context.Background())

withRegProxy(t, func(url string, t *testing.T) {
for i := 0; i < 100; i++ {
request := fmt.Sprintf("{\"name\":\"foo-%d\",\"callback\":\"baz\"}", i)
errorGroup.Go(func() error {
r, err := http.Post(url+"/register", "application/json", bytes.NewReader([]byte(request)))
if err != nil {
return err
}
if r.StatusCode != 204 {
return fmt.Errorf("Wrong status code from /register %d expected 204", r.StatusCode)
}

return nil
})
}

resultingError := errorGroup.Wait()
if resultingError != nil {
t.Fatal(resultingError)
}
})
}

func register(url string, u upstream, t *testing.T) {
b, _ := json.Marshal(u)
r, err := http.Post(url+"/register", "application/json", bytes.NewReader(b))
Expand Down