tidwall / redcon

Redis compatible server framework for Go
MIT License
2.19k stars 158 forks source link

How do you accept port 6380 and socket at the same time? #41

Closed hiqsociety closed 3 years ago

hiqsociety commented 3 years ago

with reference to this: https://github.com/tidwall/redcon/issues/21

How can it be done? I know one or the other but not both.

Thank you for redcon. It's truly amazing.

tidwall commented 3 years ago

Your best bet is to listen on both sockets in different goroutines first and share the same handler

package main

import (
    "os"

    "github.com/tidwall/redcon"
)

func main() {
    go func() {
        panic(redcon.ListenAndServeNetwork("tcp", ":6380",
            handler, accept, close))
    }()
    go func() {
        os.RemoveAll("my-unix-path")
        panic(redcon.ListenAndServeNetwork("unix", "my-unix-path",
            handler, accept, close))
    }()
    println("* Serving on port 6380 and on socket my-unix-path")
    select {}
}

func handler(conn redcon.Conn, cmd redcon.Command) {
}
func accept(conn redcon.Conn) bool {

    return true
}
func close(conn redcon.Conn, err error) {

}
tidwall commented 3 years ago

I'm closing this issue for now. Please feel free to reopen if you have further questions.