tidwall / redcon

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

possible to do a reuse port option? #64

Open kolinfluence opened 1 year ago

kolinfluence commented 1 year ago

possible to do a reuse port option?

how do i modify the code for reuse port to work?

kolinfluence commented 1 year ago

@tidwall i've modified the code for reuse port to work BUT it's not elegant.

  1. can u pls standardize it into the package?
  2. also can u also add context to it?
  3. can i buy u a coffee for doing those 2 above?

been using this package and i can only say it's great! thx!

tidwall commented 1 year ago

Sorry, but unfortunately I don't have the available time necessary to dedicate to new features for this project.

kolinfluence commented 1 year ago

@tidwall i have done the below coding. would like your guidance on how u would like call the reuse port and "add context" in. to add the feature is 15-20 mins of ur time. if u find the time in any coffee breaks, do hope to see it implemented.

only needing to add these two codes BUT not sure how u would let users define it.

    config = &net.ListenConfig{Control: func (network, address string, conn syscall.RawConn) error {
        return conn.Control(func(descriptor uintptr) {
            unix.SetsockoptInt(int(descriptor), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
        })
    }}
// ListenServeAndSignal serves incoming connections and passes nil or error
// when listening. signal can be nil.
func (s *Server) ListenServeAndSignal(signal chan error) error {
        //if port reuse option selected, use the config.Listen addition above. else normal net stuff
        //the context.Background() can also be a user-defined option
    ln, err := config.Listen(context.Background(), s.net, s.laddr)
    if err != nil {
        if signal != nil {
            signal <- err
        }
        return err
    }
    s.mu.Lock()
    s.ln = ln
    s.mu.Unlock()
    if signal != nil {
        signal <- nil
    }
    return serve(s)
}