|
| 1 | +package streaming |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/redis/go-redis/v9/auth" |
| 5 | + "github.com/redis/go-redis/v9/internal/pool" |
| 6 | +) |
| 7 | + |
| 8 | +// ConnReAuthCredentialsListener is a struct that implements the CredentialsListener interface. |
| 9 | +// It is used to re-authenticate the credentials when they are updated. |
| 10 | +// It holds reference to the connection to re-authenticate and will pass it to the reAuth and onErr callbacks. |
| 11 | +// It contains: |
| 12 | +// - reAuth: a function that takes the new credentials and returns an error if any. |
| 13 | +// - onErr: a function that takes an error and handles it. |
| 14 | +// - conn: the connection to re-authenticate. |
| 15 | +type ConnReAuthCredentialsListener struct { |
| 16 | + // reAuth is called when the credentials are updated. |
| 17 | + reAuth func(conn *pool.Conn, credentials auth.Credentials) error |
| 18 | + // onErr is called when an error occurs. |
| 19 | + onErr func(conn *pool.Conn, err error) |
| 20 | + // conn is the connection to re-authenticate. |
| 21 | + conn *pool.Conn |
| 22 | + |
| 23 | + manager *Manager |
| 24 | +} |
| 25 | + |
| 26 | +// OnNext is called when the credentials are updated. |
| 27 | +// It calls the reAuth function with the new credentials. |
| 28 | +// If the reAuth function returns an error, it calls the onErr function with the error. |
| 29 | +func (c *ConnReAuthCredentialsListener) OnNext(credentials auth.Credentials) { |
| 30 | + if c.conn.IsClosed() { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + if c.reAuth == nil { |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + c.manager.MarkForReAuth(c.conn, func(err error) { |
| 39 | + if err != nil { |
| 40 | + c.OnError(err) |
| 41 | + return |
| 42 | + } |
| 43 | + err = c.reAuth(c.conn, credentials) |
| 44 | + if err != nil { |
| 45 | + c.OnError(err) |
| 46 | + return |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +// OnError is called when an error occurs. |
| 53 | +// It can be called from both the credentials provider and the reAuth function. |
| 54 | +func (c *ConnReAuthCredentialsListener) OnError(err error) { |
| 55 | + if c.onErr == nil { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + c.onErr(c.conn, err) |
| 60 | +} |
| 61 | + |
| 62 | +// newConnReAuthCredentialsListener creates a new ConnReAuthCredentialsListener. |
| 63 | +// Implements the auth.CredentialsListener interface. |
| 64 | +func newConnReAuthCredentialsListener( |
| 65 | + conn *pool.Conn, |
| 66 | + reAuth func(conn *pool.Conn, credentials auth.Credentials) error, |
| 67 | + onErr func(conn *pool.Conn, err error), |
| 68 | +) *ConnReAuthCredentialsListener { |
| 69 | + return &ConnReAuthCredentialsListener{ |
| 70 | + conn: conn, |
| 71 | + reAuth: reAuth, |
| 72 | + onErr: onErr, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Ensure ConnReAuthCredentialsListener implements the CredentialsListener interface. |
| 77 | +var _ auth.CredentialsListener = (*ConnReAuthCredentialsListener)(nil) |
0 commit comments