ObjFW  Check-in [3f27f9cfbd]

Overview
Comment:OFDNSResolver: Invalidate cache on config changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3f27f9cfbdf15c6c26cc4dbcf647a09f2395422ab8097675f65edecd5c354766
User & Date: js on 2024-01-02 17:14:31
Other Links: manifest | tags
Context
2024-01-02
17:17
Update copyright check-in: 26ddd2e4e4 user: js tags: trunk
17:14
OFDNSResolver: Invalidate cache on config changes check-in: 3f27f9cfbd user: js tags: trunk
14:26
OFDNSResolver: Add warnings about config reloading check-in: b4507c354a user: js tags: trunk
Changes

Modified src/OFDNSResolver.h from [9ab49908f1] to [92b2145ac6].

26
27
28
29
30
31
32

33
34
35
36
37
38
39

@class OFArray OF_GENERIC(ObjectType);
@class OFDNSResolver;
@class OFDNSResolverContext;
@class OFDNSResolverSettings;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);

@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;
@class OFPair OF_GENERIC(FirstType, SecondType);
@class OFTCPSocket;
@class OFUDPSocket;

/**







>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

@class OFArray OF_GENERIC(ObjectType);
@class OFDNSResolver;
@class OFDNSResolverContext;
@class OFDNSResolverSettings;
@class OFDate;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableArray OF_GENERIC(ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;
@class OFPair OF_GENERIC(FirstType, SecondType);
@class OFTCPSocket;
@class OFUDPSocket;

/**
133
134
135
136
137
138
139

140
141
142
143
144
145
146
	char _buffer[OFDNSResolverBufferLength];
	OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
	    *_queries;
	OFMutableDictionary OF_GENERIC(OFTCPSocket *, OFDNSResolverContext *)
	    *_TCPQueries;
	OFMutableDictionary OF_GENERIC(OFDNSQuery *,
	    OFPair OF_GENERIC(OFDate *, OFDNSResponse *) *) *_cache;

	OFTimeInterval _lastCacheCleanup;
}

/**
 * @brief A dictionary of static hosts.
 *
 * This dictionary is checked before actually looking up a host.







>







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	char _buffer[OFDNSResolverBufferLength];
	OFMutableDictionary OF_GENERIC(OFNumber *, OFDNSResolverContext *)
	    *_queries;
	OFMutableDictionary OF_GENERIC(OFTCPSocket *, OFDNSResolverContext *)
	    *_TCPQueries;
	OFMutableDictionary OF_GENERIC(OFDNSQuery *,
	    OFPair OF_GENERIC(OFDate *, OFDNSResponse *) *) *_cache;
	OFMutableArray OF_GENERIC(OFString *) *_lastNameServers;
	OFTimeInterval _lastCacheCleanup;
}

/**
 * @brief A dictionary of static hosts.
 *
 * This dictionary is checked before actually looking up a host.
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
 *	    to disable reloading the config after some time. If you don't, the
 *	    config will be reloaded and your change overridden.
 */
@property (copy, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers;

/**
 * @brief The local domain.
 *
 * @warning If you change this, you need to set @ref configReloadInterval to 0
 *	    to disable reloading the config after some time. If you don't, the
 *	    config will be reloaded and your change overridden.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *localDomain;

/**
 * @brief The domains to search for queries for short names.
 *
 * @warning If you change this, you need to set @ref configReloadInterval to 0







<
<
<
<







163
164
165
166
167
168
169




170
171
172
173
174
175
176
 *	    to disable reloading the config after some time. If you don't, the
 *	    config will be reloaded and your change overridden.
 */
@property (copy, nonatomic) OFArray OF_GENERIC(OFString *) *nameServers;

/**
 * @brief The local domain.




 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *localDomain;

/**
 * @brief The domains to search for queries for short names.
 *
 * @warning If you change this, you need to set @ref configReloadInterval to 0

Modified src/OFDNSResolver.m from [033477a200] to [002fb3b586].

613
614
615
616
617
618
619

620
621
622
623
624
625
626
#ifdef OF_HAVE_IPV6
	[_IPv6Socket cancelAsyncRequests];
	[_IPv6Socket release];
#endif
	[_queries release];
	[_TCPQueries release];
	[_cache release];


	[super dealloc];
}

- (OFDictionary *)staticHosts
{
	return _settings->_staticHosts;







>







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#ifdef OF_HAVE_IPV6
	[_IPv6Socket cancelAsyncRequests];
	[_IPv6Socket release];
#endif
	[_queries release];
	[_TCPQueries release];
	[_cache release];
	[_lastNameServers release];

	[super dealloc];
}

- (OFDictionary *)staticHosts
{
	return _settings->_staticHosts;
803
804
805
806
807
808
809
810
811
812











813
814
815
816
817
818
819
	[sock asyncReceiveIntoBuffer: _buffer
			      length: bufferLength
			 runLoopMode: runLoopMode];
}

- (void)of_cleanUpCache
{
	void *pool = objc_autoreleasePoolPush();
	OFTimeInterval now = [[OFDate date] timeIntervalSince1970];
	OFMutableArray *removeList;












	if (now - _lastCacheCleanup < 1)
		return;

	_lastCacheCleanup = now;
	removeList = [OFMutableArray arrayWithCapacity: _cache.count];








<


>
>
>
>
>
>
>
>
>
>
>







804
805
806
807
808
809
810

811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
	[sock asyncReceiveIntoBuffer: _buffer
			      length: bufferLength
			 runLoopMode: runLoopMode];
}

- (void)of_cleanUpCache
{

	OFTimeInterval now = [[OFDate date] timeIntervalSince1970];
	OFMutableArray *removeList;

	if (_lastNameServers != _settings->_nameServers &&
	    ![_lastNameServers isEqual: _settings->_nameServers]) {
		OFArray *old = _lastNameServers;
		_lastNameServers = [_settings->_nameServers copy];
		[old release];

		[_cache removeAllObjects];

		return;
	}

	if (now - _lastCacheCleanup < 1)
		return;

	_lastCacheCleanup = now;
	removeList = [OFMutableArray arrayWithCapacity: _cache.count];

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
		    containsExpiredRecord(response.authorityRecords, age) ||
		    containsExpiredRecord(response.additionalRecords, age))
			[removeList addObject: query];
	}

	for (OFDNSQuery *query in removeList)
		[_cache removeObjectForKey: query];

	objc_autoreleasePoolPop(pool);
}

- (void)asyncPerformQuery: (OFDNSQuery *)query
		 delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
	[self asyncPerformQuery: query
		    runLoopMode: OFDefaultRunLoopMode







<
<







839
840
841
842
843
844
845


846
847
848
849
850
851
852
		    containsExpiredRecord(response.authorityRecords, age) ||
		    containsExpiredRecord(response.additionalRecords, age))
			[removeList addObject: query];
	}

	for (OFDNSQuery *query in removeList)
		[_cache removeObjectForKey: query];


}

- (void)asyncPerformQuery: (OFDNSQuery *)query
		 delegate: (id <OFDNSResolverQueryDelegate>)delegate
{
	[self asyncPerformQuery: query
		    runLoopMode: OFDefaultRunLoopMode
1104
1105
1106
1107
1108
1109
1110


1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
	} @catch (id e) {
		exception = e;
	}

	if (exception != nil)
		response = nil;



	if (response != nil)
		[_cache setObject: [OFPair pairWithFirstObject: [OFDate date]
						  secondObject: response]
			   forKey: context->_query];
	else
		[_cache removeObjectForKey: context->_query];

	[self of_cleanUpCache];

	[context->_delegate resolver: self
		     didPerformQuery: context->_query
			    response: response
			   exception: exception];

	return false;
}







>
>







<
<







1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128


1129
1130
1131
1132
1133
1134
1135
	} @catch (id e) {
		exception = e;
	}

	if (exception != nil)
		response = nil;

	[self of_cleanUpCache];

	if (response != nil)
		[_cache setObject: [OFPair pairWithFirstObject: [OFDate date]
						  secondObject: response]
			   forKey: context->_query];
	else
		[_cache removeObjectForKey: context->_query];



	[context->_delegate resolver: self
		     didPerformQuery: context->_query
			    response: response
			   exception: exception];

	return false;
}