f-21: code: ajout d'un system de worker
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
e1f7676093
commit
8637c33bd8
8 changed files with 87 additions and 14 deletions
|
|
@ -6,10 +6,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
agentapi "git.g3e.fr/syonad/two/internal/api/agent"
|
agentapi "git.g3e.fr/syonad/two/internal/api/agent"
|
||||||
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
|
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
promserver "git.g3e.fr/syonad/two/pkg/prometheus"
|
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
|
||||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
promserver "git.g3e.fr/syonad/two/pkg/prometheus"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/worker"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,13 +26,16 @@ func main() {
|
||||||
db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true)
|
db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
|
q := worker.New(cfg.Worker.BufferSize)
|
||||||
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
q.Start(cfg.Worker.Count)
|
||||||
|
|
||||||
registry := prometheus.NewRegistry()
|
registry := prometheus.NewRegistry()
|
||||||
registry.MustRegister(agentmetrics.NewAgentCollector(db))
|
registry.MustRegister(agentmetrics.NewAgentCollector(db))
|
||||||
|
|
||||||
go agentapi.Start(apiAddr)
|
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
|
||||||
|
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
||||||
|
|
||||||
|
go agentapi.New(q).Start(apiAddr)
|
||||||
go promserver.Start(promAddr, registry)
|
go promserver.Start(promAddr, registry)
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,24 @@ package agentapi
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/pkg/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(address string) {
|
type Server struct {
|
||||||
|
queue *worker.Queue
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(queue *worker.Queue) *Server {
|
||||||
|
return &Server{queue: queue}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Start(address string) {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/vpcs", VpcsHandler)
|
mux.HandleFunc("/vpcs", s.VpcsHandler)
|
||||||
mux.HandleFunc("/vpcs/", VpcByNameHandler)
|
mux.HandleFunc("/vpcs/", s.VpcByNameHandler)
|
||||||
mux.HandleFunc("/subnets", SubnetsHandler)
|
mux.HandleFunc("/subnets", s.SubnetsHandler)
|
||||||
mux.HandleFunc("/subnets/", SubnetByNameHandler)
|
mux.HandleFunc("/subnets/", s.SubnetByNameHandler)
|
||||||
log.Printf("API server listening on %s", address)
|
log.Printf("API server listening on %s", address)
|
||||||
log.Fatal(http.ListenAndServe(address, mux))
|
log.Fatal(http.ListenAndServe(address, mux))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
|
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
|
||||||
if name == "" {
|
if name == "" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
|
@ -18,8 +18,13 @@ func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
|
s.queue.Submit(func() {
|
||||||
|
deleteSubnet(name)
|
||||||
|
})
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteSubnet(name string) {}
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,20 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode([]interface{}{})
|
json.NewEncoder(w).Encode([]interface{}{})
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
|
s.queue.Submit(func() {
|
||||||
|
createSubnet()
|
||||||
|
})
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createSubnet() {}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
|
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
|
||||||
if name == "" {
|
if name == "" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
|
@ -18,8 +18,13 @@ func VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
|
s.queue.Submit(func() {
|
||||||
|
deleteVpc(name)
|
||||||
|
})
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteVpc(name string) {}
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,20 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode([]interface{}{})
|
json.NewEncoder(w).Encode([]interface{}{})
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
|
s.queue.Submit(func() {
|
||||||
|
createVpc()
|
||||||
|
})
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createVpc() {}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ type Config struct {
|
||||||
Address string `mapstructure:"address"`
|
Address string `mapstructure:"address"`
|
||||||
Port int `mapstructure:"port"`
|
Port int `mapstructure:"port"`
|
||||||
} `mapstructure:"prometheus"`
|
} `mapstructure:"prometheus"`
|
||||||
|
Worker struct {
|
||||||
|
Count int `mapstructure:"count"`
|
||||||
|
BufferSize int `mapstructure:"buffer_size"`
|
||||||
|
} `mapstructure:"worker"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(path string) (*Config, error) {
|
func LoadConfig(path string) (*Config, error) {
|
||||||
|
|
@ -28,6 +32,8 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
v.SetDefault("api.port", 8080)
|
v.SetDefault("api.port", 8080)
|
||||||
v.SetDefault("prometheus.address", "")
|
v.SetDefault("prometheus.address", "")
|
||||||
v.SetDefault("prometheus.port", 9090)
|
v.SetDefault("prometheus.port", 9090)
|
||||||
|
v.SetDefault("worker.count", 4)
|
||||||
|
v.SetDefault("worker.buffer_size", 100)
|
||||||
|
|
||||||
v.ReadInConfig()
|
v.ReadInConfig()
|
||||||
|
|
||||||
|
|
|
||||||
33
pkg/worker/queue.go
Normal file
33
pkg/worker/queue.go
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package worker
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
// Task is a function to be executed asynchronously by a worker.
|
||||||
|
type Task func()
|
||||||
|
|
||||||
|
// Queue is a FIFO channel-backed task queue consumed by worker goroutines.
|
||||||
|
type Queue struct {
|
||||||
|
tasks chan Task
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a Queue with the given channel buffer size.
|
||||||
|
func New(bufferSize int) *Queue {
|
||||||
|
return &Queue{tasks: make(chan Task, bufferSize)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submit enqueues a task. Blocks if the queue is full.
|
||||||
|
func (q *Queue) Submit(t Task) {
|
||||||
|
q.tasks <- t
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start launches n worker goroutines that consume and execute tasks.
|
||||||
|
func (q *Queue) Start(n int) {
|
||||||
|
log.Printf("worker: starting %d workers", n)
|
||||||
|
for i := range n {
|
||||||
|
go func(id int) {
|
||||||
|
for task := range q.tasks {
|
||||||
|
task()
|
||||||
|
}
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue