Skip to content

Commit 89d6e12

Browse files
authored
chore(sync): adding new sync package (#97)
* adding new sync package * more importas * deps
1 parent 979a5eb commit 89d6e12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2260
-973
lines changed

.golangci.yaml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- testifylint
1717
- thelper
1818
- tparallel
19+
- importas
1920
settings:
2021
goconst:
2122
ignore-calls: true
@@ -80,9 +81,8 @@ linters:
8081
exclude:
8182
- ""
8283
sloglint:
83-
attr-only: true
84+
# attr-only: true
8485
static-msg: true
85-
no-raw-keys: true
8686
key-naming-case: snake
8787
forbidden-keys:
8888
- time
@@ -93,6 +93,34 @@ linters:
9393
args-on-sep-lines: true
9494
testifylint:
9595
enable-all: true
96+
importas:
97+
alias:
98+
- pkg: github.com/jacobbrewer1/web/cache
99+
alias: pkgcache
100+
- pkg: github.com/jacobbrewer1/web/slices
101+
alias: pkgslices
102+
- pkg: github.com/jacobbrewer1/web/sync
103+
alias: pkgsync
104+
- pkg: k8s.io/client-go/listers/core/v1
105+
alias: listersv1
106+
- pkg: k8s.io/api/core/v1
107+
alias: corev1
108+
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
109+
alias: metav1
110+
- pkg: k8s.io/api/apps/v1
111+
alias: appsv1
112+
- pkg: k8s.io/apimachinery/pkg/labels
113+
alias: k8slabels
114+
- pkg: k8s.io/apimachinery/pkg/api/errors
115+
alias: k8serrors
116+
- pkg: k8s.io/api/events/v1
117+
alias: eventsv1
118+
- pkg: k8s.io/apimachinery/pkg/types
119+
alias: k8stypes
120+
- pkg: k8s.io/client-go/tools/cache
121+
alias: k8scache
122+
- pkg: github.com/oapi-codegen/runtime/types
123+
alias: oapi
96124
exclusions:
97125
generated: lax
98126
presets:

app.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"k8s.io/client-go/informers"
2020
"k8s.io/client-go/kubernetes"
2121
listersv1 "k8s.io/client-go/listers/core/v1"
22-
kubeCache "k8s.io/client-go/tools/cache"
22+
k8scache "k8s.io/client-go/tools/cache"
2323
"k8s.io/client-go/tools/leaderelection"
2424

2525
"github.com/jacobbrewer1/goredis"
@@ -28,8 +28,8 @@ import (
2828
"github.com/jacobbrewer1/vaulty/vsql"
2929
"github.com/jacobbrewer1/web/cache"
3030
"github.com/jacobbrewer1/web/logging"
31+
pkgsync "github.com/jacobbrewer1/web/sync"
3132
"github.com/jacobbrewer1/web/version"
32-
"github.com/jacobbrewer1/workerpool"
3333
)
3434

3535
const (
@@ -112,19 +112,19 @@ type (
112112
kubernetesInformerFactory informers.SharedInformerFactory
113113

114114
// podInformer is an informer for Kubernetes Pod objects.
115-
podInformer kubeCache.SharedIndexInformer
115+
podInformer k8scache.SharedIndexInformer
116116

117117
// podLister is a lister for Kubernetes Pod objects.
118118
podLister listersv1.PodLister
119119

120120
// secretInformer is an informer for Kubernetes Secret objects.
121-
secretInformer kubeCache.SharedIndexInformer
121+
secretInformer k8scache.SharedIndexInformer
122122

123123
// secretLister is a lister for Kubernetes Secret objects.
124124
secretLister listersv1.SecretLister
125125

126126
// configMapInformer is an informer for Kubernetes ConfigMap objects.
127-
configMapInformer kubeCache.SharedIndexInformer
127+
configMapInformer k8scache.SharedIndexInformer
128128

129129
// configMapLister is a lister for Kubernetes ConfigMap objects.
130130
configMapLister listersv1.ConfigMapLister
@@ -138,8 +138,8 @@ type (
138138
// redisPool is the redis pool for the application.
139139
redisPool goredis.Pool
140140

141-
// workerPool is the worker pool that can execute tasks concurrently.
142-
workerPool workerpool.Pool
141+
// workerPools is a map of worker pools for the application, keyed by pool name.
142+
workerPools sync.Map
143143

144144
// indefiniteAsyncTasks is the list of indefinite async tasks for the application.
145145
indefiniteAsyncTasks sync.Map
@@ -387,10 +387,13 @@ func (a *App) Shutdown() {
387387
a.serviceEndpointHashBucket.Shutdown()
388388
}
389389

390-
// Stop the worker pool if it exists.
391-
if a.workerPool != nil {
392-
a.workerPool.Stop()
393-
}
390+
a.workerPools.Range(func(k any, v any) bool {
391+
if wp, ok := v.(pkgsync.WorkerPool); ok {
392+
a.l.Info("stopping worker pool", "name", k)
393+
wp.Close()
394+
}
395+
return true
396+
})
394397

395398
// Close the NATS client if it exists.
396399
if a.natsClient != nil {
@@ -467,10 +470,6 @@ func (a *App) LeaderChange() <-chan struct{} {
467470

468471
// StartServer starts a new server with the given name and http.Server.
469472
func (a *App) StartServer(name string, srv *http.Server) error {
470-
if _, found := a.servers.Load(name); found {
471-
return fmt.Errorf("server %s already exists", name)
472-
}
473-
474473
// If the server handler is gorilla mux, check the not found handler and method not allowed handler
475474
if muxRouter, ok := srv.Handler.(*mux.Router); ok {
476475
if muxRouter.NotFoundHandler == nil {
@@ -483,7 +482,9 @@ func (a *App) StartServer(name string, srv *http.Server) error {
483482
}
484483
}
485484

486-
a.servers.Store(name, srv)
485+
if _, loaded := a.servers.LoadOrStore(name, srv); loaded {
486+
return fmt.Errorf("server %s already exists", name)
487+
}
487488
a.startServer(name, srv)
488489
return nil
489490
}
@@ -516,13 +517,21 @@ func (a *App) RedisPool() goredis.Pool {
516517
return a.redisPool
517518
}
518519

519-
// WorkerPool returns the worker pool for the application.
520-
func (a *App) WorkerPool() workerpool.Pool {
521-
if a.workerPool == nil {
522-
a.l.Error("worker pool has not been registered")
523-
panic("worker pool has not been registered")
520+
// WorkerPool returns the application worker pool, if one exists.
521+
func (a *App) WorkerPool(name string) pkgsync.WorkerPool {
522+
v, ok := a.workerPools.Load(name)
523+
if !ok {
524+
a.l.Error("worker pool has not been registered", "name", name)
525+
panic(fmt.Sprintf("worker pool '%s' has not been registered", name))
524526
}
525-
return a.workerPool
527+
528+
wp, ok := v.(pkgsync.WorkerPool)
529+
if !ok {
530+
a.l.Error("worker pool is not of type pkgsync.WorkerPool", "name", name)
531+
panic(fmt.Sprintf("worker pool '%s' is not of type pkgsync.WorkerPool", name))
532+
}
533+
534+
return wp
526535
}
527536

528537
// NatsClient returns the NATS client for the application.
@@ -609,7 +618,7 @@ func (a *App) PodLister() listersv1.PodLister {
609618
}
610619

611620
// PodInformer returns the pod informer for the application.
612-
func (a *App) PodInformer() kubeCache.SharedIndexInformer {
621+
func (a *App) PodInformer() k8scache.SharedIndexInformer {
613622
if a.podInformer == nil {
614623
a.l.Error("pod informer has not been registered")
615624
panic("pod informer has not been registered")
@@ -636,7 +645,7 @@ func (a *App) SecretLister() listersv1.SecretLister {
636645
}
637646

638647
// SecretInformer returns the secret informer for the application.
639-
func (a *App) SecretInformer() kubeCache.SharedIndexInformer {
648+
func (a *App) SecretInformer() k8scache.SharedIndexInformer {
640649
if a.secretInformer == nil {
641650
a.l.Error("secret informer has not been registered")
642651
panic("secret informer has not been registered")
@@ -654,7 +663,7 @@ func (a *App) ConfigMapLister() listersv1.ConfigMapLister {
654663
}
655664

656665
// ConfigMapInformer returns the config map informer for the application.
657-
func (a *App) ConfigMapInformer() kubeCache.SharedIndexInformer {
666+
func (a *App) ConfigMapInformer() k8scache.SharedIndexInformer {
658667
if a.configMapInformer == nil {
659668
a.l.Error("config map informer has not been registered")
660669
panic("config map informer has not been registered")

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ require (
1313
github.com/jacobbrewer1/goredis v0.1.7
1414
github.com/jacobbrewer1/uhttp v0.0.12
1515
github.com/jacobbrewer1/vaulty v0.1.15-0.20250422083501-a48cb7ba777e
16-
github.com/jacobbrewer1/workerpool v0.0.4
1716
github.com/nats-io/nats.go v1.44.0
1817
github.com/prometheus/client_golang v1.23.0
1918
github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b
2019
github.com/spf13/viper v1.20.1
2120
github.com/stretchr/testify v1.10.0
21+
go.uber.org/atomic v1.9.0
22+
go.uber.org/multierr v1.11.0
2223
k8s.io/api v0.33.4
2324
k8s.io/apimachinery v0.33.4
2425
k8s.io/client-go v0.33.4
@@ -83,7 +84,6 @@ require (
8384
github.com/stretchr/objx v0.5.2 // indirect
8485
github.com/subosito/gotenv v1.6.0 // indirect
8586
github.com/x448/float16 v0.8.4 // indirect
86-
go.uber.org/multierr v1.11.0 // indirect
8787
golang.org/x/crypto v0.38.0 // indirect
8888
golang.org/x/net v0.40.0 // indirect
8989
golang.org/x/oauth2 v0.30.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ github.com/jacobbrewer1/uhttp v0.0.12 h1:815Gz4rH7SEwkWPw73XtPUTBAUeyiw19EoybHtX
102102
github.com/jacobbrewer1/uhttp v0.0.12/go.mod h1:VcQ3tWUZv+L1l+xNLkNKhIarKL0lqg8QQFU1zLYgSaQ=
103103
github.com/jacobbrewer1/vaulty v0.1.15-0.20250422083501-a48cb7ba777e h1:AeMRepuFFe7fKMK29/gdkQs02ZzHEJdhlRptfFP9hq0=
104104
github.com/jacobbrewer1/vaulty v0.1.15-0.20250422083501-a48cb7ba777e/go.mod h1:hncWoF2yQ+ZJKfoIy7JaGkju+k0hlU6GVBNq2y38e1k=
105-
github.com/jacobbrewer1/workerpool v0.0.4 h1:jETZCLCXZzzMCRtL0kPae9NTOymbztd4bg4Ba7i1UU8=
106-
github.com/jacobbrewer1/workerpool v0.0.4/go.mod h1:m4t+CpY0s+Sv95Vq5OmtDYD+eXgQ0slVwJKnN3RkOkI=
107105
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
108106
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
109107
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -212,6 +210,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
212210
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
213211
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
214212
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
213+
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
214+
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
215215
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
216216
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
217217
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

options.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/jacobbrewer1/web/health"
3030
"github.com/jacobbrewer1/web/k8s"
3131
"github.com/jacobbrewer1/web/logging"
32-
"github.com/jacobbrewer1/workerpool"
32+
pkgsync "github.com/jacobbrewer1/web/sync"
3333
)
3434

3535
const (
@@ -303,14 +303,19 @@ func WithMetricsEnabled(metricsEnabled bool) StartOption {
303303
}
304304
}
305305

306-
// WithWorkerPool is a StartOption that sets up the worker pool for the application.
307-
func WithWorkerPool() StartOption {
306+
// WithWorkerPool configures the app to instantiate a worker pool for concurrent processing of tasks.
307+
func WithWorkerPool(name string, size, backlog uint) StartOption {
308308
return func(a *App) error {
309-
wp := workerpool.New(
310-
workerpool.WithDelayedStart(),
309+
a.l.Info("creating worker pool",
310+
"size", size,
311+
"name", name,
311312
)
312-
313-
a.workerPool = wp
313+
if _, loaded := a.workerPools.LoadOrStore(
314+
name,
315+
pkgsync.NewWorkerPool(a.baseCtx, name, size, backlog),
316+
); loaded {
317+
return fmt.Errorf("worker pool of name %q already exists", name)
318+
}
314319
return nil
315320
}
316321
}

0 commit comments

Comments
 (0)