Move identd to separate package

This commit is contained in:
Simon Ser 2022-05-09 16:36:54 +02:00
parent 620a8789b0
commit 95db42e645
3 changed files with 11 additions and 6 deletions

View File

@ -25,6 +25,7 @@ import (
"git.sr.ht/~emersion/soju"
"git.sr.ht/~emersion/soju/config"
"git.sr.ht/~emersion/soju/database"
"git.sr.ht/~emersion/soju/identd"
)
// TCP keep-alive interval for downstream TCP connections
@ -235,7 +236,7 @@ func main() {
}()
case "ident":
if srv.Identd == nil {
srv.Identd = soju.NewIdentd()
srv.Identd = identd.New()
}
host := u.Host
@ -247,6 +248,7 @@ func main() {
log.Fatalf("failed to start listener on %q: %v", listen, err)
}
ln = proxyProtoListener(ln, srv)
ln = soju.NewRetryListener(ln)
go func() {
if err := srv.Identd.Serve(ln); err != nil {
log.Printf("serving %q: %v", listen, err)

View File

@ -1,4 +1,4 @@
package soju
package identd
import (
"bufio"
@ -49,7 +49,7 @@ type Identd struct {
lock sync.RWMutex
}
func NewIdentd() *Identd {
func New() *Identd {
return &Identd{entries: make(map[identKey]string)}
}
@ -74,8 +74,6 @@ func (s *Identd) Delete(remoteAddr, localAddr string) {
}
func (s *Identd) Serve(ln net.Listener) error {
ln = &retryListener{Listener: ln}
for {
conn, err := ln.Accept()
if err != nil {

View File

@ -21,6 +21,7 @@ import (
"git.sr.ht/~emersion/soju/config"
"git.sr.ht/~emersion/soju/database"
"git.sr.ht/~emersion/soju/identd"
)
// TODO: make configurable
@ -101,6 +102,10 @@ type retryListener struct {
delay time.Duration
}
func NewRetryListener(ln net.Listener) net.Listener {
return &retryListener{Listener: ln}
}
func (ln *retryListener) Accept() (net.Conn, error) {
for {
conn, err := ln.Listener.Accept()
@ -138,7 +143,7 @@ type Config struct {
type Server struct {
Logger Logger
Identd *Identd // can be nil
Identd *identd.Identd // can be nil
MetricsRegistry prometheus.Registerer // can be nil
config atomic.Value // *Config