Initial commit
This commit is contained in:
26
cmd/fakeserver/main.go
Normal file
26
cmd/fakeserver/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func fakeHandler(w http.ResponseWriter, req *http.Request) {
|
||||
n := 200 + rand.Intn(600) // 200 - 800
|
||||
time.Sleep(time.Duration(n) * time.Millisecond)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, `{"time": %d}`, n)
|
||||
log.Printf("Response time: %dms", n)
|
||||
}
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
http.HandleFunc("/", fakeHandler)
|
||||
|
||||
http.ListenAndServe(":8090", nil)
|
||||
}
|
||||
51
cmd/fillqueue/main.go
Normal file
51
cmd/fillqueue/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bitbucket.org/outfrontmedia/vendor-scheduler-service-golang/internal/pkg/onexqueue"
|
||||
"encoding/json"
|
||||
"github.com/streadway/amqp"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func(start time.Time) {
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("fillqueue.main took %s", elapsed)
|
||||
}(time.Now())
|
||||
|
||||
conn, ch := onexqueue.Connect(os.Getenv("ONEX_QUEUE_URL"))
|
||||
defer conn.Close()
|
||||
defer ch.Close()
|
||||
|
||||
queue := onexqueue.CreateQueueIfNecessary(ch, "hello")
|
||||
|
||||
for i := 1; i <= 15000; i++ {
|
||||
message := &onexqueue.Message{
|
||||
Vendor: "vistar",
|
||||
Params: onexqueue.MessageParams{
|
||||
Player: "79102Z02000392500033J2000",
|
||||
VenueId: "AT0000112",
|
||||
DisplayTime: time.Now().Unix(),
|
||||
DisplayArea: onexqueue.DisplayAreaParam{
|
||||
Width: 600,
|
||||
Height: 900,
|
||||
},
|
||||
},
|
||||
}
|
||||
body, _ := json.Marshal(message)
|
||||
err := ch.Publish(
|
||||
"", // exchange
|
||||
queue.Name, // routing key
|
||||
false, // mandatory
|
||||
false, // immediate
|
||||
amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
Body: body,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to publish a message: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
106
cmd/vendorscheduler/main.go
Normal file
106
cmd/vendorscheduler/main.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bitbucket.org/outfrontmedia/vendor-scheduler-service-golang/internal/pkg/onexqueue"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var netClient = &http.Client{}
|
||||
|
||||
func init() {
|
||||
tr := &http.Transport{
|
||||
MaxIdleConns: 0,
|
||||
MaxIdleConnsPerHost: 0,
|
||||
}
|
||||
netClient = &http.Client{Transport: tr}
|
||||
}
|
||||
|
||||
var numberMessagesToProcess = 15000
|
||||
var maxConnections = 500
|
||||
|
||||
func main() {
|
||||
defer func(start time.Time) {
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("vendorscheduler.main took %s", elapsed)
|
||||
}(time.Now())
|
||||
|
||||
conn, ch := onexqueue.Connect(os.Getenv("ONEX_QUEUE_URL"))
|
||||
defer conn.Close()
|
||||
defer ch.Close()
|
||||
|
||||
messages := onexqueue.Consume(ch, "hello")
|
||||
|
||||
apiWork := make(chan onexqueue.Message)
|
||||
apiResults := make(chan string)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
result, ok := <-apiResults
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Print(result)
|
||||
}
|
||||
}()
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < maxConnections; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for {
|
||||
work, ok := <-apiWork
|
||||
if !ok {
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
|
||||
_ = work
|
||||
|
||||
CallVistarAPI(apiResults)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
queueCount := 0
|
||||
for d := range messages {
|
||||
queueCount++
|
||||
msg := onexqueue.Message{}
|
||||
_ = json.Unmarshal(d.Body, &msg)
|
||||
log.Printf("Received message %d", queueCount)
|
||||
|
||||
apiWork <- msg
|
||||
|
||||
if numberMessagesToProcess == queueCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
close(apiWork)
|
||||
|
||||
wg.Wait()
|
||||
close(apiResults)
|
||||
}
|
||||
|
||||
func CallVistarAPI(apiResults chan string) {
|
||||
r, err := netClient.Get(os.Getenv("VISTAR_API_URL"))
|
||||
if err != nil {
|
||||
apiResults <- fmt.Sprintf("ERROR: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
apiResults <- fmt.Sprintf("%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiResults <- fmt.Sprintf("Response received: %v", string(body))
|
||||
}
|
||||
Reference in New Issue
Block a user