@@ -74,23 +74,24 @@ type rotatingAuth struct {
7474 csaPubKey ed25519.PublicKey
7575 signer Signer
7676 signerTimeout time.Duration
77- headers map [string ]string
77+ headers atomic. Value // stores map[string]string
7878 ttl time.Duration
7979 lastUpdatedNanos atomic.Int64
8080 requireTransportSecurity bool
8181 mu sync.Mutex
8282}
8383
8484func NewRotatingAuth (csaPubKey ed25519.PublicKey , signer Signer , ttl time.Duration , requireTransportSecurity bool ) Auth {
85- return & rotatingAuth {
85+ r := & rotatingAuth {
8686 csaPubKey : csaPubKey ,
8787 signer : signer ,
8888 signerTimeout : time .Second * 5 ,
8989 ttl : ttl ,
90- headers : make (map [string ]string ),
9190 lastUpdatedNanos : atomic.Int64 {},
9291 requireTransportSecurity : requireTransportSecurity ,
9392 }
93+ r .headers .Store (make (map [string ]string ))
94+ return r
9495}
9596
9697func (r * rotatingAuth ) Headers (ctx context.Context ) (map [string ]string , error ) {
@@ -107,7 +108,7 @@ func (r *rotatingAuth) Headers(ctx context.Context) (map[string]string, error) {
107108 // updated the headers and lastUpdated while waiting for the lock.
108109 lastUpdated = time .Unix (0 , r .lastUpdatedNanos .Load ())
109110 if time .Since (lastUpdated ) < r .ttl {
110- return r .headers , nil
111+ return r .headers . Load ().( map [ string ] string ) , nil
111112 }
112113
113114 // Append the bytes of the public key with bytes of the timestamp to create the message to sign
@@ -125,11 +126,14 @@ func (r *rotatingAuth) Headers(ctx context.Context) (map[string]string, error) {
125126 return nil , fmt .Errorf ("beholder: failed to sign auth header: %w" , err )
126127 }
127128
128- r .headers [authHeaderKey ] = fmt .Sprintf ("%s:%x:%d:%x" , authHeaderV2 , r .csaPubKey , ts .UnixNano (), signature )
129+ headers := r .headers .Load ().(map [string ]string )
130+ headers [authHeaderKey ] = fmt .Sprintf ("%s:%x:%d:%x" , authHeaderV2 , r .csaPubKey , ts .UnixNano (), signature )
131+
132+ r .headers .Store (headers )
129133 r .lastUpdatedNanos .Store (ts .UnixNano ())
130134 }
131135
132- return r .headers , nil
136+ return r .headers . Load ().( map [ string ] string ) , nil
133137}
134138
135139func (a * rotatingAuth ) Credentials () credentials.PerRPCCredentials {
0 commit comments