M config/config.go => config/config.go +18 -0
@@ 1,9 1,11 @@
package config
import (
+ "fmt"
"log"
"os"
"path/filepath"
+ "strconv"
"git.sr.ht/~sircmpwn/getopt"
"github.com/vaughan0/go-ini"
@@ 92,3 94,19 @@ func GetOrigin(conf ini.File, svc string, external bool) string {
origin, _ = conf.Get(svc, "origin")
return origin
}
+
+const DefaultQueueSize = 512
+
+// Returns the configured integer value for the given section and variable name.
+// If nothing is configured, the default value will be returned.
+// If an invalid integer is configured, this function will panic.
+func GetInt(conf ini.File, section, name string, defValue int) int {
+ value := defValue
+ if s, ok := conf.Get(section, name); ok {
+ var err error
+ if value, err = strconv.Atoi(s); err != nil {
+ panic(fmt.Errorf("[%s]%s: %w", section, name, err))
+ }
+ }
+ return value
+}
M email/worker.go => email/worker.go +2 -8
@@ 9,10 9,10 @@ import (
"log"
"net/http"
"os"
- "strconv"
"strings"
"time"
+ "git.sr.ht/~sircmpwn/core-go/config"
work "git.sr.ht/~sircmpwn/dowork"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/emersion/go-message"
@@ 257,13 257,7 @@ func NewQueue(conf ini.File) *Queue {
}
}
- queueSize := 512
- if s, ok := conf.Get("mail", "egress-queue-size"); ok {
- var err error
- if queueSize, err = strconv.Atoi(s); err != nil {
- panic(fmt.Errorf("[mail]egress-queue-size: %w", err))
- }
- }
+ queueSize := config.GetInt(conf, "mail", "egress-queue-size", config.DefaultQueueSize)
return &Queue{
Queue: work.NewQueue("email", queueSize),
M server/server.go => server/server.go +1 -7
@@ 260,13 260,7 @@ func (server *Server) WithMiddleware(
// Add dowork task queues for this server to manage
func (server *Server) WithQueues(queues ...*work.Queue) *Server {
- queueWorkers := 1
- if n, ok := server.conf.Get(server.service, "queue-workers"); ok {
- var err error
- if queueWorkers, err = strconv.Atoi(n); err != nil {
- panic(fmt.Errorf("[%s]queue-workers: %w", server.service, err))
- }
- }
+ queueWorkers := config.GetInt(server.conf, server.service, "queue-workers", 1)
server.queues = append(server.queues, queues...)
for _, queue := range queues {
// Use a different context per worker to allow "goroutine-local"
M webhooks/legacy.go => webhooks/legacy.go +2 -8
@@ 9,7 9,6 @@ import (
"io/ioutil"
"log"
"net/http"
- "strconv"
"strings"
"time"
@@ 18,6 17,7 @@ import (
"github.com/google/uuid"
"github.com/vaughan0/go-ini"
+ "git.sr.ht/~sircmpwn/core-go/config"
"git.sr.ht/~sircmpwn/core-go/crypto"
"git.sr.ht/~sircmpwn/core-go/database"
)
@@ 36,13 36,7 @@ type LegacySubscription struct {
// Creates a new worker for delivering legacy webhooks. The caller must start
// the worker themselves.
func NewLegacyQueue(conf ini.File) *LegacyQueue {
- queueSize := 512
- if s, ok := conf.Get("webhooks", "queue-size"); ok {
- var err error
- if queueSize, err = strconv.Atoi(s); err != nil {
- panic(fmt.Errorf("[webhooks]queue-size: %w", err))
- }
- }
+ queueSize := config.GetInt(conf, "webhooks", "queue-size", config.DefaultQueueSize)
return &LegacyQueue{
work.NewQueue("webhooks_legacy", queueSize),
}
M webhooks/queue.go => webhooks/queue.go +2 -8
@@ 9,7 9,6 @@ import (
"io/ioutil"
"log"
"net/http"
- "strconv"
"strings"
"time"
@@ 20,6 19,7 @@ import (
"github.com/vaughan0/go-ini"
"git.sr.ht/~sircmpwn/core-go/auth"
+ "git.sr.ht/~sircmpwn/core-go/config"
"git.sr.ht/~sircmpwn/core-go/crypto"
"git.sr.ht/~sircmpwn/core-go/database"
)
@@ 45,13 45,7 @@ type WebhookSubscription struct {
// Creates a new worker for delivering webhooks. The caller must start the
// worker themselves.
func NewQueue(schema graphql.ExecutableSchema, conf ini.File) *WebhookQueue {
- queueSize := 512
- if s, ok := conf.Get("webhooks", "queue-size"); ok {
- var err error
- if queueSize, err = strconv.Atoi(s); err != nil {
- panic(fmt.Errorf("[webhooks]queue-size: %w", err))
- }
- }
+ queueSize := config.GetInt(conf, "webhooks", "queue-size", config.DefaultQueueSize)
return &WebhookQueue{work.NewQueue("webhooks", queueSize), schema}
}