Giter Club home page Giter Club logo

ttlmap's Issues

Unprotected Access in All()

TtlMap/ttlMap.go

Lines 118 to 120 in 1e9219b

func (m *TtlMap) All() map[CustomKeyType]*item {
return m.m
}

The All() method returns a direct reference to the internal map (m.m).

Any caller can then modify the map without acquiring the mutex, leading to potential race conditions.

Returning a deep map copy instead of a direct reference could be a solution.

ttlMap.Put does not update existing data

I noticed a bug in ttlMap.Put(): the value is only set in the item.Value when the item is initially created, not on subsequent calls to Put with the same key.

package main

import (
	"fmt"
	"time"

	"github.com/jftuga/TtlMap"
)

func main() {
	tm := TtlMap.New[string](time.Second*4, 3, time.Second, true)
	defer tm.Close()

	tm.Put("example", "original")
	tm.Put("example", "revised")

	fmt.Printf("Example value is %s\n", tm.Get("example"))
	// Output:
	// Example value is original
}

Setting the value just needs to be moved out of the if !ok scope.

Orphaned Goroutine

TtlMap/ttlMap.go

Lines 49 to 70 in 1e9219b

func New(maxTTL int, ln int, pruneInterval int, refreshLastAccessOnGet bool) (m *TtlMap) {
// if pruneInterval > maxTTL {
// print("WARNING: TtlMap: pruneInterval > maxTTL\n")
// }
m = &TtlMap{m: make(map[CustomKeyType]*item, ln)}
m.refresh = refreshLastAccessOnGet
go func() {
for now := range time.Tick(time.Second * time.Duration(pruneInterval)) {
currentTime := now.Unix()
m.l.Lock()
for k, v := range m.m {
// print("TICK:", currentTime, " ", v.lastAccess, " ", (currentTime - v.lastAccess), " ", maxTTL, " ", k, "\n")
if currentTime-v.lastAccess >= int64(maxTTL) {
delete(m.m, k)
// print("deleting: ", k, "\n")
}
}
// print("\n")
m.l.Unlock()
}
}()
return

The goroutine started in the New function will run indefinitely. This could lead to resource leaks.

Suggestion: Add a Close or Stop method to signal the goroutine to stop.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.