packs2
DEV 3 years ago
parent 3fca3ef79d
commit deb98e9d4e

@ -36,7 +36,7 @@ import (
type (
BufferPool struct {
syncs.PoolStats
syncs.PoolStatsInfo
inner sync.Pool

@ -32,7 +32,7 @@ import (
type (
Pool[T any] struct {
PoolStats
PoolStatsInfo
inner sync.Pool

@ -4,7 +4,7 @@
//
// File name pool_stats.go
// Created by DEV
// Modified 17/01/2024
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -23,36 +23,17 @@
package syncs
import "sync/atomic"
type (
PoolStats struct {
created uint64
acquired uint64
released uint64
}
)
func (p *PoolStats) Created() uint64 {
return atomic.LoadUint64(&p.created)
}
func (p *PoolStats) Acquired() uint64 {
return atomic.LoadUint64(&p.acquired)
}
type (
func (p *PoolStats) Released() uint64 {
return atomic.LoadUint64(&p.released)
}
PoolStats interface {
func (p *PoolStats) AddCreated() {
atomic.AddUint64(&p.created, 1)
Created () uint64
Acquired() uint64
Released() uint64
}
)
func (p *PoolStats) AddReleased() {
atomic.AddUint64(&p.released, 1)
}
func (p *PoolStats) AddAcquired() {
atomic.AddUint64(&p.acquired, 1)
}

@ -0,0 +1,65 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name pool_stats.go
// Created by DEV
// Modified 17/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package syncs
import "sync/atomic"
type (
PoolStatsInfo struct {
created uint64
acquired uint64
released uint64
}
)
func NewPoolStatsInfo () *PoolStatsInfo {
return &PoolStatsInfo{} }
func (p *PoolStatsInfo) Created() uint64 {
return atomic.LoadUint64(&p.created)
}
func (p *PoolStatsInfo) Acquired() uint64 {
return atomic.LoadUint64(&p.acquired)
}
func (p *PoolStatsInfo) Released() uint64 {
return atomic.LoadUint64(&p.released)
}
func (p *PoolStatsInfo) AddCreated() {
atomic.AddUint64(&p.created, 1)
}
func (p *PoolStatsInfo) AddReleased() {
atomic.AddUint64(&p.released, 1)
}
func (p *PoolStatsInfo) AddAcquired() {
atomic.AddUint64(&p.acquired, 1)
}

@ -0,0 +1,26 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name gogo.go
// Created by DEV
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package tasks

@ -0,0 +1,38 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name worker.go
// Created by DEV
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package tasks
import "time"
type (
worker struct {
lastTime time.Time
jobCh chan func()
}
)

@ -0,0 +1,151 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name worker_pool.go
// Created by DEV
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package tasks
import (
"activething.com/go/gogo/core/opts"
"activething.com/go/gogo/core/syncs"
"runtime"
"sync"
"sync/atomic"
"time"
)
const (
minWorkers = 10
maxWorkers = 1000
minIdle = time.Millisecond * 100
maxIdle = time.Second * 10
)
type (
WorkerPool struct {
stats *syncs.PoolStatsInfo
pool sync.Pool
mux sync.Mutex
cap int32
max int32
count int32
state uint32
maxIdle int64
workers []*worker
doneCh chan struct{}
}
)
func NewWorkerPool (options ...opts.OptionFnc[WorkerPool]) *WorkerPool {
c := int32(1)
if runtime.GOMAXPROCS(0) == 1 {
c = 0
}
wp := opts.Apply[WorkerPool](&WorkerPool{
stats : syncs.NewPoolStatsInfo(),
maxIdle: int64(maxIdle),
cap : c,
max : maxWorkers,
state : uint32(WorkerPoolStateCreated),
}, options ...)
wp.pool.New = wp.newWorker
return wp
}
func (p *WorkerPool) newWorker () any {
p.stats.AddCreated()
return &worker {
jobCh: make(chan func(), atomic.LoadInt32(&p.cap)) }}
func (p *WorkerPool) Stats () syncs.PoolStats {
return p.stats }
func (p *WorkerPool) State () WorkerPoolState {
return WorkerPoolState(atomic.LoadUint32(&p.state))
}
func (p *WorkerPool) Count () int32 {
return atomic.LoadInt32(&p.count) }
func (p *WorkerPool) MaxWorkers () int32 {
return atomic.LoadInt32(&p.max) }
func (p *WorkerPool) SetMaxWorkers (max int32) {
if max <= 0 {
max = maxWorkers
}
atomic.StoreInt32(&p.max,max)
}
func (p *WorkerPool) WorkerCap () int32 {
return atomic.LoadInt32(&p.cap) }
func (p *WorkerPool) SetWorkerCap (cap int32) {
if cap <= 0 {
cap = 0
}
atomic.StoreInt32(&p.cap,cap)
}
func (p *WorkerPool) MaxWorkerIdle () time.Duration {
return time.Duration(atomic.LoadInt64(&p.maxIdle)) }
func (p *WorkerPool) SetMaxWorkerIdle (idle time.Duration) {
if idle < minIdle {
idle = minIdle
}
atomic.StoreInt64(&p.maxIdle,int64(idle))
}
func (p *WorkerPool) Done () <- chan struct{} {
return p.doneCh }
func (p *WorkerPool) Start () error {
p.mux.Lock()
st := WorkerPoolState(atomic.LoadUint32(&p.state))
if st == WorkerPoolStateRunning {
p.mux.Unlock()
return
}
}

@ -0,0 +1,40 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name worker_pool_state.go
// Created by DEV
// Modified 20/02/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package tasks
const (
WorkerPoolStateCreated WorkerPoolState = iota
WorkerPoolStateRunning
WorkerPoolStateStopped
)
type (
WorkerPoolState uint32
)

@ -29,11 +29,17 @@ var (
G = struct {
ErrReadOnlyProperties errs.ErrCode
ErrFixedProperties errs.ErrCode
ErrInvalidScope errs.ErrCode
ErrInvalidScopeKey errs.ErrCode
ErrReadOnlyScope errs.ErrCode
}{
ErrReadOnlyProperties : errs.ErrCode("core:values.read-only-properties"),
ErrFixedProperties : errs.ErrCode("core:values.fixed-properties"),
ErrInvalidScope: errs.ErrCode("core:values.invalid-scope"),
ErrInvalidScopeKey: errs.ErrCode("core:values.invalid-scope-key"),
ErrReadOnlyScope: errs.ErrCode("core:values.read-only-scope"),

@ -34,7 +34,7 @@ type (
}
PropertiesWalkFnc func(key string, value any) bool
PropertiesWalkFnc func(key string, value any, level int) bool
)
@ -63,10 +63,10 @@ func (p *Properties) Get (name string) (any,bool) {
func (p *Properties) Set (nme string, val interface{}) error {
if p.atts.IsReadOnly() {
return G.Errors.PropertiesReadOnly
return G.ErrReadOnlyProperties
}
if _,hs := p.props[nme]; !hs && p.IsFixed() {
return Errors.PropertiesFixed
if _,hs := p.props[nme]; !hs && p.atts.IsFixed() {
return G.ErrFixedProperties
}
p.props[nme]=val
return nil
@ -75,19 +75,19 @@ func (p *Properties) Set (nme string, val interface{}) error {
func (p *Properties) Del (nme string) error {
if p.ref != nil {
if _,hs := p.props[nme]; hs && p.IsReadOnly() {
return Errors.PropertiesReadOnly
if p.parent != nil {
if _,hs := p.props[nme]; hs && p.atts.IsReadOnly() {
return G.ErrReadOnlyProperties
}
delete(p.props,nme)
return nil
}
if _,hs := p.props[nme]; hs {
if p.IsFixed() {
return Errors.PropertiesFixed
if p.atts.IsFixed() {
return G.ErrFixedProperties
}
if p.IsReadOnly() {
return Errors.PropertiesReadOnly
if p.atts.IsReadOnly() {
return G.ErrReadOnlyProperties
}
}
delete(p.props,nme)
@ -95,24 +95,25 @@ func (p *Properties) Del (nme string) error {
}
// Properties.Map
// Desc:
//
// Prms: mfn,
//
func (p *Properties) Map (mfn PropertiesMapFnc) {
for ky,vl := range p.props {
if !mfn(ky,vl,false) {
return
func (p *Properties) Value (key any) any {
if v,h := p.Get(key.(string)); h {
return v
}
return nil
}
if p.ref != nil {
for ky,vl := range p.ref.props {
if _,hs := p.props[ky]; !hs {
if !mfn(ky, vl, true) {
func (p *Properties) Walk (walker PropertiesWalkFnc, walkTree bool) {
p.walk(walker,walkTree,0) }
func (p *Properties) walk (walker PropertiesWalkFnc, walkTree bool, level int) {
for ky,vl := range p.props {
if !walker(ky,vl,level) {
return
}
}
}
if p.parent != nil && walkTree {
p.parent.walk(walker, true, level+1)
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.