Skip to content

Commit

Permalink
fix: various problems with connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
wilriker committed Sep 25, 2023
1 parent e699da1 commit 933f82e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
28 changes: 15 additions & 13 deletions nntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import (
)

type safeConn struct {
mutex *sync.Mutex
closed bool
*nntp.Conn
}

var connectionGuard chan struct{}
var (
initConnGuard sync.Once
connectionGuard chan struct{}
)

func ConnectNNTP() (*safeConn, error) {
if connectionGuard == nil {
initConnGuard.Do(func() {
connectionGuard = make(chan struct{}, conf.Directsearch.Connections)
}
})
connectionGuard <- struct{}{} // will block if guard channel is already filled
var conn *nntp.Conn
var err error
Expand All @@ -29,29 +31,29 @@ func ConnectNNTP() (*safeConn, error) {
conn, err = nntp.Dial("tcp", conf.Directsearch.Host+":"+strconv.Itoa(conf.Directsearch.Port))
}
safeConn := safeConn{
&mutex,
false,
conn,
Conn: conn,
}
if err != nil {
safeConn.close()
safeConn.Close()
return nil, fmt.Errorf("Connection to usenet server failed: %v\r\n", err)
}
if err = safeConn.Authenticate(conf.Directsearch.Username, conf.Directsearch.Password); err != nil {
safeConn.close()
safeConn.Close()
return nil, fmt.Errorf("Authentication with usenet server failed: %v\r\n", err)
}
return &safeConn, nil
}

func (c *safeConn) close() {
c.mutex.Lock()
func (c *safeConn) Close() {
mutex.Lock()
defer mutex.Unlock()
if !c.closed {
c.Quit()
if c.Conn != nil {
c.Quit()
}
if len(connectionGuard) > 0 {
<-connectionGuard
}
c.closed = true
}
c.mutex.Unlock()
}
8 changes: 4 additions & 4 deletions nzbdirectsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func searchInGroup(group string) error {
endDate = args.UnixDate + int64(60*60*conf.Directsearch.Forward_hours)
var currentMessageID int
conn, firstMessageID, lastMessageID, err := switchToGroup(group)
defer conn.close()
if err != nil {
return err
}
defer conn.Close()
Log.Info("Scanning from %s to %s", time.Unix(startDate, 0).Format("02.01.2006 15:04:05"), time.Unix(endDate, 0).Format("02.01.2006 15:04:05"))
currentMessageID, _, err = scanForDate(conn, firstMessageID, lastMessageID, -interval, true, " Scanning for first message ID ...")
if err != nil {
Expand All @@ -125,7 +125,7 @@ func searchInGroup(group string) error {
if currentMessageID >= lastMessageID {
return errors.New("no messages found within search range")
}
conn.close()
conn.Close()
directsearchCounter = 0
bar := progressbar.NewOptions(lastMessageID-currentMessageID,
progressbar.OptionSetDescription(" Scanning messages ... "),
Expand Down Expand Up @@ -187,10 +187,10 @@ func searchMessages(ctx context.Context, firstMessage int, lastMessage int, grou
default: // required, otherwise it will block
}
conn, firstMessageID, lastMessageID, err := switchToGroup(group)
defer conn.close()
if err != nil {
return err
}
defer conn.Close()
if firstMessage < firstMessageID {
firstMessage = firstMessageID
}
Expand All @@ -203,7 +203,7 @@ func searchMessages(ctx context.Context, firstMessage int, lastMessage int, grou
default: // required, otherwise it will block
}
results, err := conn.Overview(firstMessage, lastMessage)
conn.close()
conn.Close()
if err != nil {
return fmt.Errorf("Error retrieving message overview from the usenet server while searching in group '%s': %v\n", group, err)
}
Expand Down

0 comments on commit 933f82e

Please sign in to comment.