Refactor Client struct to improve organization and clarity

Rearranged and grouped struct fields more logically within Client. Introduced the dialContextFunc and fallbackPort fields to enhance connection flexibility. Minor code style adjustments were also made for better readability.
This commit is contained in:
Winni Neessen 2024-09-27 10:36:09 +02:00
parent 23c71d608f
commit 2084526c77
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -88,13 +88,15 @@ type DialContextFunc func(ctx context.Context, network, address string) (net.Con
// Client is the SMTP client struct // Client is the SMTP client struct
type Client struct { type Client struct {
mutex sync.RWMutex
// connection is the net.Conn that the smtp.Client is based on // connection is the net.Conn that the smtp.Client is based on
connection net.Conn connection net.Conn
// Timeout for the SMTP server connection // Timeout for the SMTP server connection
connTimeout time.Duration connTimeout time.Duration
// dialContextFunc is a custom DialContext function to dial target SMTP server
dialContextFunc DialContextFunc
// dsn indicates that we want to use DSN for the Client // dsn indicates that we want to use DSN for the Client
dsn bool dsn bool
@ -104,11 +106,9 @@ type Client struct {
// dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled // dsnrntype defines the DSNRcptNotifyOption in case DSN is enabled
dsnrntype []string dsnrntype []string
// isEncrypted indicates if a Client connection is encrypted or not // fallbackPort is used as an alternative port number in case the primary port is unavailable or
isEncrypted bool // fails to bind.
fallbackPort int
// noNoop indicates the Noop is to be skipped
noNoop bool
// HELO/EHLO string for the greeting the target SMTP server // HELO/EHLO string for the greeting the target SMTP server
helo string helo string
@ -116,12 +116,24 @@ type Client struct {
// Hostname of the target SMTP server to connect to // Hostname of the target SMTP server to connect to
host string host string
// isEncrypted indicates if a Client connection is encrypted or not
isEncrypted bool
// logger is a logger that implements the log.Logger interface
logger log.Logger
// mutex is used to synchronize access to shared resources, ensuring that only one goroutine can
// modify them at a time.
mutex sync.RWMutex
// noNoop indicates the Noop is to be skipped
noNoop bool
// pass is the corresponding SMTP AUTH password // pass is the corresponding SMTP AUTH password
pass string pass string
// Port of the SMTP server to connect to // port specifies the network port number on which the server listens for incoming connections.
port int port int
fallbackPort int
// smtpAuth is a pointer to smtp.Auth // smtpAuth is a pointer to smtp.Auth
smtpAuth smtp.Auth smtpAuth smtp.Auth
@ -132,26 +144,20 @@ type Client struct {
// smtpClient is the smtp.Client that is set up when using the Dial*() methods // smtpClient is the smtp.Client that is set up when using the Dial*() methods
smtpClient *smtp.Client smtpClient *smtp.Client
// Use SSL for the connection
useSSL bool
// tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol // tlspolicy sets the client to use the provided TLSPolicy for the STARTTLS protocol
tlspolicy TLSPolicy tlspolicy TLSPolicy
// tlsconfig represents the tls.Config setting for the STARTTLS connection // tlsconfig represents the tls.Config setting for the STARTTLS connection
tlsconfig *tls.Config tlsconfig *tls.Config
// user is the SMTP AUTH username
user string
// useDebugLog enables the debug logging on the SMTP client // useDebugLog enables the debug logging on the SMTP client
useDebugLog bool useDebugLog bool
// logger is a logger that implements the log.Logger interface // user is the SMTP AUTH username
logger log.Logger user string
// dialContextFunc is a custom DialContext function to dial target SMTP server // Use SSL for the connection
dialContextFunc DialContextFunc useSSL bool
} }
// Option returns a function that can be used for grouping Client options // Option returns a function that can be used for grouping Client options