2023-10-19 10:19:10 +00:00
|
|
|
package dialect
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
|
|
|
|
type ConnectionInfo struct {
|
|
|
|
EventstorePusher ConnectionConfig
|
|
|
|
ZITADEL ConnectionConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionConfig struct {
|
|
|
|
MaxOpenConns,
|
|
|
|
MaxIdleConns uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnectionInfo(openConns, idleConns uint32, pusherRatio float64) (*ConnectionInfo, error) {
|
|
|
|
if pusherRatio < 0 || pusherRatio > 1 {
|
|
|
|
return nil, errors.New("EventPushConnRatio must be between 0 and 1")
|
|
|
|
}
|
2023-10-19 13:37:22 +00:00
|
|
|
if openConns != 0 && openConns < 2 {
|
2023-10-19 10:19:10 +00:00
|
|
|
return nil, errors.New("MaxOpenConns of the database must be higher that 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
info := new(ConnectionInfo)
|
|
|
|
|
|
|
|
info.EventstorePusher.MaxOpenConns = uint32(pusherRatio * float64(openConns))
|
|
|
|
info.EventstorePusher.MaxIdleConns = uint32(pusherRatio * float64(idleConns))
|
|
|
|
|
2023-10-19 13:37:22 +00:00
|
|
|
if openConns != 0 && info.EventstorePusher.MaxOpenConns < 1 && pusherRatio > 0 {
|
2023-10-19 10:19:10 +00:00
|
|
|
info.EventstorePusher.MaxOpenConns = 1
|
|
|
|
}
|
2023-10-19 13:37:22 +00:00
|
|
|
if idleConns != 0 && info.EventstorePusher.MaxIdleConns < 1 && pusherRatio > 0 {
|
2023-10-19 10:19:10 +00:00
|
|
|
info.EventstorePusher.MaxIdleConns = 1
|
|
|
|
}
|
|
|
|
|
2023-10-19 13:37:22 +00:00
|
|
|
if openConns != 0 {
|
|
|
|
info.ZITADEL.MaxOpenConns = openConns - info.EventstorePusher.MaxOpenConns
|
|
|
|
}
|
|
|
|
if idleConns != 0 {
|
|
|
|
info.ZITADEL.MaxIdleConns = idleConns - info.EventstorePusher.MaxIdleConns
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|