go - Golang close net listener -


i having trouble closing listener can reopen on same port. writing proxy server hot-configurable - i.e (redirects/blocks etc) can adjusted on fly. proxy runs in go routine. problem having when reconfigure listener , proxy listener still using port previous configuration. (fyi listener global pointer) i'm trying like:

to kill listener:

func killproxy() {      if listener != nil {         log.println(" *** trying stop proxy server")         (*listener).close()         listener = nil     } } 

before reconfiguring like:

log.println("listener (s1): ", listener) if listener == nil {     // start listener listen on connection.     l, err := net.listen(conn_type, conn_host + ":" + conn_port)      if err != nil {         log.println("error here: ", err)     }         listener = &l //set pointer newly created listener }  log.println("listener (s2): ", listener) 

however doesnt seem work - error:

listener (s1):

error here: listen tcp 0.0.0.0:8080: bind: address in use

listener (s2): 0xc2080015e0

and massive stack trace summizes to:

panic: runtime error: invalid memory address or nil pointer dereference

edit in response jim:

interesting re pointers. ok. not handling waiting socket close, im not sure how should done. proxy library using one: https://github.com/abourget/goproxy. order things happen is:

killproxy() refreshproxy() 

refreshproxy holds code posted above tries repurpose listener. last thing happens in refreshproxy() is:

go http.serve(*listener, proxy)

so if revert listener being global variable, not pointer can make killproxy():

func killproxy() {     if listener != nil {         listener.close()         listener = nil     } } 

and setup listener again as

listener, err := net.listen(conn_type, conn_host + ":" + conn_port)      if err != nil {         log.println("error here: ", err)     }     

however don't know how wait , check whether socket has closed before trying re create listener object?

ok far perfect, way found of not having error out on me artifically put delay in code socket had time close. isn't great means proxy down 2 seconds while socket closes, @ least doesnt error out. if has better solution hear it

func killproxy() {     if listener != nil {         listener.close()         log.println("waiting socket close")         time.sleep(2 * time.second)     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -