Firstly, sorry all for bad English.
I try to develop a WAF(Web Application Firewall) service in Golang. Everything is in the map[string]*Struct{} in memory. When request has come, I set request header's host to map in handler function.
host,err := GetHost(r.Host)
func GetHost(host string) (*Host,error){
split, _, err := net.SplitHostPort(host)
if err == nil {
host = split
}
if data, val := hosts[host]; val {
return data, nil
}
return nil,errors.New("host not found!)
}
//hosts is a map for all host, key is host and value is host struct.
The problem is, map is messing when service has got a lot of request. For example;
host is example.com but hosts["example.com"] gives an another value which is irrelevant.
type Server struct {
mu sync.RWMutex
Host *models.Host
}
func (c *Server) handler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mutex.Lock()
defer mutex.Unlock()
var err error
c.Host, err = GetHost(r.Host)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(r.Host + " not found!!"))
return
}
//it's going on..Edited part
So, I try to use mutex and wg for solving this problem but it didn't work. Open for any suggestion.