no need for FreeList, we have sync.Pool at home

This commit is contained in:
2026-02-20 15:01:45 -07:00
parent d2e6396ca7
commit d8eeeb7140
3 changed files with 6 additions and 72 deletions
+3 -3
View File
@@ -12,9 +12,9 @@ package email
import ( import (
"fmt" "fmt"
"sync"
"git.erbosoft.com/amy/amsterdam/config" "git.erbosoft.com/amy/amsterdam/config"
"git.erbosoft.com/amy/amsterdam/util"
"github.com/CloudyKit/jet/v6" "github.com/CloudyKit/jet/v6"
) )
@@ -51,7 +51,7 @@ type amMessage struct {
} }
// freeMessages is a free list for amMessage structures. // freeMessages is a free list for amMessage structures.
var freeMessages util.FreeList[amMessage] var freeMessages sync.Pool
// formatAddress outputs an E-mail address with optional name associated with it. // formatAddress outputs an E-mail address with optional name associated with it.
func formatAddress(addr string, name string) string { func formatAddress(addr string, name string) string {
@@ -126,7 +126,7 @@ func (m *amMessage) Send() {
* The new Message. * The new Message.
*/ */
func AmNewEmailMessage(sender int32, ip string) Message { func AmNewEmailMessage(sender int32, ip string) Message {
rc := freeMessages.Get() rc := freeMessages.Get().(*amMessage)
if rc == nil { if rc == nil {
rc = &amMessage{to: make([]string, 0), cc: make([]string, 0), bcc: make([]string, 0), rc = &amMessage{to: make([]string, 0), cc: make([]string, 0), bcc: make([]string, 0),
headers: make(map[string]string), vars: make(jet.VarMap)} headers: make(map[string]string), vars: make(jet.VarMap)}
+3 -2
View File
@@ -18,6 +18,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"sync"
"time" "time"
"git.erbosoft.com/amy/amsterdam/config" "git.erbosoft.com/amy/amsterdam/config"
@@ -510,7 +511,7 @@ var defoptions *AmSessionOptions = &AmSessionOptions{
} }
// freeContext is a free list for amContext structures. // freeContext is a free list for amContext structures.
var freeContext util.FreeList[amContext] var freeContext sync.Pool
// amContextRecycleBin is the channel we put contexts on to be recycled. // amContextRecycleBin is the channel we put contexts on to be recycled.
var amContextRecycleBin chan *amContext var amContextRecycleBin chan *amContext
@@ -523,7 +524,7 @@ var amContextRecycleBin chan *amContext
* Standard Go error status. * Standard Go error status.
*/ */
func newContext(ctxt echo.Context) (*amContext, error) { func newContext(ctxt echo.Context) (*amContext, error) {
rc := freeContext.Get() rc := freeContext.Get().(*amContext)
if rc == nil { if rc == nil {
rc = &amContext{ rc = &amContext{
rendervars: make(jet.VarMap), rendervars: make(jet.VarMap),
-67
View File
@@ -1,67 +0,0 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package util contains utility definitions.
package util
import "sync"
// freeListElem is an element of the free list.
type freeListElem[T any] struct {
next *freeListElem[T]
prev *freeListElem[T]
data *T
}
// FreeList defines a free list.
type FreeList[T any] struct {
New func() *T
mutex sync.Mutex
listptr *freeListElem[T]
}
// Put adds a value to the free list.
func (l *FreeList[T]) Put(value *T) {
l.mutex.Lock()
defer l.mutex.Unlock()
ne := freeListElem[T]{data: value}
if l.listptr == nil {
ne.next = &ne
ne.prev = &ne
l.listptr = &ne
} else {
ne.next = l.listptr
ne.prev = l.listptr.prev
ne.next.prev = &ne
ne.prev.next = &ne
}
}
// Get removes a value from the free list. If there are no values and New is specified, is calls that.
func (l *FreeList[T]) Get() *T {
l.mutex.Lock()
defer l.mutex.Unlock()
var rc *T = nil
if l.listptr == nil {
if l.New != nil {
rc = l.New()
}
} else {
elt := l.listptr
rc = elt.data
l.listptr = elt.next
if l.listptr == elt {
l.listptr = nil
} else {
elt.prev.next = elt.next
elt.next.prev = elt.prev
}
}
return rc
}