GithubHelp home page GithubHelp logo

Comments (4)

ryanchapman avatar ryanchapman commented on August 16, 2024

Could you please add a full stack trace of the panic you’re running into?

from go-any-proxy.

keminar avatar keminar commented on August 16, 2024
package main

import (
	"fmt"
	"time"

	"gitea.com/lunny/log"
)

type cacheEntry struct {
	hostname string
	expires  time.Time
}
type reverseLookupCache struct {
	hostnames map[string]*cacheEntry
	keys      []string
	next      int
}

func NewReverseLookupCache() *reverseLookupCache {
	return &reverseLookupCache{
		hostnames: make(map[string]*cacheEntry),
		keys:      make([]string, 65536),
	}
}
func (c *reverseLookupCache) lookup(ipv4 string) string {
	hit := c.hostnames[ipv4]
	if hit != nil {
		if hit.expires.After(time.Now()) {
			log.Debugf("lookup(): CACHE_HIT")
			return hit.hostname
		} else {
			log.Debugf("lookup(): CACHE_EXPIRED")
			delete(c.hostnames, ipv4)
		}
	} else {
		log.Debugf("lookup(): CACHE_MISS")
	}
	return ""
}
func (c *reverseLookupCache) store(ipv4, hostname string) {
	delete(c.hostnames, c.keys[c.next])
	c.keys[c.next] = ipv4
	c.next = (c.next + 1) & 65535
	c.hostnames[ipv4] = &cacheEntry{hostname: hostname, expires: time.Now().Add(time.Hour)}
}

var gReverseLookupCache *reverseLookupCache

func main() {
	gReverseLookupCache = NewReverseLookupCache()

	for i := 0; i < 10; i++ {
		go func() {
			ipv4 := fmt.Sprintf("test-%d", i)
			hostname := gReverseLookupCache.lookup(ipv4)
			if hostname != "" {
				ipv4 = hostname
			} else {
				val := fmt.Sprintf("val-%d", i)
				gReverseLookupCache.store(ipv4, val)
			}
		}()
	}
}

from go-any-proxy.

keminar avatar keminar commented on August 16, 2024
go: finding gitea.com/lunny/log latest
fatal error: concurrent map read and map write

goroutine 12 [running]:
runtime.throw(0x4cf674, 0x21)
>---/data/opt/go/src/runtime/panic.go:617 +0x72 fp=0xc000031eb0 sp=0xc000031e80 pc=0x428f92
runtime.mapaccess1_faststr(0x4ae680, 0xc0000721b0, 0xc000182090, 0x7, 0x7)
>---/data/opt/go/src/runtime/map_faststr.go:21 +0x469 fp=0xc000031f20 sp=0xc000031eb0 pc=0x40ffe9
main.(*reverseLookupCache).lookup(0xc0000721e0, 0xc000182090, 0x7, 0x1, 0x1)
>---/data/app/golang/src/test.go:27 +0x52 fp=0xc000031f70 sp=0xc000031f20 pc=0x494f22
main.main.func1(0xc000014110)
>---/data/app/golang/src/test.go:56 +0xb3 fp=0xc000031fd8 sp=0xc000031f70 pc=0x4954e3
runtime.goexit()
>---/data/opt/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc000031fe0 sp=0xc000031fd8 pc=0x452181
created by main.main
>---/data/app/golang/src/test.go:54 +0xe8

goroutine 1 [runnable]:
runtime.gopark(0x0, 0x0, 0xc000021008, 0x1)
>---/data/opt/go/src/runtime/proc.go:284 +0x13d
runtime.main()
>---/data/opt/go/src/runtime/proc.go:219 +0x287
runtime.goexit()
>---/data/opt/go/src/runtime/asm_amd64.s:1337 +0x1

goroutine 8 [runnable]:
fmt.Sprintf(0x4cce92, 0x14, 0x0, 0x0, 0x0, 0xc0001b4000, 0x0)
>---/data/opt/go/src/fmt/print.go:215 +0x92
gitea.com/lunny/log.Debugf(0x4cce92, 0x14, 0x0, 0x0, 0x0)
>---/data/app/golang/pkg/mod/gitea.com/lunny/[email protected]/logext.go:522 +0x57
main.(*reverseLookupCache).lookup(0xc0000721e0, 0xc000192010, 0x7, 0x1, 0x1)
>---/data/app/golang/src/test.go:37 +0x196
main.main.func1(0xc000014110)
>---/data/app/golang/src/test.go:56 +0xb3
created by main.main
>---/data/app/golang/src/test.go:54 +0xe8

from go-any-proxy.

ryanchapman avatar ryanchapman commented on August 16, 2024

Thanks for reporting that. You are indeed correct, that struct needs to be synchronized. If you have time, could you send in a PR? If not, I'll take care of it this week.

from go-any-proxy.

Related Issues (20)

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.